Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: how many elements in array?

by tcf03 (Deacon)
on Apr 18, 2005 at 12:34 UTC ( [id://448814]=note: print w/replies, xml ) Need Help??


in reply to how many elements in array?

or
#!/usr/bin/perl -w use strict; my @one=(1,2,3,4,5,6); print scalar(@one);
or
print $#one;
depends on what you want.
UPDATE
you should also check out the the basic datatypes, three node

Replies are listed 'Best First'.
Re^2: how many elements in array?
by Limbic~Region (Chancellor) on Apr 18, 2005 at 12:43 UTC
    tcf03,
    $#one and scalar @one do not produce the same thing. Using scalar @one gives the number of elements, which is what was asked for, while $#one gives is the index of the last element.

    Cheers - L~R

      Although, technically, they actually could produce the same thing, if one were to modify $[ to be 1

      Update: Just a caveat that this is not recommended in the slightest :) as the docs for index say:
      The return value is based at 0 (or whatever you've set the $[ variable to--but don't do that).
      --------------
      "But what of all those sweet words you spoke in private?"
      "Oh that's just what we call pillow talk, baby, that's all."
        RazorbladeBidet,
        While perldoc perlvar highly discourages the modification of $[, I recognize that in some very odd situation it may be the most natural way to solve a problem. In this case, it would be doing something very un-natural for the sake of being correct.

        A little bit of knowledge is a dangerous thing. If you are going to give someone a loaded gun, please give them the instruction manual too.

        Cheers - L~R

      Helpful in a loop as in:

      for ( 0 .. $#one ) { ...do stuff to each element... }

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        bradcathey,
        There are lots of ways in which $#array can be useful otherwise it wouldn't exist in the language. My point was more about correctness. For instance, if I am doing the following:
        my @ucase = grep { $_ eq uc() } @words; my $cnt = @ucase;
        I have the count of uppercase words, making all appropriate assumptions about @words. I would need to use $#ucase + 1* since it doesn't produce a count of elements otherwise. This was not clear from the post that I was replying to.

        Cheers - L~R

        * As RazorbladeBidet points out, modifying $[, is one highly discouraged way of accomplishing the same thing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://448814]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-28 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found