How to get the number of Elements in a Matrix (Multi-Dimisional array)?

L

L. D. James

I would like to thank everyone in advance for taking the time to look
at my code below. I believe the term for the variable type I'm
trying to manipulate is called matrix. It's rather new to me, and
I'm appreciative of any input on the proper nomenclature.

I know that you can evaluate the number of elements in a normal "one
dimensional" @array by assigning it to a $string value. I'm trying
to figure out how to get the number of elements from my "multi
dimensional" array, and having problems in the process.

I believe the code below might shed light on where my confusion exists,
as it produces errors which might be obvious to someone a little more
experience.

Where I attempt to output the number of elements per day I get
"ARRAY(0x903a2a0)" instead of a number. Where I try to itemize the
list ("They are:") the output is blank.

Thanks in advance for any comments on this matter.

Code begin:
---------------------------------------------------------
#!/usr/local/bin/perl

use warnings;
use strict;
use DBI;
use CGI qw:)standard);

my @week;

# The week is categorized into days and items per day

$week[0][0] = "Process Agenda 1";
$week[0][1] = "Process Agenda 2";
$week[0][2] = "Process Agenda 3";
$week[1][0] = "Process Agenda 1";
$week[1][1] = "Process Agenda 2";
$week[2][0] = "Process Agenda 1";

my $numdays = @week;

print "So far there are a total of $numdays days scheduled.\n";

# Out put a list of each agenda item per day.

my $daycount = 0;
foreach my$thisday (@week)
{
# Number of elements in the @week[$daycount] row or matrix
my $itemsperday = $week[$daycount];
print "On day $daycount we have $itemsperday to do. They are:\n";
my $count = 0;
while ( $count < $daycount )
{
print "$week[$daycount][$count]\n";
$count++;
}
$daycount++;
}
 
M

Matt Garrish

L. D. James said:
I would like to thank everyone in advance for taking the time to look
at my code below. I believe the term for the variable type I'm
trying to manipulate is called matrix. It's rather new to me, and
I'm appreciative of any input on the proper nomenclature.

I know that you can evaluate the number of elements in a normal "one
dimensional" @array by assigning it to a $string value. I'm trying
to figure out how to get the number of elements from my "multi
dimensional" array, and having problems in the process.

I believe the code below might shed light on where my confusion exists,
as it produces errors which might be obvious to someone a little more
experience.

Where I attempt to output the number of elements per day I get
"ARRAY(0x903a2a0)" instead of a number. Where I try to itemize the
list ("They are:") the output is blank.

Thanks in advance for any comments on this matter.

Code begin:
---------------------------------------------------------
#!/usr/local/bin/perl

use warnings;
use strict;
use DBI;
use CGI qw:)standard);

my @week;

# The week is categorized into days and items per day

$week[0][0] = "Process Agenda 1";
$week[0][1] = "Process Agenda 2";
$week[0][2] = "Process Agenda 3";
$week[1][0] = "Process Agenda 1";
$week[1][1] = "Process Agenda 2";
$week[2][0] = "Process Agenda 1";

my $numdays = @week;

print "So far there are a total of $numdays days scheduled.\n";

# Out put a list of each agenda item per day.

my $daycount = 0;
foreach my$thisday (@week)
{
# Number of elements in the @week[$daycount] row or matrix
my $itemsperday = $week[$daycount];

@week is an array of arrays, so each value is a reference to an array:

my $itemsperday = @{$week[$daycount]};

Matt
 
L

L. D. James

Thanks, Matt. That's exactly what I needed.

By the way, I made a mistake and posted in comp.os.linux.misc,
when I thought I was reading this group. I'll be more careful in the
future. I didn't mean to cross post.

-- L. James
 
P

Peter J. Holzer

L. D. James said:
I would like to thank everyone in advance for taking the time to look
at my code below. I believe the term for the variable type I'm
trying to manipulate is called matrix. It's rather new to me, and
I'm appreciative of any input on the proper nomenclature.

I know that you can evaluate the number of elements in a normal "one
dimensional" @array by assigning it to a $string value.

More precisely, by evaluating it in a scalar context.
I'm trying to figure out how to get the number of elements from my
"multi dimensional" array, and having problems in the process.

I believe the code below might shed light on where my confusion
exists, as it produces errors which might be obvious to someone a
little more experience.

Where I attempt to output the number of elements per day I get
"ARRAY(0x903a2a0)" instead of a number.

This is because perl "multidimensional arrays" are not really
multidimensional arrays. They are arrays which contain references to
arrays. In
# Number of elements in the @week[$daycount] row or matrix
my $itemsperday = $week[$daycount];

You assign the reference to $itemsperday.
You could then access the elements with

$itemsperday->[0], $itemsperday->[1],

and so on.

If you just print (or interpolate into a string) an array reference,
perl replaces it with "ARRAY(address)".

If you want to get at the array through the reference, you have to
dereference it by prepending an @:

my $itemsperday = @{$week[$daycount]};
print "On day $daycount we have $itemsperday to do. They are:\n";
my $count = 0;
while ( $count < $daycount )
^^^^^^^^^
I think you meant $itemsperday here.
{
print "$week[$daycount][$count]\n";
$count++;
}
$daycount++;
}
 

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

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top