Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: regex needed

by Chady (Priest)
on Mar 14, 2003 at 14:18 UTC ( #243050=note: print w/replies, xml ) Need Help??

in reply to regex needed

is foo always the second item?
print (split /:/)[1];

Update: I just noticed that this does not actually run, it spits a syntax error, this, however, works:

print '', (split /:/)[1];
Anyone can explain this a bit? got anything to do with list|scalar context?
He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

Chady |

Replies are listed 'Best First'.
Re:x2 regex needed
by grinder (Bishop) on Mar 14, 2003 at 14:44 UTC

    Operator precedence and print's hunger for a filehandle get in the way. perl parses that as

    (print (split /:/))[1];

    And therefore thinks that (split /:/) should be a filehandle (not sure about that) and then you're taking a slice of the whole mess (which definitely won't fly). Extra parentheses are all you need.

    print( (split /:/)[1] );

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Re: regex needed
by thelenm (Vicar) on Mar 14, 2003 at 17:48 UTC
    Another way to keep the parentheses from being grabbed by print is to add a + in front of them, like this:
    print +(split /:/)[1];
    perlfunc explains why this happens: Perl built-in functions can be called with or without parentheses, so when perl sees parentheses there, it associates them with the function call, not with the creation of a list.

    -- Mike


Re: Re: regex needed
by MrYoya (Monk) on Mar 14, 2003 at 19:52 UTC
    I believe that the
    print (split /:/)[1];
    is not working because it first evaluates the print (split /:/), which returns a scalar value, in this case "1" for a successful print. So it's trying to get a list element of a scalar, which does not work. Same as trying 1[0]; .

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-10-16 01:29 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (82 votes). Check out past polls.