Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a split expression in Perl?

I want to create a reference to an array obtained by a split in Perl. I'm thinking something like:

my $test = \split( /,/, 'a,b,c,d,e');

foreach $k (@$test) {
   print "k is $k\n";
}

But that complains with Not an ARRAY reference at c:\temp\test.pl line 3. I tried a few other alternatives, all without success.

like image 920
dr jerry Avatar asked Dec 09 '11 11:12

dr jerry


People also ask

What does split command do in Perl?

split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc..

How do I split a string with multiple delimiters in Perl?

A string is splitted based on delimiter specified by pattern. By default, it whitespace is assumed as delimiter. split syntax is: Split /pattern/, variableName.

How do I split a space in Perl?

The split() function is used to divide any string based on any particular delimiter and if no delimiter is provided the space is used as the default delimiter. The delimiter can be a character, a list of characters, a regular expression pattern, the hash value, and an undefined value.


1 Answers

Background explanation:

split, like other functions, returns a list. You cannot take a reference to a list. However, if you apply the reference operator to a list, it gets applied to all its members. For example:

use Data::Dumper;

my @x = \('a' .. 'c');

print Dumper \@x

Output:

$VAR1 = [
          \'a',
          \'b',
          \'c'
        ];

Therefore, when you write my $test = \split( /,/, 'a,b,c,d,e');, you get a reference to the last element of the returned list (see, for example, What’s the difference between a list and an array?). Your situation is similar to:

Although it looks like you have a list on the righthand side, Perl actually sees a bunch of scalars separated by a comma:

my $scalar = ( 'dog', 'cat', 'bird' );  # $scalar gets bird

Since you’re assigning to a scalar, the righthand side is in scalar context. The comma operator (yes, it’s an operator!) in scalar context evaluates its lefthand side, throws away the result, and evaluates it’s righthand side and returns the result. In effect, that list-lookalike assigns to $scalar it’s rightmost value. Many people mess this up becuase they choose a list-lookalike whose last element is also the count they expect:

my $scalar = ( 1, 2, 3 );  # $scalar gets 3, accidentally

In your case, what you get on the RHS is a list of references to the elements of the list returned by split, and the last element of that list ends up in $test. You first need to construct an array from those return values and take a reference to that. You can make that a single statement by forming an anonymous array and storing the reference to that in $test:

my $test = [ split( /,/, 'a,b,c,d,e') ];
like image 138
Sinan Ünür Avatar answered Oct 13 '22 11:10

Sinan Ünür