Need info

A

axl.rajesh

Hi,

I am new to this group. I have executed following simple perl script.

#! c:\perl\bin\perl
my $a = qw/1 2 3 4 6/;
print $a;

The out put I am getting is 6. can any one suggest me why this behavior
is shown.
I thought I will be getting size of list i.e. 5.

can any one me clarify me on this.

Thanks.
 
A

A. Sinan Unur

(e-mail address removed) wrote in @j52g2000cwj.googlegroups.com:
Subject: Need info

Please use meaningful subject lines.
I am new to this group.

Then, this is just the time to read the posting guidelines.
I have executed following simple perl script.

#! c:\perl\bin\perl
my $a = qw/1 2 3 4 6/;
print $a;

The out put I am getting is 6. can any one suggest me why this
behavior is shown.

This is a FAQ. You are expected to the read the FAQ list before you
post:

perldoc -q "What is the difference between a list and an array?"
can any one me clarify me on this.

Yes.

Sinan
 
V

Vipin Nair

The $a scalar takes the last value.
#! c:\perl\bin\perl
my @a = qw/1 2 3 4 6/;
print $#a +1;

will return 5



Rgds

Vipin
 
P

Paul Lalli

Hi,

I am new to this group. I have executed following simple perl script.

#! c:\perl\bin\perl
my $a = qw/1 2 3 4 6/;
print $a;

The out put I am getting is 6. can any one suggest me why this behavior
is shown.
I thought I will be getting size of list i.e. 5.

You are confusing lists with arrays. An array evaluated in scalar
context gives its size. A list in scalar context ... doesn't exist.
Instead, you're using the scalar version of the comma operator, which
evaluates and discards its left operand before returning its right
operand.

In the generic case:

$x = (foo(), bar(), baz());

both of foo() and bar() will both be called, but their return values
are discarded. baz() will then be called, and $x will receive its
return value.

perldoc -q "difference between a list and an array"

for more information.

Paul Lalli.
 
J

Joe Smith

my $a = qw/1 2 3 4 6/;
print $a;

The out put I am getting is 6. can any one suggest me why this behavior
is shown.
I thought I will be getting size of list i.e. 5.

Maybe this will help show where your misunderstanding lies.

cygwin% perl -le '$a=qw/1 2 3 4 xyzzy/; print $a'
xyzzy
cygwin% perl -le '$a=()=qw/1 2 3 4 x/; print $a'
5

In the second example, qw// is evaluated in list context.
In the first example, it isn't.

-Joe
 

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,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top