CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
size of an array when use strict is involved... a simple question

 



perlFun
Novice

Jun 8, 2013, 12:23 PM

Post #1 of 6 (355 views)
size of an array when use strict is involved... a simple question Can't Post

The way I know to get the size of an array is say I have

my @foo=(1,2,3);
my $size=$foo;

now, if I'm using "strict" (as in a put 'use strict' at the top), perl yells at me for not having declared $foo. How else can I do this? I don't understand why it "works" when I omit strict but not if strict is involved? Does this mean the my $size=$foo approach to getting the size of a scalar is not sound? is there documentation i can read which will help me understand why or why not?


thanks so much


FishMonger
Veteran / Moderator

Jun 8, 2013, 12:34 PM

Post #2 of 6 (349 views)
Re: [perlFun] size of an array when use strict is involved... a simple question [In reply to] Can't Post

$foo is not the same var as @foo. One is a scalar, and the other is an array.


recruiter
User

Jun 8, 2013, 12:35 PM

Post #3 of 6 (347 views)
Re: [FishMonger] size of an array when use strict is involved... a simple question [In reply to] Can't Post


Code
use strict; 
use warnings;

my @foo = (1,2,3);

print scalar @foo;



(This post was edited by hwnd on Jun 8, 2013, 12:35 PM)


FishMonger
Veteran / Moderator

Jun 8, 2013, 12:38 PM

Post #4 of 6 (342 views)
Re: [hwnd] size of an array when use strict is involved... a simple question [In reply to] Can't Post


Code
my $size = @foo;



2teez
Novice

Jun 8, 2013, 1:09 PM

Post #5 of 6 (340 views)
Re: [perlFun] size of an array when use strict is involved... a simple question [In reply to] Can't Post


Quote
I don't understand why it "works"

YES, it does WORK, but print NO result, even without using

Quote
use strict;

.
WHY? because variable $foo is not defined at all.

However, if you use

Code
  
$size = @foo; ## NOTE the difference btw '$' and '@'
print $size,$/; ## prints 3


I suppose you wanted to use '@' instead of '$' before foo.


Laurent_R
Enthusiast / Moderator

Jun 8, 2013, 2:52 PM

Post #6 of 6 (329 views)
Re: [2teez] size of an array when use strict is involved... a simple question [In reply to] Can't Post

Your approach to obtain the size of an array is perfectly sound and valid, provided you do the following correction (already mentionned) :


Code
my @foo=(1,2,3); 
my $size=@foo;


(i.e. changing $foo to @foo). Or even, if you want to be more explicit on the context conversion you are really doing:


Code
my @foo=(1,2,3); 
my $size = scalar @foo;


This does not make a real difference, but this states more clearly that you are using an array in a scalar context to obtain the number of elements of this array.

And, by the way, this code:


Code
my $size = $foo;


used without the "use strict" pragma did not report any compilation error, but certainly did not work properly (because $foo and @foo are two different variables). So, using strict tells you about a bug in your program that would or might not have been seen otherwise, this is perfect example of why using struct is so useful (even though, in this specific case, you did not understand the real meaning of the compilation error being reported, which is not very surprising, you need a bit of experience to understand the link between what the compiler is telling you and what is really wrong in your code).

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives