in reply to Re: Re: Extract lines and output...
in thread Extract lines and output...

If you like brief you might like this:
#!/usr/bin/perl -w while(<*>) { next if /^$0$/; open I, $_ or die "$_: $!"; open O,">$_.bak" or die "$_.bak: $!"; print O $_, "\n", (<I>)[0..3]; close I, close O; }

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: Extract lines and output...
by broquaint (Abbot) on Oct 10, 2001 at 13:42 UTC
    Shorter still -
    open(LOG, ">logfile.txt"); while($FL = <*.extension>) { open(FL) and print LOG "$_\n",(<FL>)[0..3] and close(FL) } close(LOG);
    But we could golf this 'til the cows come home :o)
    HTH

    broquaint

Re: Re: Re: Re: Extract lines and output...
by belden (Friar) on Oct 10, 2001 at 08:58 UTC
    I like it really brief. The only reason I have close(I)
    is to reset $. You don't ever use $. so $. doesn't need to
    be reset - therefore the close calls aren't needed. This
    works just as well for me...
    #!/usr/bin/perl -w while(<*>) { next if /^$0$/; open I, $_ or die "$_: $!"; open O,">$_.bak" or die "$_.bak: $!"; print O $_, "\n", (<I>)[0..3]; #close I, close O; }
    A few things I picked up from your code:
    You used .bak instead of .$$, which makes your solution
    portable beyond *nix systems. I like your <*> more than my
    glob. Your treatment of the filehandle as though it were an
    array is also new to me.

    blyman

      As you say you don't have to close filehandles in many cases. It is just tidy to do so. If you want to do it the more politiacally correct way you use a lexically scoped variable as FH. This is destroyed when you go out of scope and is well scoped.

      while(<*>) { /^$0$/ && next; open my $IN, $_ or die "$_: $!";; open my $OUT,">$_.bak" or die "$_.bak: $!"; print $OUT $_, $/, (<$IN>)[0..3]; }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print