Re: Question about dereferencing

[email protected] ("Jenda Krynicky")
Newsgroups perl.beginners
Message-ID <[email protected]>
From:           	Christopher Yee Mon <[email protected]>
> I have a bit of Perl homework where we got a code sample. I can see what
> the code sample is doing except for two little bits
> 
> There's a part that has $row->[$i] and a part that has @$row . What do
> these two parts mean?

$row->[$i] is the $i-th element of the array referenced by $row and 
@$row is the array referenced by $row.
Another way to write the first thing would be ${$row}[$i].
 
> The code sample turns csv into fixed width so it reads each line of the
> file into an array @rows. Then in a for loop
> 
> for $row (@rows) {

That should be

   for my $row (@rows) {

You should put
   use strict;
on top of your scripts and declare all variables. And you should 
declare them for the smallest scope possible. In the case of $row, 
just for the loop.

> There's another for loop
> 
> for $row (@rows) {
> ~  for ($i = 0; $i < @$row; i++) {
> ~    $w = length $row->[$i]
> ~    ...
> 
> I'm assuming that the $row is itself another array

It's a reference to one

> or can be treated as one and @$row can give you a number of the 
> elements in it 

No, the @$row is the array referenced by $row, but an array, in 
scalar context, returns its length. So just like

 my $len = @array;

sets $len to the number of elements in @array,

 my $len = @$arrayref;

sets it to the number of elements in the array referenced by 
$arrayref. The concept of context takes some getting used to, but 
it's one of the best inventions of Perl.

> and $row->[$i]
> is some sort of deferencing mechanism. 

Right.

> The perlop page seemed a bit
> cryptic to me as to what the arrow operator does exactly and I could
> search for the use of @ in that context.

See perlref and perldsc

HTH, Jenda
===== [email protected] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.