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

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!

Replies are listed 'Best First'.
Re^5: awk like question in perl
by davido (Cardinal) on Aug 12, 2004 at 20:45 UTC
    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.


      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.

        The snippet I provided has nothing to do with opening files.

        If you read the documentation for split in the links I provided, you'll see that split can take on several args or none:

        split /regexp/, $splitstring, $quantity; split /regexp/, $splitstring; # quantity is assumed to be as many # as possible. split /regexp/; # $_ will be split up. split; # $_ will be split up as if the regex # looked like //.

        print is a function, and thus, kinda likes to be called like this: print( "stuff" );. You can also call it like print "stuff";, but if the first thing to come after print is an opening parenthesis, print assumes that what follows is the args to the print function.

        However, when you say (split), you're using the parens as a list constructor, to provide list context to split, not as a parameter set for print. So how do you specify to print that you want to treat the () as a list constructor or as grouping parens instead of as function parameter parens? You treat the parens as though they're part of an expression, by prepending the unary + sign.

        Next, the [1,5] takes a slice of the list created by (split), returning only the 2nd and 6th elements.

        This is all kind of idiomatic, which is why I recommend backing up a step and reading the docs. Once you've done that, you should have that lightbulb flick on and you'll catch on to what's going on.