@platforms = (sort keys %prj_platforms) || (DEFAULT);

A

A. Farber

Hi,

I must confess, that I don't fully understand the
difference between an array and a list and suspect
that the cause for my problem lies somewhere there...

Could someone please explain, why does the code below warns
"Useless use of sort in scalar context at ./test_case.pl"?

#!/usr/bin/perl -w
use constant DEFAULT => qw(ARMI ARM4 THUMB WINS WINSCW);
@platforms = (sort keys %prj_platforms) || (DEFAULT);

Thank you
Alex
 
P

Paul GABORIT

À (at) 4 Aug 2004 08:04:33 -0700,
Could someone please explain, why does the code below warns
"Useless use of sort in scalar context at ./test_case.pl"?

#!/usr/bin/perl -w
use constant DEFAULT => qw(ARMI ARM4 THUMB WINS WINSCW);
@platforms = (sort keys %prj_platforms) || (DEFAULT);


Use :

(@platforms = sort keys %prj_platforms) || (@platforms = DEFAULT);

or :

@platforms = sort keys %platforms or @platforms = DEFAULT;
 
P

Paul Lalli

Hi,

I must confess, that I don't fully understand the
difference between an array and a list

perldoc -q 'list and an array'

Basically, a list is a constant sequence of scalar values. An array is a
Perl datatype which contains a list. The array may be altered. A list
cannot. It is roughly analogous to a string 'Hello' and a variable $foo
which contains 'Hello'. You can say $foo .= ' World'. But you cannot
say 'Hello' .= ' World'.
and suspect
that the cause for my problem lies somewhere there...

Could someone please explain, why does the code below warns
"Useless use of sort in scalar context at ./test_case.pl"?

#!/usr/bin/perl -w
use constant DEFAULT => qw(ARMI ARM4 THUMB WINS WINSCW);
@platforms = (sort keys %prj_platforms) || (DEFAULT);

List vs Array is not really the problem, no. The || operator forces
scalar context on its arguments. An array in scalar context returns the
size of that array. A list used scalar context evaluates each of its
members, from left to right, and discards all but the last value, which it
returns. sort() returns a list. But even if it returned an array, your
code would still not do what you want.

Your problem is not list vs. array, but rather list context vs. array
context. The || operator is forcing scalar context, where you need list
context - so that one of your two lists can be assigned to @platforms.

(@platforms = sort keys %prj_platforms) || (@platforms = DEFAULT);

Now you have an expression on the left which is done in list context -
because of the assignment to @platforms. Once that assignment is
completed, @platforms is evaluated in scalar context, as the argument to
the || operator. Thus, it returns its new size. If that size is 0, the
short-circuit nature of || causes its second argument to be evaluated,
causing DEFAULT to be assigned to @platforms instead.

Hope this helps,
Paul Lalli
 
B

Ben Morrow

Quoth (e-mail address removed) (A. Farber):
Hi,

I must confess, that I don't fully understand the
difference between an array and a list and suspect
that the cause for my problem lies somewhere there...

Could someone please explain, why does the code below warns
"Useless use of sort in scalar context at ./test_case.pl"?

#!/usr/bin/perl -w
use constant DEFAULT => qw(ARMI ARM4 THUMB WINS WINSCW);
@platforms = (sort keys %prj_platforms) || (DEFAULT);

|| evaluates its left arg (and, in older perls, its right arg as well)
in scalar context: it has to, it needs to get a boolean out of it.

You need to do

my @platforms = sort keys %prj_platforms;
@platforms or @platforms = DEFAULT;

or something instead.

Ben
 
U

Uri Guttman

PG> Use :

PG> (@platforms = sort keys %prj_platforms) || (@platforms = DEFAULT);

PG> or :

PG> @platforms = sort keys %platforms or @platforms = DEFAULT;

those are both very poor coding IMO. hard to read and not clear what is
going on. i would break it up into 2 lines:

@platforms = sort keys %platforms ;
@platforms = DEFAULT unless @platforms ;

simple and clear

uri
 

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

Forum statistics

Threads
474,158
Messages
2,570,882
Members
47,414
Latest member
djangoframe

Latest Threads

Top