Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are two issues here.

First, in list assignment the first array or hash will soak up all remaining values not yet assigned.

    my ($first, $next, @rest, @empty) = (1, 2, 3, 4, 5);

  
$first is now 1

$next is 2

@rest is (3,4,5)

@empty is undefined;

see: http://perldoc.perl.org/perldata.html#List-value-constructor...

The second issue is that the split() function takes an optional "Limit" argument (split /PATTERN/,EXPR,LIMIT)

This causes the example code (split /-/,$str2, split /;/, $str4) to be evaluated as split /-/,$str2,split(/;/, $str4) which evaluates as split /-/,$str2,2

Here, split /;/,$str4 evaluates to 2 because, in scalar context, split returns the length of the resulting list - since there are two elements after splitting $str4 it returns 2.

In this case, the result of splitting $str2 results in two elements so the limit doesn't matter.

If $str2 were 'This-string-has-many-dashes', the result of (split /-/,$str2, split /;/, $str4) would be:

    array 3[0] is: This
    array 3[1] is: string-has-many-dashes
    array 4[0] is: 
    array 4[1] is:

  
see: http://perldoc.perl.org/functions/split.html


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: