in reply to Re: Search Hash with array element Help!
in thread Search Hash with array element Help!

I really liked ur concise code. :) I didn't understand how the following statement:

my ($num) = $id =~ /(\d+)/ or next;

I understand what the result is but I didn't understand how does that gives you a numeric form and removes the characters. Actually, this is the first time I have seen something like this therefore, can you please forward me some reference material too.

Thanks.

Replies are listed 'Best First'.
Re^3: Search Hash with array element Help!
by ikegami (Patriarch) on Mar 04, 2010 at 22:45 UTC
    The same with parens that illustrate precedence:
    ( my ($num) = ( $id =~ /(\d+)/ ) ) or next;

    The first thing that happens is that $id is matched against /(\d+)/.

    If it matches, a list of all the captured data (that which the content of parens in the pattern matched) is returned by the match operator. The first (and only) element of that list is assigned to $num. The assignment returns 1 (the size of the list returned by the match), so next is skipped.

    If it doesn't matches, an empty list is returned. undef is assigned to $num (which won't be used). The assignment returns zero (the size of the list returned by the match), so next is evaluated and the rest of the loop is skipped.

    All of these operators (match, list assignment and or) are documented in perlop.