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

Discussion in 'Perl Misc' started by L. D. James, Apr 9, 2006.

1. ### L. D. JamesGuest

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.

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

use warnings;
use strict;
use DBI;
use CGI qwstandard);

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++;
}
---------------------------------------------------------
Code end:

-- L. James

--
------------------------------------
L. D. James

www.apollo3.com/~ljames

L. D. James, Apr 9, 2006

2. ### Matt GarrishGuest

"L. D. James" <> wrote in message
news:...
>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.
>
>
> Code begin:
> ---------------------------------------------------------
> #!/usr/local/bin/perl
>
> use warnings;
> use strict;
> use DBI;
> use CGI qwstandard);
>
> 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

Matt Garrish, Apr 9, 2006

3. ### L. D. JamesGuest

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

--
--------------------
L. D. James

www.apollo3.com/~ljames

L. D. James, Apr 9, 2006
4. ### Peter J. HolzerGuest

L. D. James wrote:
> 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,

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++;
> }

--
_ | Peter J. Holzer | LÃ¶schung von at.usenet.schmankerl?