how to find how many items are in a reference?

T

Tomasz Chmielewski

How can I find how many items are there in a reference?


Let's say we first make:

my $data = $db->selectall_arrayref("SELECT * FROM table");


Now, I can access any data with:

print $$hardware[X][Y];


How can I find the maximum element for X, Y?
 
J

Jürgen Exner

Tomasz Chmielewski said:
How can I find how many items are there in a reference?

There is only one item in a reference, the reference itself.
Let's say we first make:

my $data = $db->selectall_arrayref("SELECT * FROM table");

Now, I can access any data with:

print $$hardware[X][Y];

I have no idea where this "$hardware" is coming from all of a sudden, but I
assume you are talking about a reference to an array of references to
arrays.
How can I find the maximum element for X, Y?

To get the size of an array just use the array in scalar context. To get the
last element use the $#array notation.

To access the array from the reference please see
perldoc perlreftut
perldoc perlref, "Using references", rule#2

jue
 
J

John W. Krahn

Tomasz said:
How can I find how many items are there in a reference?


Let's say we first make:

my $data = $db->selectall_arrayref("SELECT * FROM table");


Now, I can access any data with:

print $$hardware[X][Y];

That is usually written as:

print $hardware->[X][Y];

How can I find the maximum element for X, Y?

scalar( @$hardware ), scalar( @{ $hardware->[X] } )




John
 
J

Jürgen Exner

John W. Krahn said:
Tomasz Chmielewski wrote:
print $hardware->[X][Y];
How can I find the maximum element for X, Y?

scalar( @$hardware ), scalar( @{ $hardware->[X] } )

Off-by-one error. He was asking for the maximum element (I suppose he
actually meant maximum index), not the size of the array.

jue
 
P

Peter J. Holzer

Tomasz Chmielewski said:
How can I find how many items are there in a reference?

Let's say we first make:

my $data = $db->selectall_arrayref("SELECT * FROM table");


Now, I can access any data with:

print $$hardware[X][Y];

Or somewhat more readable:

print $hardware->[X][Y];
How can I find the maximum element for X, Y?

I am not certain what your question means.
Let me simulate the structure returned from
db->selectall_arrayref
and demonstrate how to reference the data.

use strict;
use warnings;

my $hardware = [
# type diam(in). length(in) finish
[ "hex", "1/4", "1", "brass", ],
[ "slot", "1/8", "1/2", "chrome",],
[ "pan", "3/16", "3/4", "black", ],
];

local $, = " ";
for my $screw (@$hardware){
print @$screw, "\n";
}

my $number_screws = @$hardware;
my $number_columns = @{${$hardware}[0]};

This assumes that all the rows have the same number of elements. That's
always true for the return value of selectall_arrayref, but in the more
general case you might need to loop over the rows to determine the
maximum.
print "Number of screws: $number_screws Number of columns:
$number_columns\n";
__END__

[ second copy of the same script snipped - took me some time to see that
it differed only in formatting ]

Former C programmers do find it easier to read perl's alternate syntax
($array[X][Y]) for arrays. I find it much easier to write perl in its
normal syntax expecially when using features unique to perl such as the "@"
operator and array cross sections.

I don't know which syntax is "normal" for perl, but I do find
my $number_columns = @{${$hardware}[0]};

very hard to read. I very much prefer

my $number_columns = @{ $hardware->[0] };

here.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top