Loading ...
Sorry, an error occurred while loading the content.

The war of Perlish proportions

Expand Messages
  • John
    And thus she saideth, to those willing to listen, It began with a simple Perl question: how does one strip whitespace from a string? Two masters with
    Message 1 of 5 , Jul 20, 2007
    • 0 Attachment
      And thus she saideth, to those willing to listen,

      "It began with a simple Perl question: how does one strip whitespace
      from a string? Two masters with radically different styles of helping
      answered, and a conflict erupted, as one who thinks by feeling took
      exception with the answer of one who feels by thinking.

      The two programmers' online battle escalated to the point that neither
      would concede anything. So finally, during a lull in the battle, the
      originator of the question responded. He was both the one who needed
      help and the one who, at the end of the day, negotiated peace. He
      spoke with clarity that both combatants lacked in their war lust: the
      thinker who thinks that people need to be self-sufficient, and who
      feels sick of hand-delivering every answer to people perfectly capable
      of doing the work themselves -- work that HE did when he was a newbie,
      and which helped him become an expert in the field; and the feeler who
      feels (empathizes with) the confusion and intimidation that Perl
      beginners experience, and who thinks that people need a helping hand
      sometimes, even if it means taking an extra five minutes to explain a
      particular point. And in the midst of it all, what was the
      negotiator's wisdom?: "I appreciate both sides of the debate. But it's
      nothing to argue or cause strife over this topic."

      I agree.

      Everyone is different. Everyone has something to offer. Everyone has
      their faults. Rather than arguing over disagreements in one's
      teaching style, these two masters should get down to the task of
      actually teaching."

      And so spoke Mara, divine of peace and compassion in the world of
      Tamriel. Make your donations in American dollars, please.
    • David Seruyange
      Hello everyone, I am writing code that edits a file and was wondering what the best approach is. I ve currently got the following subroutine: sub
      Message 2 of 5 , Jul 22, 2007
      • 0 Attachment
        Hello everyone,

        I am writing code that edits a file and was wondering what the best
        approach is. I've currently got the following subroutine:

        sub removePlanEntry{
        my $fileName = shift;
        my $removeLinenumber = shift;
        open PLAN, "$fileName";
        my @tasks = <PLAN>;
        close PLAN;
        open PLAN, ">$fileName";
        my $taskCount = 1;
        for my $task(@tasks){
        unless($taskCount == $removeLinenumber){
        print PLAN $task;
        }
        $taskCount++;
        }
        close PLAN;
        }

        So I'm essentially slurping (that's the correct verb, yes?) the file
        into an array and then writing it back out skipping the line that is
        meant to be removed. But it seems rather ugly - is there a more compact
        way to do this? I'm assuming I could employ map instead of the for loop
        for writing?

        Thanks!

        David
      • Mr. Shawn H. Corey
        ... I would write it like this: sub removePlanEntry { my $fileName = shift @_; my $removeLinenumber = shift @_; open my $plan,
        Message 3 of 5 , Jul 22, 2007
        • 0 Attachment
          David Seruyange wrote:
          > Hello everyone,
          >
          > I am writing code that edits a file and was wondering what the best
          > approach is. I've currently got the following subroutine:
          >
          > sub removePlanEntry{
          > my $fileName = shift;
          > my $removeLinenumber = shift;
          > open PLAN, "$fileName";
          > my @tasks = <PLAN>;
          > close PLAN;
          > open PLAN, ">$fileName";
          > my $taskCount = 1;
          > for my $task(@tasks){
          > unless($taskCount == $removeLinenumber){
          > print PLAN $task;
          > }
          > $taskCount++;
          > }
          > close PLAN;
          > }
          >
          > So I'm essentially slurping (that's the correct verb, yes?) the file
          > into an array and then writing it back out skipping the line that is
          > meant to be removed. But it seems rather ugly - is there a more compact
          > way to do this? I'm assuming I could employ map instead of the for loop
          > for writing?

          I would write it like this:

          sub removePlanEntry {
          my $fileName = shift @_;
          my $removeLinenumber = shift @_;

          open my $plan, '<', $fileName or die "cannot open $fileName for
          reading: $!";
          my @tasks = <$plan>;
          close $plan;

          # Line numbering starts at 1, array numbering at 0, hence subtract one.
          delete $tasks[$removeLinenumber-1];

          open $plan, '>', $fileName or die "cannot open $fileName for
          writing: $!";
          print $plan @tasks or die "cannot write to $fileName: $!";
          close $plan or die "cannot close $fileName: $!";
          }


          --
          Just my 0.00000002 million dollars worth,
          Shawn

          "For the things we have to learn before we can do them, we learn by
          doing them."
          Aristotle
        • Emen Zhao
          If you are working on a Linux box, here is a more compact way (i hope) perl -wnl -e $. == 1 or print; newfile Replace 1 with the value of
          Message 4 of 5 , Jul 23, 2007
          • 0 Attachment
            If you are working on a Linux box, here is a more compact way (i hope)

            perl -wnl -e '$. == 1 or print;' > newfile

            Replace "1" with the value of $removeLinenumber.

            Thanks,
            Emen

            On 7/22/07, Mr. Shawn H. Corey <shawnhcorey@...> wrote:
            >
            > David Seruyange wrote:
            > > Hello everyone,
            > >
            > > I am writing code that edits a file and was wondering what the best
            > > approach is. I've currently got the following subroutine:
            > >
            > > sub removePlanEntry{
            > > my $fileName = shift;
            > > my $removeLinenumber = shift;
            > > open PLAN, "$fileName";
            > > my @tasks = <PLAN>;
            > > close PLAN;
            > > open PLAN, ">$fileName";
            > > my $taskCount = 1;
            > > for my $task(@tasks){
            > > unless($taskCount == $removeLinenumber){
            > > print PLAN $task;
            > > }
            > > $taskCount++;
            > > }
            > > close PLAN;
            > > }
            > >
            > > So I'm essentially slurping (that's the correct verb, yes?) the file
            > > into an array and then writing it back out skipping the line that is
            > > meant to be removed. But it seems rather ugly - is there a more compact
            > > way to do this? I'm assuming I could employ map instead of the for loop
            > > for writing?
            >
            > I would write it like this:
            >
            > sub removePlanEntry {
            > my $fileName = shift @_;
            > my $removeLinenumber = shift @_;
            >
            > open my $plan, '<', $fileName or die "cannot open $fileName for
            > reading: $!";
            > my @tasks = <$plan>;
            > close $plan;
            >
            > # Line numbering starts at 1, array numbering at 0, hence subtract one.
            > delete $tasks[$removeLinenumber-1];
            >
            > open $plan, '>', $fileName or die "cannot open $fileName for
            > writing: $!";
            > print $plan @tasks or die "cannot write to $fileName: $!";
            > close $plan or die "cannot close $fileName: $!";
            > }
            >
            > --
            > Just my 0.00000002 million dollars worth,
            > Shawn
            >
            > "For the things we have to learn before we can do them, we learn by
            > doing them."
            > Aristotle
            >
            >


            [Non-text portions of this message have been removed]
          • kapil v
            The code looks compact, but seems to be quite inefficient as the number of comparisons of $. with the current line number will be equal to the number of lines
            Message 5 of 5 , Jul 23, 2007
            • 0 Attachment
              The code looks compact, but seems to be quite inefficient as the number of comparisons of $. with the current line number will be equal to the number of lines in the file, which could be considerable. The array solution seems to be more efficient.

              Emen Zhao <emenzhaowork@...> wrote: If you are working on a Linux box, here is a more compact way (i hope)

              perl -wnl -e '$. == 1 or print;' > newfile

              Replace "1" with the value of $removeLinenumber.

              Thanks,
              Emen

              On 7/22/07, Mr. Shawn H. Corey <shawnhcorey@...> wrote:
              >
              > David Seruyange wrote:
              > > Hello everyone,
              > >
              > > I am writing code that edits a file and was wondering what the best
              > > approach is. I've currently got the following subroutine:
              > >
              > > sub removePlanEntry{
              > > my $fileName = shift;
              > > my $removeLinenumber = shift;
              > > open PLAN, "$fileName";
              > > my @tasks = <PLAN>;
              > > close PLAN;
              > > open PLAN, ">$fileName";
              > > my $taskCount = 1;
              > > for my $task(@tasks){
              > > unless($taskCount == $removeLinenumber){
              > > print PLAN $task;
              > > }
              > > $taskCount++;
              > > }
              > > close PLAN;
              > > }
              > >
              > > So I'm essentially slurping (that's the correct verb, yes?) the file
              > > into an array and then writing it back out skipping the line that is
              > > meant to be removed. But it seems rather ugly - is there a more compact
              > > way to do this? I'm assuming I could employ map instead of the for loop
              > > for writing?
              >
              > I would write it like this:
              >
              > sub removePlanEntry {
              > my $fileName = shift @_;
              > my $removeLinenumber = shift @_;
              >
              > open my $plan, '<', $fileName or die "cannot open $fileName for
              > reading: $!";
              > my @tasks = <$plan>;
              > close $plan;
              >
              > # Line numbering starts at 1, array numbering at 0, hence subtract one.
              > delete $tasks[$removeLinenumber-1];
              >
              > open $plan, '>', $fileName or die "cannot open $fileName for
              > writing: $!";
              > print $plan @tasks or die "cannot write to $fileName: $!";
              > close $plan or die "cannot close $fileName: $!";
              > }
              >
              > --
              > Just my 0.00000002 million dollars worth,
              > Shawn
              >
              > "For the things we have to learn before we can do them, we learn by
              > doing them."
              > Aristotle
              >
              >

              [Non-text portions of this message have been removed]






              ---------------------------------
              Yahoo! Answers - Get better answers from someone who knows. Tryit now.

              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.