Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Good advice in the other responses. Some more words for you:

Your line of code to find the length:

$asize = @a;
works because an array evaluates to the number of elements it contains in a scalar context. Assigning to a scalar (as you do above) provides a scalar context to the right hand side (RHS). So the array is evaluated in this scalar context, giving the length, which the assignment then puts into your '$asize' variable.

Why did I bother spelling this out in a long-winded way? Because it is useful to understand what it going on. For example, I usually write the above as:

my $asize = scalar @a;
The 'scalar' keyword supplies a scalar context to its argument and gives the result of the evaluation. It is redundant in the above example (because we already have a scalar context from the assignment to the scalar).

So why do I write it like that? Because:

  • It is an explicit statement of what I am doing. I consider that more readable.
  • I can use the same syntax every place I need an array size. Look at the following:
    #!/usr/local/bin/perl -w use strict; my @array = qw( 1 2 3 4 5 ); my $asize = scalar @array; print "Array size 1 is ", $asize, "\n"; print "Array size 2 is ", @array, "\n"; print "Array size 3 is ", scalar @array, "\n";
    I *need* to use the scalar keyword here, because otherwise the 'print' command gives a list context to the array, which lists all the elements instead of giving the size.

    Since I sometimes get cut-and-paste happy, it is nice to have code which is explicit about what context it wants.

And I'll conclude this overly-pedantic missive to state that you should look into using 'use strict' and '#!... -w', to force you into good habits. (Heh...habits! Monks! How funny is that!? (Hrrrm...the first couple of million times were probably worth chuckling at, but thats about it))

In reply to Re: How to find size of array in array? by jbert
in thread How to find size of array in array? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2020-06-02 09:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you really want to know if there is extraterrestrial life?



      Results (16 votes). Check out past polls.

      Notices?