Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Convert array into list context (http://www.velocityreviews.com/forums/t913186-convert-array-into-list-context.html)

Ryan Chan 06-13-2010 08:42 AM

Convert array into list context
 
Consider the simple code.

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

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


How to print out 'c' instead of 3?


Alan Curry 06-13-2010 09:53 AM

Re: Convert array into list context
 
Ryan Chan <ryanchan404@gmail.com> wrote:
>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...

--
Alan Curry

Ryan Chan 06-13-2010 01:24 PM

Re: Convert array into list context
 
Hi,

On 6月13日, 下午5時53分, pac...@kosh.dhis.org (Alan Curry) wrote:
> 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.

sln@netherlands.com 06-13-2010 02:17 PM

Re: Convert array into list context
 
On Sun, 13 Jun 2010 01:42:54 -0700 (PDT), Ryan Chan <ryanchan404@gmail.com> wrote:

>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

Uri Guttman 06-13-2010 04:40 PM

Re: Convert array into list context
 
>>>>> "RC" == Ryan Chan <ryanchan404@gmail.com> writes:

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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Xho Jingleheimerschmidt 06-13-2010 07:20 PM

Re: Convert array into list context
 
Uri Guttman wrote:
>>>>>> "RC" == Ryan Chan <ryanchan404@gmail.com> writes:

>
> 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

Ilya Zakharevich 06-13-2010 11:45 PM

Re: Convert array into list context
 
On 2010-06-13, Ryan Chan <ryanchan404@gmail.com> wrote:
>> 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.


The only thing ambiguous is who is "we" in this sentence. Scalar vs
List context is on of the few parts of Perl which has pretty good
documentation.

Yours,
Ilya

Uri Guttman 06-14-2010 12:51 AM

Re: Convert array into list context
 
>>>>> "XJ" == Xho Jingleheimerschmidt <xhoster@gmail.com> writes:

XJ> Uri Guttman wrote:
>>>>>>> "RC" == Ryan Chan <ryanchan404@gmail.com> writes:

>>

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.


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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Xho Jingleheimerschmidt 06-14-2010 02:24 AM

Re: Convert array into list context
 
Uri Guttman wrote:
>>>>>> "XJ" == Xho Jingleheimerschmidt <xhoster@gmail.com> writes:

>
> XJ> Uri Guttman wrote:
> >>>>>>> "RC" == Ryan Chan <ryanchan404@gmail.com> writes:
> >>

> 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.

>
> 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

Uri Guttman 06-14-2010 03:03 AM

Re: Convert array into list context
 
>>>>> "XJ" == Xho Jingleheimerschmidt <xhoster@gmail.com> writes:

XJ> Uri Guttman wrote:
>>>>>>> "XJ" == Xho Jingleheimerschmidt <xhoster@gmail.com> writes:

>>

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.


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


>> and the OP's need is still silly.


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

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------


All times are GMT. The time now is 03:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.