[32441] in Perl-Users-Digest
Perl-Users Digest, Issue: 3708 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 6 09:09:18 2012
Date: Wed, 6 Jun 2012 06:09:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Wed, 6 Jun 2012 Volume: 11 Number: 3708
Today's topics:
Problem with syntax getting array elements <dave@invalid.invalid>
Re: Problem with syntax getting array elements <tw+usenet@dionic.net>
Re: Problem with syntax getting array elements <dave@invalid.invalid>
Re: Problem with syntax getting array elements <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 6 Jun 2012 08:32:18 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Problem with syntax getting array elements
Message-Id: <fV45K0OBJxbE-pn2-orLxwxEhxbGX@localhost>
Given
my @x = qw(a b c);
print "$x[2]\n";
($a, $b) = @x[1,2];
print "$a $b\n";
Gives me
c
b c
But
my %h;
$h{a} = [1,2,3];
print $h{a}[2]."\n";
my ($a, $b) = $h{a}[1,2];
print "$a $b\n";
Gives
3
Use of uninitialized value $b in concatenation (.) or string
3
I have tried all sorts of brackets braces etc. in various combinations
but I cannot get the anonymous array contained in the hash to work
like the first example. What silly thing am I missing?
TIA
--
Regards
Dave Saville
------------------------------
Date: Wed, 06 Jun 2012 10:01:26 +0100
From: Tim Watts <tw+usenet@dionic.net>
Subject: Re: Problem with syntax getting array elements
Message-Id: <6si3a9-9h3.ln1@squidward.local.dionic.net>
Dave Saville wrote:
> Given
>
> my @x = qw(a b c);
> print "$x[2]\n";
> ($a, $b) = @x[1,2];
> print "$a $b\n";
>
> Gives me
>
> c
> b c
>
> But
>
> my %h;
> $h{a} = [1,2,3];
> print $h{a}[2]."\n";
> my ($a, $b) = $h{a}[1,2];
> print "$a $b\n";
>
> Gives
>
> 3
> Use of uninitialized value $b in concatenation (.) or string
> 3
>
> I have tried all sorts of brackets braces etc. in various combinations
> but I cannot get the anonymous array contained in the hash to work
> like the first example. What silly thing am I missing?
>
> TIA
my %h;
$h{a} = [1,2,3];
print $h{a}->[2]."\n"; #[1]
my ($a, $b) = @{$h{a}}[1,2]; #[2]
print "$a $b\n";
[1] As the value of $h{a} is an array ref, not an array, I prefer to use the
deref operator -> for clarity.
[2] This defrefs the arrayref explicitly.
I am not a syntax master so I will not attempt to explain the ins and outs,
but the above works I think as you intended...
HTH
Tim
--
Tim Watts
------------------------------
Date: Wed, 6 Jun 2012 09:49:19 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Problem with syntax getting array elements
Message-Id: <fV45K0OBJxbE-pn2-lRojrOY1D8Qo@localhost>
On Wed, 6 Jun 2012 09:01:26 UTC, Tim Watts <tw+usenet@dionic.net>
wrote:
<snip>
> [1] As the value of $h{a} is an array ref, not an array, I prefer to use the
> deref operator -> for clarity.
>
> [2] This defrefs the arrayref explicitly.
>
> I am not a syntax master so I will not attempt to explain the ins and outs,
> but the above works I think as you intended...
Thank you very much. I had tried -> but not the same way. I knew it
was something like that but could not figure it out nor find any
helpful example.
--
Regards
Dave Saville
------------------------------
Date: Wed, 06 Jun 2012 05:50:06 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Problem with syntax getting array elements
Message-Id: <87mx4gg46p.fsf@stemsystems.com>
>>>>> "DS" == Dave Saville <dave@invalid.invalid> writes:
DS> On Wed, 6 Jun 2012 09:01:26 UTC, Tim Watts <tw+usenet@dionic.net>
DS> wrote:
DS> <snip>
>> [1] As the value of $h{a} is an array ref, not an array, I prefer to use the
>> deref operator -> for clarity.
>>
>> [2] This defrefs the arrayref explicitly.
>>
>> I am not a syntax master so I will not attempt to explain the ins and outs,
>> but the above works I think as you intended...
DS> Thank you very much. I had tried -> but not the same way. I knew it
DS> was something like that but could not figure it out nor find any
DS> helpful example.
you use -> to get only one element from a ref. it is clearer to use ->
for this purpose. when you want to slice (getting multiple elements) you
must do a full deref of the ref with @{} or %{} followed by the normal
slice index (e.g. [1, 2]).
uri
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3708
***************************************