Loading ...
Sorry, an error occurred while loading the content.

24714Re: [PBML] Counting elements of Array

Expand Messages
  • Jenda Krynicky
    Dec 14, 2006
      From: Marcos Onisto <mlonisto@...>
      > How can I count the elements of one array? I'm using the push
      > function, but this is return message like below.
      > -bash-3.00$ ./checksrvs.pl
      > Useless use of push with no values at ./checksrvs.pl line 68.

      push() is used to add elements to arrays, not for getting their
      length or anything.

      There is one important and unusual concept in Perl that you simply
      have to learn and understand. Context. Expressions return different
      values based on the context! Please try to read up on context in your
      book or some docs.

      Eg. in case of arrays, if the list context the value of an array is
      the list of its elements, in scalar context it's the length of the

      my @a = (1,2,3);
      my @b = (4,5);

      my @c = (@a, @b); # list context
      my @copy = @a; # list context as well

      print "\@c = (@c)\n";

      my $len = @a; # scalar context
      print "\$len=$len\n";

      HTH, Jenda

      ===== Jenda@... === http://Jenda.Krynicky.cz =====
      When it comes to wine, women and song, wizards are allowed
      to get drunk and croon as much as they like.
      -- Terry Pratchett in Sourcery
    • Show all 7 messages in this topic