Simple Array question

Discussion in 'Perl Misc' started by elroyerni, Oct 11, 2007.

  1. elroyerni

    elroyerni Guest

    Hi -

    I'm trying to assign to a variable the number of elements in a array
    for example. I have array with data:

    my @array_data;

    I want to assign the number of elements in the array to a variable, i
    tried this and looked it up and can't find a solution :(

    Say array_data contains {0,1,2,3,4,5} i want to assign the number of
    elements to a variable, in this case "6" to a variable.

    i tried this and failed...


    my $total = `0 + @{$results}`;

    Any help?

    Thanks!
     
    elroyerni, Oct 11, 2007
    #1
    1. Advertising

  2. elroyerni

    Lars Eighner Guest

    In our last episode,
    <>,
    the lovely and talented elroyerni
    broadcast on comp.lang.perl.misc:

    > Hi -


    > I'm trying to assign to a variable the number of elements in a array
    > for example. I have array with data:


    > my @array_data;


    > I want to assign the number of elements in the array to a variable, i
    > tried this and looked it up and can't find a solution :(


    > Say array_data contains {0,1,2,3,4,5} i want to assign the number of
    > elements to a variable, in this case "6" to a variable.


    > i tried this and failed...



    > my $total = `0 + @{$results}`;


    > Any help?


    Wow, it's a little late to be doing your first week's homework.

    Why doesn't $#array_data + 1 work for you?

    --
    Lars Eighner <http://larseighner.com/> <http://myspace.com/larseighner>
    Countdown: 466 days to go.
    What do you do when you're debranded?
     
    Lars Eighner, Oct 11, 2007
    #2
    1. Advertising

  3. elroyerni

    Guest

    On Oct 11, 3:51 pm, Lars Eighner <> wrote:
    > Why doesn't $#array_data + 1 work for you?


    Many programmers would just use @array_data in a scalar context, or
    force it with scalar(@array_data). When an array is evaluated in
    scalar context it returns the number of elements it contains.

    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
     
    , Oct 12, 2007
    #3
  4. elroyerni

    Paul Lalli Guest

    On Oct 11, 6:51 pm, Lars Eighner <> wrote:
    > In our last episode,
    > <>,
    > the lovely and talented elroyerni
    > broadcast on comp.lang.perl.misc:
    >
    > > Hi -
    > > I'm trying to assign to a variable the number of elements in a array
    > > for example. I have array with data:
    > > my @array_data;
    > > I want to assign the number of elements in the array to a variable, i
    > > tried this and looked it up and can't find a solution :(
    > > Say array_data contains {0,1,2,3,4,5} i want to assign the number of
    > > elements to a variable, in this case "6" to a variable.
    > > i tried this and failed...
    > > my $total = `0 + @{$results}`;
    > > Any help?

    >
    > Wow, it's a little late to be doing your first week's homework.
    >
    > Why doesn't $#array_data + 1 work for you?


    A little late for you to have to be told you shouldn't use that.
    Somewhere, someway, some schmuck will edit your code and change the $
    [ variable, and all of a sudden, $#foo will no longer be "one less
    than the size". Because $#foo is not intended to be "one less than
    the size". It actually is "the last index of".

    Use the array in scalar context. That is defined to be the size.

    Paul Lalli
     
    Paul Lalli, Oct 12, 2007
    #4
  5. elroyerni

    Paul Lalli Guest

    On Oct 11, 6:45 pm, elroyerni <> wrote:
    > I'm trying to assign to a variable the number of elements in a
    > array for example. I have array with data:
    >
    > my @array_data;
    >
    > I want to assign the number of elements in the array to a
    > variable


    my $size = @array_data;

    > i tried this and looked it up and can't find a solution :(


    perldoc perldata:
    If you evaluate an array in scalar context, it returns the
    length of the array. (Note that this is not true of lists,
    which return the last value, like the C comma operator, nor
    of built-in functions, which return whatever they feel like
    returning.)


    > Say array_data contains {0,1,2,3,4,5} i want to assign the number
    > of elements to a variable, in this case "6" to a variable.
    >
    > i tried this and failed...
    >
    > my $total = `0 + @{$results}`;


    There are a number of things wrong with that expression. For one,
    what the heck is $results? You've only mentioned the array
    @array_data. You're using $results there as though it's an array
    reference. Is it? How is it being created.

    For two, you're taking the result of that 0 + @{$results} expression,
    and executing it as an external program. Then you're taking the
    output of that program and assigning it to $total. That's what the
    `...` quotes do.


    Paul Lalli
     
    Paul Lalli, Oct 12, 2007
    #5
  6. elroyerni <> wrote in news:1192142726.763354.235610
    @e34g2000pro.googlegroups.com:

    > I'm trying to assign to a variable the number of elements in a array
    > for example. I have array with data:
    >
    > my @array_data;
    >
    > I want to assign the number of elements in the array to a variable, i
    > tried this and looked it up and can't find a solution :(
    >
    > Say array_data contains {0,1,2,3,4,5} i want to assign the number of
    > elements to a variable, in this case "6" to a variable.


    my @array_data = (0 .. 5);
    my $total = @array_data;

    print "Total = $total\n";

    >
    > i tried this and failed...
    >
    >
    > my $total = `0 + @{$results}`;


    Read perldoc -f system to find out what backticks actually do (or
    consult perldoc perlop for the qx operator).

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)
    clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>
     
    A. Sinan Unur, Oct 12, 2007
    #6
  7. On Thu, 11 Oct 2007 22:45:26 -0000, elroyerni <>
    wrote:

    >Say array_data contains {0,1,2,3,4,5} i want to assign the number of
    >elements to a variable, in this case "6" to a variable.
    >
    >i tried this and failed...
    >
    >
    >my $total = `0 + @{$results}`;


    And were is array_data now? What is it anyway? Do you mean @array_data
    perhaps? However what you wrote would work... if

    (i) you were not having a shell execute it;
    (ii) $results is a arrayref;

    and last,

    (iii) the C<0 +> bit is not necessary at all.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 12, 2007
    #7
    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. Replies:
    0
    Views:
    575
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    678
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    856
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    383
    Roland Hall
    Jan 26, 2004
  5. Peter Bailey

    simple, simple array question

    Peter Bailey, Apr 8, 2008, in forum: Ruby
    Replies:
    7
    Views:
    221
    Peter Bailey
    Apr 8, 2008
Loading...

Share This Page