how to find how many items are in a reference?

Discussion in 'Perl Misc' started by Tomasz Chmielewski, Feb 14, 2008.

  1. 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
     
    Tomasz Chmielewski, Feb 14, 2008
    #1
    1. Advertising

  2. Tomasz Chmielewski <> wrote:
    >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ürgen Exner, Feb 14, 2008
    #2
    1. Advertising

  3. 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
     
    John W. Krahn, Feb 14, 2008
    #3
  4. "John W. Krahn" <> wrote:
    >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
     
    Jürgen Exner, Feb 14, 2008
    #4
  5. On 2008-02-15 05:43, Bill Smith <> wrote:
    > "Tomasz Chmielewski" <> wrote in message
    > news:fp1brl$jh4$...
    >> 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
     
    Peter J. Holzer, Feb 15, 2008
    #5
    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. Marco Ippolito
    Replies:
    0
    Views:
    2,602
    Marco Ippolito
    Oct 11, 2004
  2. =?Utf-8?B?U3R1?=

    session vars how many is to many ?

    =?Utf-8?B?U3R1?=, Mar 5, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    346
  3. dee
    Replies:
    2
    Views:
    406
  4. Gary Cohan

    Many to Many Collections

    Gary Cohan, Oct 31, 2003, in forum: Java
    Replies:
    4
    Views:
    450
    Chris Riesbeck
    Nov 3, 2003
  5. Eric Ford
    Replies:
    2
    Views:
    593
    Michael Meyer
    Jan 29, 2004
Loading...

Share This Page