|
Posted by Tomasz Chmielewski on February 14, 2008, 7:23 am
Please log in for more thread options
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?
--
Tomasz Chmielewski
http://wpkg.org
|
|
Posted by Jürgen Exner on February 14, 2008, 8:20 am
Please log in for more thread options
>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
|
|
Posted by John W. Krahn on February 14, 2008, 8:26 am
Please log in for more thread options Tomasz Chmielewski wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
Posted by Jürgen Exner on February 14, 2008, 8:32 am
Please log in for more thread options >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
|
|
Posted by Bill Smith on February 15, 2008, 12:43 am
Please log in for more thread options
> 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?
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 = @[0]};
print "Number of screws: $number_screws Number of columns:
$number_columns\n";
__END__
use strict;
use warnings;
my $hardware = [
# type diam. length 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 = @[0]};
print "Number of screws: $number_screws Number of columns:
$number_columns\n";
__END__
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.
|
| Similar Threads | Posted | | @data = @items construct after a foreach - where can I find a literature reference to this? | February 25, 2005, 2:02 am |
| Grouping like items together | November 15, 2005, 2:11 pm |
| locking items in a collection | October 9, 2004, 12:52 am |
| Returning multiple items from a sub? | November 19, 2006, 3:02 am |
| Checking how many items have been captured in a pattern match | February 8, 2006, 5:21 am |
| XML::Simple folding broken for single items? | February 26, 2006, 7:21 pm |
| How to insert or append new items into a perl hash ? | January 15, 2007, 5:33 pm |
| I'm confused by reference to reference in my code | February 6, 2006, 6:03 pm |
| Passing a reference as a reference | August 16, 2007, 1:04 pm |
| Passing paramaters to the File::Find find() function | February 15, 2006, 5:39 am |
|