Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: what is @$varname in perl

by perlfan (Vicar)
on Jul 03, 2014 at 11:34 UTC ( [id://1092156]=note: print w/replies, xml ) Need Help??


in reply to what is @$varname in perl

It seems like you're writing your arrays as array references just so you can avoid creating a reference out of a non-reference.

You have:

foreach $line (@lines){ chomp $line; $count++; next unless $count; # skip header in csv my $row; @$row = split(/,/, $line ); push @$sheet2 , $row; }
But if you ask me, it'd be tons more clear if you did this:
my @sheet2 = () # .... foreach $line (@lines){ chomp $line; $count++; next unless $count; # skip header in csv my @row = (); @row = split(/,/, $line ); push @sheet2 , \@row; }
Another example is:
@$sheet2 = ();
Should be:
@sheet2 = ();
or
$sheet2 = [];
It seems to me you're conflating scalars, arrays, and references. Cleaning up the code and being consistent will go a long way in dispelling your confusion.

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

    i read up on reference and understand a scalar is assigned as reference :

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

    i modified using your suggestion as below

    foreach $line (@lines){ chomp $line; $count++; next unless $count; my @row = (); @row = split(/,/, $line ); push @sheet2 , \@row; } foreach my $row ( sort {$a->[0] cmp $b->[0] || $a->[1] cmp $b- +>[1]} @sheet2 ) #sorting based on date ,then stockcode { chomp $row; print hanw join (',', @$row ),"\n"; }

    then why assign @sheet2 as reference

      this :

      ... my $row; @$row = split(/,/, $line ); push @$sheet2 , $row; ...
      OR
      ... my @row = (); @row = split(/,/, $line ); push @sheet2 , \@row; ...
      Could be written like this in one line instead of three
      ... push @sheet2, [ split(/,/, $line ) ]; ...
      And there is no need for the array variable row at all.
      You could use Data::Dumper to view your variable @sheet2 to confirm.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me
      push @sheet2 , \@row;
      You're pushing a reference to @row as an item into @sheet2. In order to create complex data structures (array of arrays, array of hashes, etc) you need to assign references.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found