Convert array into list context

R

Ryan Chan

Consider the simple code.

============
my @a = (1,2,'c');

my $s = ('a' , @a);
print $s;
============


How to print out 'c' instead of 3?
 
A

Alan Curry

Ryan Chan said:
Consider the simple code.

============
my @a = (1,2,'c');

my $s = ('a' , @a);
print $s;
============


How to print out 'c' instead of 3?

Oh that wacky scalar-context comma operator! You were hoping it would
flatten the list and then act as if the elements of @a had been
separated by scalar-context comma operators, yielding the last element
of the flattened list? Too clever.

my $s = ('a' , @a)[-1];

In addition to actually working, the [-1] index also makes it obvious to
the later maintenance programmer that you wanted the last element of the
list. Explicit is better than implicit...
 
R

Ryan Chan

Hi,

my $s = ('a' , @a)[-1];


Yes, this work. Thanks.

But it is ambiguous that we don't know when the array is being
flattened, e.g. why not @a is being taken out.
 
S

sln

Consider the simple code.

============
my @a = (1,2,'c');

my $s = ('a' , @a);
print $s;
============


How to print out 'c' instead of 3?

@a is viewed in scalar context giving the # of elements.
You can expand the RHS a couple of ways.

With the Array slice notation on the list element -
$s = ('a' , @a[0..$#a]);

or index the list where [-1] gets last element of the expanded list -
$s = ('a' , @a)[-1]; # best way

or, a little of both -
$s = ('a' , @a[0..$#a])[-1]; # redundant

-sln
 
U

Uri Guttman

RC> Consider the simple code.
RC> ============
RC> my @a = (1,2,'c');

RC> my $s = ('a' , @a);

regardless, that is throwing away 'a'. if you had warnings enabled, that
would be flagged. so why are you doing that? there is no benefit to that
construct.

RC> print $s;
RC> ============

RC> How to print out 'c' instead of 3?

why are you trying to do it the wrong and hard way? arrays in scalar
context return their count.

uri
 
X

Xho Jingleheimerschmidt

Uri said:
RC> Consider the simple code.
RC> ============
RC> my @a = (1,2,'c');

RC> my $s = ('a' , @a);

regardless, that is throwing away 'a'. if you had warnings enabled, that
would be flagged. so why are you doing that? there is no benefit to that
construct.

Of course not. But it did what he wanted, then there would be.

Xho
 
U

Uri Guttman

RC> Consider the simple code.
RC> ============
RC> my @a = (1,2,'c');
XJ> Of course not. But it did what he wanted, then there would be.

me thinks you have a grammar mistake or i don't get your second sentence
at all.

and the OP's need is still silly.

uri
 
X

Xho Jingleheimerschmidt

Uri said:
RC> Consider the simple code.
RC> ============
RC> my @a = (1,2,'c');

XJ> Of course not. But it did what he wanted, then there would be.

me thinks you have a grammar mistake or i don't get your second sentence
at all.

It was supposed to be "But if it did".

and the OP's need is still silly.

Make $s be the last thing in @a, or 'a' if @a is empty. Of course, the
()[-1] construct handles that nicely. I'm not sure why that need is silly.

Xho
 
U

Uri Guttman

XJ> It was supposed to be "But if it did".


XJ> Make $s be the last thing in @a, or 'a' if @a is empty. Of course,
XJ> the ()[-1] construct handles that nicely. I'm not sure why that need
XJ> is silly.

the need for getting the last element of a list with a fixed element
that is lost is silly. yes it could be done with ()[-1] but not the way
it was coded nor did he say that was the goal (defaulting to 'a'). i
would code it this way which is clear that is the goal:

$x = $a[0] || 'a' ;

of course change that to -1 if you want the last element and use // if
you want it to work with 0 vs defined. this way makes it clear that 'a'
is a default and also it doesn't build up a wasteful list.

uri
 
J

Jürgen Exner

Ryan Chan said:
But it is ambiguous that we don't know when the array is being
flattened,

I may very well be mistaken but I tend to believe that flattening
happens only for function arguments and return values.

jue
 
U

Uri Guttman

JE> I may very well be mistaken but I tend to believe that flattening
JE> happens only for function arguments and return values.

it depends on the function (builtin or sub). some builtins have
prototypes where an array is passed whole (e.g. splice, shift) as scalar
args may follow which are kept separate and not slurped in. and you can
code subs with prototypes like that as well. i generally stay away from
sub protos but recognize they exist in builtins.

actually all list contexts will flatten an array (or multiple
arrays). most are provided by functions or return but you missed
assignment (and return's context is provided by what it is assigned
to).

also indexing provides list context:

$x = (1, 4, @a) ;
$x = (1, 4, @a)[$y] ;

the first line was the example the OP posted.

there are likely others but i can't think clearly now. been a long weekend.

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top