Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: what is @$varname in perl

by AppleFritter (Vicar)
on Jul 03, 2014 at 09:13 UTC ( [id://1092126]=note: print w/replies, xml ) Need Help??


in reply to what is @$varname in perl

$row is an array reference (sort of like a pointer in C); @$row is the actual (anonymous) array it references. The same goes for $sheet2 and @$sheet2.

What the code is essentially doing is to read each file line by line, split each line along commas, and store everythign in an array of arrays. So, for instance, the following file:

Name,Level,Writeups,XP Corion,Pope (28),8574,108561 Grandfather,Cardinal (24),6205,67751 Athanasius,Prior (17),773,11777 AppleFritter,Pilgrim (8),92,784

would be turned into this data structure:

$VAR1 = [ [ 'Corion', 'Pope (28)', '8574', '108561' ], [ 'Grandfather', 'Cardinal (24)', '6205', '67751' ], [ 'Athanasius', 'Prior (17)', '773', '11777' ], [ 'AppleFritter', 'Pilgrim (8)', '92', '784' ] ];

This is then used to sort the file on the first two columns.

Replies are listed 'Best First'.
Re^2: what is @$varname in perl
by sandy105 (Scribe) on Jul 03, 2014 at 10:16 UTC

    thank you but its still not clear, so @{$row} contains that data structure and @{$sheet2} is pushed $row the reference . i was reading into undefined and reference but still am confused ..refrences are declared like this i saw . could you take out time and give clarity to the undefined $row ,@$row , and what $row being pushed to @$sheet2 means

    my @array = (1, 2, 3, 'four'); my $reference = \@array;

      I'm not entirely sure what your question is, or where the confusion lies. Could you clarify?

      As I said above, references are sort of like pointers in C, if you're familiar with that. A data structure lives somewhere in memory; another variable, a scalar (which might in turn be an element of an array or hash, of course) holds a reference to it. A redirect, if you will: "the data you're looking for is found in another location. Walk this way!"

      Programming Perl has an entire chapter on references, so if you've got that book, I'd read that. (If you don't have that book, buy it, it's a must-have for any Perl programmer.) Also, see perlref for a less gentle introduction.

      Anyhow, to sum it up:

      • $row contains (colloquially: is) a reference to an array, i.e. the one that split returns.
      • That array does not have a name of its own; i.e., it's not @array or any such thing.
      • However, you can use the reference in $row -- "walk this way", as it were, following the directions it contains -- by dereferencing the reference. For an array reference, this is done by adding the array sigil, @: @$row.
      • The same applies to $sheet2 and @$sheet2.
      • Finally, @$sheet2 -- an array, remember! -- is used to store a list of references, each of which points to another array: one for each row.

      So you have something like this for each line:

      $row @$row +---+ +-----+-----+-----+-- | *-----> | ... | ... | ... | ... +---+ +-----+-----+-----+--

      and something like this for the entire file:

      $sheet2 @$sheet2 (each of the @$row's) +---+ +---+ | | +-----+-----+-----+-- | *-----> | *-----> | ... | ... | ... | ... +---+ | | +-----+-----+-----+-- +---+ | | +-----+-----+-----+-- | *-----> | ... | ... | ... | ... | | +-----+-----+-----+-- +---+ | . | .

      Does that make it clearer?

        thank you !!!! ..that cleared up my doubts.

        guess i bought the wrong book :learning perl -O'reilly.

        It does'nt have the topics which i have had problems with since starting with perl like map,threads,queue,reference :(

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1092126]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found