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. James

    L. D. James Guest

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

    -- L. James

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

    www.apollo3.com/~ljames
     
    L. D. James, Apr 9, 2006
    #1
    1. Advertising

  2. L. D. James

    Matt Garrish Guest

    "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.
    >
    > 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
     
    Matt Garrish, Apr 9, 2006
    #2
    1. Advertising

  3. L. D. James

    L. D. James Guest

    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
    #3
  4. 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,
    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++;
    > }


    --
    _ | Peter J. Holzer | Löschung von at.usenet.schmankerl?
    |_|_) | Sysadmin WSR/LUGA |
    | | | | Diskussion derzeit in at.usenet.gruppen
    __/ | http://www.hjp.at/ |
     
    Peter J. Holzer, Apr 9, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,719
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    436
    Holgerson
    Oct 26, 2007
  3. Terry Reedy
    Replies:
    0
    Views:
    587
    Terry Reedy
    Apr 2, 2009
  4. Robert Kern
    Replies:
    0
    Views:
    624
    Robert Kern
    Apr 2, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    419
    Kai-Uwe Bux
    Apr 11, 2010
Loading...

Share This Page