a simple question

R

Raghavendra Mahuli

Hi all,
I have a simple question in perl... I have an array -

@a = (10,20,30,40);

Since '$' gives the scalar of a array , i expect "print $a" to give the no.
of elements in the array...
But it dosent work.. However if i copy it to a scalar and then print it, it
works.. Can u pls explain why it is so?
Here is the program listing:

@a = (10,20,30,40);
$x= @a;
print $a;
print $x;

thanx in advance,
regards,
raghu
 
B

Brian McCauley

Raghavendra said:
Subject: a simple question

Please put the subect of your post in the Subject of your post.
Hi all,
I have a simple question in perl... I have an array -

@a = (10,20,30,40);

Since '$' gives the scalar of a array ,

No it doesn't. I'm not sure what you mean. Do you mean $a is the same
as scalar(@a) ? No, that's not true.
i expect "print $a" to give the no.
of elements in the array...

No, @a and $a are two completely separate variables.

I have to confess this is rather messy in Perl5 - apparently it'll all
be different in Perl6.

In Perl5 @a is the array called 'a' and $a is a separate scalar variable
also called 'a'. Confusingly $a[0] is the first element of @a and has
nothing to do with $a. $#a is the last subscript of @a.

This is all explained in some detail in the perldata manual.
 
V

vnick

$x=@a is the evaluation of an array (a list) in a scaler context which
gives by definition the number of array elements whereas this 'print
"$a"' prints the scalar var (no evaluation within " ") $a which has
nothing to do with @a.
On the contrary you can define $a and @a at the same time not affecting
each other.
To get the number of elements of an array use
$#a + 1 or
scalar(@a) or
print @a ."\n"; (but not: print "@a\n"; !!!)

Hope that helps.
 
R

Raghavendra Mahuli

Thanx for all the responses....

Brian McCauley said:
Raghavendra said:
Subject: a simple question

Please put the subect of your post in the Subject of your post.
Hi all,
I have a simple question in perl... I have an array -

@a = (10,20,30,40);

Since '$' gives the scalar of a array ,

No it doesn't. I'm not sure what you mean. Do you mean $a is the same
as scalar(@a) ? No, that's not true.
i expect "print $a" to give the no.
of elements in the array...

No, @a and $a are two completely separate variables.

I have to confess this is rather messy in Perl5 - apparently it'll all
be different in Perl6.

In Perl5 @a is the array called 'a' and $a is a separate scalar variable
also called 'a'. Confusingly $a[0] is the first element of @a and has
nothing to do with $a. $#a is the last subscript of @a.

This is all explained in some detail in the perldata manual.
 
G

George

Raghavendra said:
Hi all,
I have a simple question in perl... I have an array -

@a = (10,20,30,40);

Since '$' gives the scalar of a array , i expect "print $a" to give
the no. of elements in the array...
But it dosent work.. However if i copy it to a scalar and then print
it, it works.. Can u pls explain why it is so?
Here is the program listing:

@a = (10,20,30,40);
$x= @a;
print $a;
print $x;

thanx in advance,
regards,
raghu

In perl your best friend is

use strict;
use warnings;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top