Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Hi all,

$ref is a reference to a hash. Hash contains a component called a 
which is an array. I would like to iterate all elements of said array.


I would like to do something along the lines of:
foreach my $elem @%$ref{a}
{
   print $elem\n;
}

Which, I think it clear, does not work. I also tried:
foreach my $elem @(%$ref){a}
{
   print $elem\n;
}

which complains about:
Bareword a not allowed while strict subs in use

and:
foreach my $elem @(%$ref){'a'}
{
   print $elem\n;
}

which complains about:
Global symbol $elem requires explicit package name at line 3 (i.e. - 
inside the for).


Any help would be appreciated.

Thanks,
Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com


--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Noam Rathaus
Hi Shachar,

First you can always use Data::Dumper:
use Data::Dumper;
print Dumper($ref);

To make sure that the data is stored correctly.

In regard to your question:

my $ref;
my %hash = %{$ref};
foreach my $ptrelem (keys %hash) {
 my @array = @{$ptrelem};
 foreach my $item (@array) {
  print $item;
 }
}

From my experience with perl, doing shortcuts don't work too well, and
doing it the long way, helps with debugging and finding out what the
problem is.

2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Hi all,

 $ref is a reference to a hash. Hash contains a component called a which is
 an array. I would like to iterate all elements of said array.

 I would like to do something along the lines of:
 foreach my $elem @%$ref{a}
 {
     print $elem\n;
 }

 Which, I think it clear, does not work. I also tried:
 foreach my $elem @(%$ref){a}
 {
     print $elem\n;
 }

 which complains about:
 Bareword a not allowed while strict subs in use

 and:
 foreach my $elem @(%$ref){'a'}
 {
     print $elem\n;
 }

 which complains about:
 Global symbol $elem requires explicit package name at line 3 (i.e. -
 inside the for).

 Any help would be appreciated.

 Thanks,
 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il



___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Noam Rathaus
Sorry a mistake...

