in reply to Re: using fields in perl
in thread using fields in perl

Perhaps better:
perl -lane'print $F[0]' FILE
(I wish it could be -elan.)

Replies are listed 'Best First'.
Re^3: using fields in perl
by jettero (Monsignor) on Dec 25, 2007 at 13:00 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.

        Im hoping 5.12 will allow one to supply a modifier to the split so you can have the "ignore leading nulls" behaviour with any pattern.

        ---
        $world=~s/war/peace/g

      (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.