Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Printing number of elements in an array

Reply
Thread Tools

Printing number of elements in an array

 
 
Derek Basch
Guest
Posts: n/a
 
      04-17-2006
I just started with Perl after programming in Python for many years and
I must say it is driving me friggin nuts. For instance why does the
first method of printing the number of elements in an array work but
the second prints nothing?

print $temp = @fields;

$temp = @fields;
print $temp

Thanks,
Derek Basch

 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      04-17-2006
"Derek Basch" <> writes:
> I just started with Perl after programming in Python for many years and
> I must say it is driving me friggin nuts. For instance why does the
> first method of printing the number of elements in an array work but
> the second prints nothing?


Assuming you put

my @fields = qw/one two three/;

> print $temp = @fields;
>
> $temp = @fields;
> print $temp


This works fine. Both print statements output '3'. Of course, you
didn't put a newline between them, so you'll see '33' on your screen,
but that's a technicality of output.

-=Eric
 
Reply With Quote
 
 
 
 
Derek Basch
Guest
Posts: n/a
 
      04-17-2006
Ahhh. Found it. I had not put a line terminator after:

print $temp

It should have been:

print $temp;

Now I get an integer either way.

Thanks,
Derek

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      04-17-2006
Marc Dashevsky <> wrote:

> use warnings;
> use strict;
>
> my @fields = (0..;
> my $temp;
>
> print $temp = @fields, "\n";
> $temp = @fields;
> print $temp, "\n";


how about print scalar( @fields), "\n"; ?

--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
 
Reply With Quote
 
Derek Basch
Guest
Posts: n/a
 
      04-17-2006
Thanks for the replies everyone!

Derek Basch

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      04-18-2006

Derek Basch wrote:
> Ahhh. Found it. I had not put a line terminator after:
>
> print $temp
>
> It should have been:
>
> print $temp;
>
> Now I get an integer either way.


There is no need to put a semicolon after a Perl statement. It's not a
terminator, it's a separator. Was this perhaps not the last line in
your code?

Note: you should get into the habit of putting "use strict'" and "use
warnings" at the top of even the simplest script. Doing so would
probably have caused Perl to higlight your error. In this case it
would probably have got upset that you were trying to use the string
"3" in a context where a file handle was exepected and said...

Can't use string ("3") as a symbol ref while "strict refs" in use

Without "use strict" Perl will silently convert the string "3" into a
reference to a filehandle called 3 and you'd get a warning...

print() on unopened filehandle 3

Without "use warnings" either Perl will silently discard output sent to
an unopened file handle.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57