Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Explaining small Code

by madM (Beadle)
on Sep 20, 2013 at 14:13 UTC ( #1055038=perlquestion: print w/ replies, xml ) Need Help??
madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi! i just wanted to know what happens when you use the split operator without nothing and why it´s needed to dereference \@numbers(this code reads a file with a matrix and builds it in the @matrix array) Thanks!
while(<$fastd> ) { chomp; my @numbers = split; push @matrix, \@numbers; }

Comment on Explaining small Code
Download Code
Re: Explaining small Code
by Bloodnok (Vicar) on Sep 20, 2013 at 14:24 UTC
    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 :-))
Re: Explaining small Code
by kcott (Abbot) on Sep 20, 2013 at 14:25 UTC
Re: Explaining small Code
by hdb (Prior) on Sep 20, 2013 at 14:25 UTC

    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.

Re: Explaining small Code
by Marshall (Prior) on Sep 21, 2013 at 03:52 UTC
    #!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
Re: Explaining small Code
by Grimy (Pilgrim) on Sep 21, 2013 at 18:26 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055038]
Approved by hdb
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-12-22 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (112 votes), past polls