Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Extract lines and output...

by belden (Friar)
on Oct 10, 2001 at 07:51 UTC ( [id://117922]=note: print w/replies, xml ) Need Help??


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

Hi Fletch,

I liked your approach. I'm not sure you need the sub in it
though; you're already doing variable interpolation when you
open( OUT, ">$outfile" ) or die ... so you could lean it up a bit:
open( OUT, ">${_}_distilled" ) or die ...

Here's what your code inspired me to do

#!/usr/bin/perl for(glob '*') { /^$0$/ and next; # don't process the script open(I,"<$_") or die("$_: $!"); open(O,">$_.$$") or die("$_: $!"); print O $_,"\n"; { $. > 4 ? last : print O while(<I>); } close(I); } exit;

While writing and subsequently edting above program I discov-
ered the redundancy of local() in certain contexts. I originally wrote
{ local $_ = undef; $. > 4 ? last : print O while(<I>) }
RRedundancy bad, so local went away.

Update: changed >$outfile_distilled
to ${_}_distilled which actually makes sense
in the context of the original program.

blyman

Replies are listed 'Best First'.
Re: Re: Re: Extract lines and output...
by tachyon (Chancellor) on Oct 10, 2001 at 08:17 UTC
    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

      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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://117922]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-20 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found