foreach my $ptrelem (keys %hash) {

Should be

foreach my $key (keys %hash) {
 my $ptritem = %hash-{$key};

On Sat, Oct 24, 2009 at 7:55 PM, Noam Rathaus no...@beyondsecurity.com wrote:
 Hi Shachar,

 First you can always use Data::Dumper:
 use Data::Dumper;
 print Dumper($ref);

 To make sure that the data is stored correctly.

 In regard to your question:

 my $ref;
 my %hash = %{$ref};
 foreach my $ptrelem (keys %hash) {
  my @array = @{$ptrelem};
  foreach my $item (@array) {
  print $item;
  }
 }

 From my experience with perl, doing shortcuts don't work too well, and
 doing it the long way, helps with debugging and finding out what the
 problem is.

 2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Hi all,

 $ref is a reference to a hash. Hash contains a component called a which is
 an array. I would like to iterate all elements of said array.

 I would like to do something along the lines of:
 foreach my $elem @%$ref{a}
 {
     print $elem\n;
 }

 Which, I think it clear, does not work. I also tried:
 foreach my $elem @(%$ref){a}
 {
     print $elem\n;
 }

 which complains about:
 Bareword a not allowed while strict subs in use

 and:
 foreach my $elem @(%$ref){'a'}
 {
     print $elem\n;
 }

 which complains about:
 Global symbol $elem requires explicit package name at line 3 (i.e. -
 inside the for).

 Any help would be appreciated.

 Thanks,
 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il




___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Dov Grobgeld
Noam beat me to it, but here's perl solution without additional variables:

#!/usr/bin/perl

%hash = (a=['moo','goo','woo'],
 foo=3,
 baz=5);

$ref = \%hash;
foreach my $elem (@{$ref-{a}})
{
print $elem\n;
}

Regards,
Dov

2009/10/24 Shachar Shemesh shac...@shemesh.biz

  Hi all,

 $ref is a reference to a hash. Hash contains a component called a which
 is an array. I would like to iterate all elements of said array.

 I would like to do something along the lines of:
 foreach my $elem @%$ref{a}
 {
 print $elem\n;
 }

 Which, I think it clear, does not work. I also tried:
 foreach my $elem @(%$ref){a}
 {
 print $elem\n;
 }

 which complains about:
 Bareword a not allowed while strict subs in use

 and:
 foreach my $elem @(%$ref){'a'}
 {
 print $elem\n;
 }

 which complains about:
 Global symbol $elem requires explicit package name at line 3 (i.e. -
 inside the for).

 Any help would be appreciated.

 Thanks,
 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.http://www.lingnu.com


 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.http://www.lingnu.com


 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Dov Grobgeld wrote:

Noam beat me to it, but here's perl solution without additional variables:

#!/usr/bin/perl

%hash = (a=['moo','goo','woo'],
 foo=3,
 baz=5);

$ref = \%hash;
foreach my $elem (@{$ref-{a}})

Hi Dov,

Yes, it works. Now can you, please, explain to me why? What is the role 
of each bracket you used (and its location)?


Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Noam Rathaus
Shachar,

{ } in Perl are casting when they surround a value
And the second set of { } around the 'a' mean variable of Hash


2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Dov Grobgeld wrote:

 Noam beat me to it, but here's perl solution without additional variables:

 #!/usr/bin/perl

 %hash = (a=['moo','goo','woo'],
  foo=3,
  baz=5);

 $ref = \%hash;
 foreach my $elem (@{$ref-{a}})

 Hi Dov,

 Yes, it works. Now can you, please, explain to me why? What is the role of
 each bracket you used (and its location)?

 Shachar

 --
 Shachar Shemesh
 Lingnu Open Source Consulting Ltd.
 http://www.lingnu.com

 ___
 Linux-il mailing list
 Linux-il@cs.huji.ac.il
 http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il



___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Noam Rathaus wrote:

Shachar,

{ } in Perl are casting when they surround a value
And the second set of { } around the 'a' mean variable of Hash


  

Grumble grumble grumble

Okay, I'm sorry for being difficult. I really couldn't find the answer 
in the Perl documentation.


I understand the second set of curly braces. I also, somewhat, 
understand that the - replaces the % (i.e. - reference dereferencing). 
What I'm not so clear is what the first set of curly braces do (what do 
you mean by casting - casting to what? How is that decided?). I'm also 
not clear on why the surrounding round brackets are needed. I understand 
they are so this will be a list context, but I don't understand why it's 
needed once I put a @ to dereference the array.


Thanks,
Shachar



foreach my $elem (@{$ref-{a}})



--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Gabor Szabo
2009/10/24 Shachar Shemesh shac...@shemesh.biz:
 Noam Rathaus wrote:

 Shachar,

 { } in Perl are casting when they surround a value
 And the second set of { } around the 'a' mean variable of Hash




 Grumble grumble grumble

not surprised as this is one of the funky places of Perl 5.


 Okay, I'm sorry for being difficult. I really couldn't find the answer in
 the Perl documentation.

 I understand the second set of curly braces. I also, somewhat, understand
 that the - replaces the % (i.e. - reference dereferencing). What I'm not so
 clear is what the first set of curly braces do (what do you mean by
 casting - casting to what? How is that decided?). I'm also not clear on
 why the surrounding round brackets are needed. I understand they are so this
 will be a list context, but I don't understand why it's needed once I put a
 @ to dereference the array.

 Thanks,
 Shachar

 foreach my $elem (@{$ref-{a}})


err, I don't think that casting is the right word to use here. What
{} does here is
disambiguates the expression. Here is a table

$x - scalar
@x - array
%x - hash

$ra = \...@x  reference to array

sticking @ infront of the reference to an array dereferences it
@x is the same as @$ra   (you could also write and @{$ra} but it is
not necessary)
$x[1]  is the same as  $$ra[1]   (element of array, replace @ by $ and
attach the index)
 but it is ugly so it can also be written as $ra-[1]


$rh = \%x  reference to hash

sticking % infront of the reference to a hash dereferences it
%x is the same as %$rh  (could be also written as %{$rh} but it is not
necessary)
$x{foo} is the same as $$fh{foo} which is the same as $fh-{foo}

Now what if you have two dimensions: first dimension is a hash second
dimension is an array.

%h  is a hash
@something = ('foo');
$h{a} = \...@something;

Which means
print $h{a}[0];# 'foo';


$ref = \%h;   reference to hash

%h is the same as %$ref
$h{a}  is the same as $$ref{a}  or as $ref-{a} which is the reference
to the array: \...@something

sticking @ in front of it would dereference the array which would yield

@$h{a}   or  @$$ref{a}  @$ref-{a}   which is the same as @something

but it is not clear what does either of these mean
(looking at the last one, @$ref could mean $ref is a reference to an array and
that you are dereferencing @$ref  and the resulting thingy is a hash ref)

So we wrap the reference in a curly brace to make it clear that is a
single variable:

@{$h{a}}   or  @{$$ref{a}}  @{$ref-{a}}  which is the same as @{something}

so the {} is basically around the name' of the variable.



Hope this helps
   Gabor

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Shachar Shemesh

Gabor Szabo wrote:


err, I don't think that casting is the right word to use here. What
{} does here is
disambiguates the expression.

Let me try to summarize what I understood from your excellent explanation:

Putting a modifier in front of a reference dereference it to the right 
type ($ for scalar etc.). Alternatively, putting a '-' (which is a 
unary operator, not a binary one) also dereferences it, no matter what 
it is pointing to.(at least for array and hash), so long as there is 
some reference to its content on the operator's right (the same as it is 
implemented in C++, only more confusing).


The curly braces act as a scoping operator, making the $/@/% relation to 
parts of the expression unique.


All that is left is understanding why the round braces around the whole 
expression.


Many thanks (the explanation was very useful)
Shachar

--
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il


Re: Perl question: array member of referenced hash

2009-10-24 Thread Gabor Szabo
On Sat, Oct 24, 2009 at 11:25 PM, Shachar Shemesh shac...@shemesh.biz wrote:
 Let me try to summarize what I understood from your excellent explanation:

if that works for you :-)



 All that is left is understanding why the round braces around the whole
 expression.

Oh, the syntax of foreach has those parentheses

foreach my $iterator (things to iterate over) {
}

Where the things to iterate over can be a simple list of values, an array or
anything that returns a list of values which of course can be any expression.

foreach my $iterator (1,2,3,4,5) {
}

or for the lazy ones

foreach my $iterator (1..5) {
}

or for the really lazy ones:

for my $iterator (1..5) {
}


or

foreach my $iterator (@names) {
}


foreach my $iterator (@$ref) {
}

foreach my $iterator (function_that_returns_thingies()) {
}


Gabor

http://szabgab.com/blog.html
Perl Training Israel http://www.pti.co.il/

___
Linux-il mailing list
Linux-il@cs.huji.ac.il
http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il