Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: awk like question in perl

by davido (Cardinal)
on Aug 12, 2004 at 00:28 UTC ( #382132=note: print w/replies, xml ) Need Help??


in reply to Re^2: awk like question in perl
in thread awk like question in perl

I kind of figured splitting on whitespace was the original objective, but I suggested unpack as what I thought might be a better alternative. However, split is pretty simple to use, so here's an example that pulls out the 2nd and 6th fields:

{ local $, = "\t"; print +(split)[1,5], $/ while <DATA>; }

HTH!


Dave

Replies are listed 'Best First'.
Re^4: awk like question in perl
by drock (Beadle) on Aug 12, 2004 at 15:54 UTC
    great thanks alot! You rock! I asked the email list at beginners@perl.org and I received no responses. Anyway I have two questions related to my desired output. 1) from my code I am getting these errors Filehandle FOO opened only for output at derek line 23, <FOO> line 39. 08/09/04 11:02:43 E01220 Filehandle FOO opened only for output at derek line 23, Here is my code:
    ## Define ENV $ENV{"PATH"} = qq(/usr/epoch/bin:/usr/epoch/EB/bin:/usr/bin:/usr/sbin: +/bin:/sbin); delete $ENV{"IFS"}; ## Declare scalers for client templates and client tape logs my $scratches="/usr/local/log/scratch_tapes.log"; my $logf="/usr/epoch/EB/log/ebcat.log"; ## Gather data for scratch tapes; open file, create array list, then p +rint it. open (FOO, ">$scratches") || die "could not open file:$!"; my $scratch_list = `grep 9840S $logf |grep "ebexpire, ebexpire +"`; print FOO "$scratch_list"; while (<FOO>) { local $, = "\t"; print +(split)[0,1,5], $/; } close (FOO);
    can anyone shed some light? Also to use the Perl grep I have code as follows but it is not working.
    ## Define ENV $ENV{"PATH"} = qq(/usr/epoch/bin:/usr/epoch/EB/bin:/usr/bin:/usr/sbin +:/bin:/sbin); delete $ENV{"IFS"}; ## Declare scalers for client templates and client tape logs my $scratches="/usr/local/log/scratch_tapes.log2"; my $logf="/usr/epoch/EB/log/ebcat.log"; open (FOO, ">$scratches") || die "could not open file:$!"; open (D, "$logf") || die "could not open file:$!"; while ( <D> ) { if (( /9840S/ ) && ( /"ebexpire, ebexpire"/ )) { print $_ $scratches } } close (FOO); close (D);
    2 example lines from the ebcat.log looks like 04/29/04 11:01:17 6687:ebexpire, ebexpire.c@7475 E01012 9840S 820 6E01C1874C2E12CA 04/29/04 11:01:17 6687:ebexpire, ebexpire.c@7475 E01093 9840S 898 9601C7263CDC2B34 thank you!

      The problem is that you've opened the filehandle FOO for output, but then you try to read from it with the <FOO> operator. You can't do that. Opening the file for output is going to clobber whatever its contents were, and you can't read from a file opened with ">".

      See perlopentut and perlopen for details.


      Dave

Re^4: awk like question in perl
by drock (Beadle) on Aug 12, 2004 at 20:25 UTC
    so... is the code
    print +(split)[1,5], $/ while <DATA>;
    saying "split <DATA> on the 1st and 5th fields as IRS's for output? But what does the +(split) mean? thanks!
      print +(split)[1,5], $/ while <DATA>;

      Could also be written like this:

      while ( my $line = <DATA> ) { my ( $second, $sixth ) = ( split //, $line )[1,5]; print $second, $sixth, "\n"; }

      The unary + sign is used to signal to Perl that you mean print( (split)[1,5], "\n" ); and not print(split[1,5]),"\n"; (which doesn't make sense, but is confusing to Perl nevertheless.

      For a description of split, see split. For a description of using slices, see perldata. For a description of what $/ is, see perlvar. And for an explanation of the "do something while condition" syntax, see perlsyn.


      Dave

        ok so is the +(split)1,5 referencing the regexp meaning match 1 or more times? or meaning "open for reading and writing?" I a still not understanding what +(split) really means.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-12-10 20:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?