SPUG: scalar(@array)

Darren Stalder darren at u.washington.edu
Thu Apr 13 17:07:57 CDT 2000


Dan Ebert, in an immanent manifestation of deity, wrote:
>I though that it would give the number of elements in an array, but ...
>
>I have some code that creates a nested array:
>
>$record[101][0] = $var1a;
>$record[101][1] = $var1b
>[...]
>$record[322][0] = $var322a;
>$record[322][1] = $var322b;
>
>When I do a "scalar(@record)" it gives me 323 ... when I expected to get 221
>(322 minus 101).  It is OK, because my goal was to arrive at the value that
>'scalar(@record)' returned anyway, but it wasn't what I expected.

It does give you the number of elements in the array.  You have 101
elements from $record[0] to $record[100] that are set to undef.

Note that it gives you the number of elements once removed.  It forces
the array into a scalar context.  Any array in scalar context will yield
the number of elements in the array.

This means that the following give identical answers:
scalar(@record);
$num_recs = @record;

Darren
-- 
<torin at daft.com> <http://www.daft.com/~torin> <torin at debian.org> <torin at io.com>
Darren Stalder/2608 Second Ave, @282/Seattle, WA 98121-1212/USA/+1-800-921-4996
@ Sysadmin, webweaver, postmaster for hire. C/Perl/CGI/Pilot programmer/tutor @
@		     Make a little hot-tub in your soul.		      @

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address





More information about the spug-list mailing list