Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with syntax getting array elements

Reply
Thread Tools

Problem with syntax getting array elements

 
 
Dave Saville
Guest
Posts: n/a
 
      06-06-2012
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
 
Reply With Quote
 
 
 
 
Tim Watts
Guest
Posts: n/a
 
      06-06-2012
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
 
Reply With Quote
 
 
 
 
Dave Saville
Guest
Posts: n/a
 
      06-06-2012
On Wed, 6 Jun 2012 09:01:26 UTC, Tim Watts <(E-Mail Removed)>
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
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      06-06-2012
>>>>> "DS" == Dave Saville <(E-Mail Removed)> writes:

DS> On Wed, 6 Jun 2012 09:01:26 UTC, Tim Watts <(E-Mail Removed)>
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


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      06-06-2012
"Dave Saville" <(E-Mail Removed)> writes:
> my %h;
> $h{a} = [1,2,3];
> print $h{a}[2]."\n";
> my ($a, $b) = $h{a}[1,2];


The reason why this doesn't work is because the expression inside the
subscript ([]) is evaluated in scalar context in this case,
consequently, it's a comma-operator expression whose value is 2. In
order to denote a slice, a @ is needed in here.

$h{a}[1,2]

is 'syntactic sugar' for

$h{a}->[1,2]

which is, in turn, syntactic sugar for

${$h{a}}[1,2]

but since you want an array slice, it should be
(as Tim Watts already posted)

@{$h{a}}[1,2]

{{$h{a}} being a block which returns a reference of 'a suitable type'
for this expression.

If being unsure how perl 'sees' a specific expression, the Deparse
module can sometimes be helpful. For your code above, the output (perl
-MO=Deparse) is

my %h;
$h{'a'} = [1, 2, 3];
print $h{'a'}[2] . "\n";
my($a, $b) = $h{'a'}['???', 2];
print "$a $b\n";
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting array index for non-zero elements Victor Reyes Ruby 5 04-30-2008 11:44 AM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
getting array elements sam Perl Misc 9 12-28-2004 10:51 PM
searching elements of an array within another array diffused Java 9 08-01-2004 10:09 AM
Find if elements of one array are present in the other and return a boolean array Shalini C++ 2 01-09-2004 06:13 PM



Advertisments