Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: using fields in perl

by hsinclai (Deacon)
on Dec 25, 2007 at 04:14 UTC ( [id://658946]=note: print w/replies, xml ) Need Help??


in reply to using fields in perl

Yes, autosplit :)

perl -ane 'print $F[0] . $/;' FILE is close to what you'd get with awk '{print $1}' FILE

Do perldoc perlrun, look for -a for the gory details.

Replies are listed 'Best First'.
Re^2: using fields in perl
by ysth (Canon) on Dec 25, 2007 at 05:21 UTC
      You guys, I'm not sure '-a' is quite equivalent to the awk fields. I'm no good at awk, but I believe it splits on m/\s+/ rather than \s. perlrun says '-a' is this:
      while (<>) { @F = split(' '); print pop(@F), "\n"; }

      But I think you'd have to @F = grep {$_} @F or change the split or something.

      no?

      (Also, I think elan should work. I mean, I know it doesn't, but isn't there some posix-y arg processing style where if 'a' and 'b' take arguments "-dacbe aarg barg" still works? Perhaps I imagined that.)

      UPDATE: oh, jstrom, I did *not* know that... I assumed split splooged any argument into a regex.

      -Paul

        From perlfunc's entry on split:
        As a special case, specifying a PATTERN of space (' ') will split on white space just as split with no arguments does. Thus, split(' ') can be used to emulate awk's default behavior, whereas split(/ /) will give you as many null initial fields as there are leading spaces. A split on /\s+/ is like a split(' ') except that any leading whitespace produces a null first field. A split with no arguments really does a split(' ', $_) internally.
        (Also, I think elan should work. I mean, I know it doesn't, but isn't there some posix-y arg processing style where if 'a' and 'b' take arguments "-dacbe aarg barg" still works? Perhaps I imagined that.)
        If so, perl doesn't use such a style. But perl does allow bundling -l even though it takes an octal argument, so long as the switch after it doesn't look like an octal number.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found