Reputation: 767
I want to split a scalar by whitespaces and save the result in an ArrayReference.
use strict;
use warnings;
use Data::Dumper;
my $name = 'hans georg mustermann';
my $array = split ' ', $name;
print Dumper($array); #$VAR1 = 3;
So it seems $array
is now a scalar with the size
resulted by the split
operation.
When i change the code to my $array = [split ' ', $name];
the variable $array
is now a ArrayReference and contains all 3 strings.
I just don't understand this behavior. Would be really great if someone could explain it to me or post a good documentation about these things, as i don't know how to search for this topic.
Thank you in advance
Upvotes: 1
Views: 123
Reputation: 830
What you see here is called "context". The documentation about this is rather scattered. You also want to take a look at this tutorial about "scalar vs list context" https://perlmaven.com/scalar-and-list-context-in-perl
If you assign the result of split (or any subroutine calls) to an array, it's list context:
my @arr = split ' ', $name;
#=> @arr = ('hans', 'georg', 'mustermann');
What your example code shows is assigning them to a scalar -- and therefore it's under "scalar context".
Since, naturally, multiple things cannot fit into one position, some sort of summarization needs to be done. In the case of split
function, perl5 has defined that the number of elements in the result of split
shall be the best.
Check the documentation of the split
function: https://perldoc.pl/functions/split -- which actually defines the behaviour under scalar context as well as list context.
Also take a glance at the documentation of all builtin functions at https://perldoc.pl/functions -- you'll find the behaviour definition under "list context" and "scalar context" for most of them -- although many of them are not returning "the size of lists" but rather something else.
Upvotes: 5
Reputation: 69244
If you read the documentation for split()
, you'll find the bit that explains what the function returns.
Splits the string EXPR into a list of strings and returns the list in list context, or the size of the list in scalar context.
You're calling the function in scalar context (because you're assigning the result of the call to a scalar variable) so you're getting the size of the list.
If you want to get the list, then you need to store it either in a list of variables:
my ($forename, $middlename, $surname) = split ' ', $name;
Or (more usually) in an array:
my @name_parts = split ' ', $name;
But actually, you say that you want an array reference. You can do that by calling split()
inside an anonymous array constructor ([ ... ]
) and assigning the result of that call to a scalar variable.
my $name_parts = [ split ' ', $name ];
Upvotes: 2
Reputation: 39158
That's called context.
The partial expression split ' ', $name
evaluates to a list. The partial expression $array = LIST
coerces the list to a scalar value, namely counting the number of elements in the list. That's the default behaviour of lists in scalar context.
You should write @array = LIST
instead, using an array variable, not a scalar variable, in order to preserve the list values.
Upvotes: 2