In split, it is explained that split; defaults to split ' ', $_;.
\@numbers is not de-referencing but is creating a reference to the array @numbers. So @matrix will be an array of array references. Without it, the push would push all individual elements of @numbers onto @matrix, effectively creating a long one-dimensional list instead of a matrix.
| [reply] [Watch: Dir/Any] [d/l] [select] |
| [reply] [Watch: Dir/Any] [d/l] [select] |
RTFM - the use of perldoc -f split will provide the answer to your first question ... and indeed, to a degree, your second question...
push() pushes all its' args onto the array given as the first arg, so you need to dereference the numbers array iff the required end result is a n array of arrays i.e. without the de-ref, all you do is flatten out the list so numbers will end up containing a list of the elements of numbers.
In the cold light of day, I realise that some of the above response is, at the very least, misleading - instead of '...need to dereference...', I should have said '...create a reference to...'.
A user level that continues to overstate my experience :-))
| [reply] [Watch: Dir/Any] [d/l] [select] |
#!usr/bin/perl -w
use strict;
my @matrix;
while (<DATA>)
{
chomp;
my @numbers = split;
#default variable to split upon is $_
#default split is on spaces
next unless @numbers; #skip blank lines (no characters)
push (@matrix, \@numbers); #An array of array
}
foreach my $array_ref (@matrix)
{
print "@$array_ref\n";
}
=Output
5 5.1 62.1
6 6.1 6.7
=cut
__DATA__
5 5.1 62.1
6 6.1 6.7
| [reply] [Watch: Dir/Any] [d/l] |
You seem to be confused about vocabulary. \@numbers takes a reference to @numbers, which is the opposite of “dereferencing“.
It is needed because the natural representation for a matrix is an array of arrays. Since a perl array can only hold scalar values, arrays of arrays are actually arrays of references to arrays. PerlLoL goes into a lot more detail about this concept.
Without the backslash, all numbers would be squashed together inside @matrix without structure. The result would effectively be a list, not a matrix.
The question about split is easy to answer by looking at the doc. Without any argument, split splits $_ on whitespace. It is equivalent to split /\s+/, except that leading whitespace in $_ is ignored.
| [reply] [Watch: Dir/Any] [d/l] [select] |