richill has asked for the wisdom of the Perl Monks concerning the following question:

I understand that split creates an array from a string by spliting the string at every occurance of a character defined in a regular expression.

So

@array = split(/\s+/, $_)

splits the $_ varible at whitespaces and places the results in array.

@array =split

does the same.
I know that my declares a private varible in a subfunction .So i think that
my ($subdir, @keywords) = split;
takes whatever is on $_ splits it and places it into a private varibles $subdir and @keywords.
What part of $_ does $subdir get?

Replies are listed 'Best First'.
Re: what does my ($subdir, @keywords) = split; do.
by ikegami (Patriarch) on Mar 17, 2006 at 22:13 UTC
    my ($subdir, @keywords) = split;

    is the same as

    my @array = split; my ($subdir, @keywords) = @array;

    is the same as

    my @array = split; my $subdir = $array[0]; my @keywords = @array[1..$#array];

    In other words, $subdir gets whatever's in front of the first space, and @keywords gets all the other fields returned by split (if any).

Re: what does my ($subdir, @keywords) = split; do.
by friedo (Prior) on Mar 17, 2006 at 22:13 UTC
    You're right on the money. In this case, the list taking the results from split consists of $subdir and the array @keywords. So $subdir gets the first item, and @keywords gets all the rest. This is a common idiom in Perl, where you often see things like my ($first, $second, @rest) = @foo;.
Re: what does my ($subdir, @keywords) = split; do.
by Anonymous Monk on Mar 18, 2006 at 00:58 UTC
    I understand that split creates an array from a string
    No, split creates a list.

    What is the difference between a list and an array?

    So @array = split(/\s+/, $_) splits the $_ varible at whitespaces and places the results in array. @array =split does the same.
    No, split with no arguments does something similar but not quite the same as split( /\s+/, $_ ). You will only notice the difference if the string in $_ has leading whitespace.

    my ($subdir, @keywords) = split; assigns the list returned from split to the list in the parentheses. If any of the variables in the parentheses is an array or a hash it will receive the remaining items in the list and any variables following said array or hash will not receive anything. This is all explained in the documentation.

    perldoc perldata

      So as I now understand it

      ($subdir, @keywords) = split

      Creates a list from a string in $_. It assigns the first value of that list into $subdir and the remaining values into the @keywords array.

      ($subdir, @keywords, $empty) = split

      $empty does not recieve anything because all thevalues go into the @keywords array Thanks
Re: what does my ($subdir, @keywords) = split; do.
by GrandFather (Saint) on Mar 17, 2006 at 22:14 UTC

    my () = rhs; puts rhs in a list context. The assignment is made to the elements in the order they are listed. So in the sample you gave the first element in the list ($subdir) gets the first element in the list created by split, and @keywords gets all the rest.


    DWIM is Perl's answer to Gödel
Re: what does my ($subdir, @keywords) = split; do.
by stu42j (Sexton) on Mar 17, 2006 at 22:13 UTC
    $subdir will get the first part.