arrays and hashes

P

Paul Lalli

Oobi said:
Could someone please brief me a little on arrays and hashes.

You should REALLY REALLY read a decent tutorial on Perl for these sorts
of questions. I recommend starting in this case with
http://perldoc.perl.org/perldata.html
For example:
What is the difference with () and {}?

Parentheses are (usually) used to construct a list, which is used to
populate an array or a hash:

my @array = (1, 2, 3, 4);
my %hash = (one => 1, two => 2, three => 3);

Curly-braces have a variety of functions in Perl. They are used to
access a specific value of a hash by enclosing a key:
$hash{four} = 4;
They are used to create an anonymous hash reference:
my $hash_ref = { one => 1, two => 2, three => 3 } ;

The are also used to create blocks and subroutines, as well as delimit
a variable name inside a double-quoted string. There are other uses as
well, but I don't think they apply to your question.
also:
do I need to enclose each element in an array with []?

No. Brackets, like braces, have multiple functions. They are used to
access a specific element from an array, by enclosing an index:

$array[2] = 20;

They are also used to create an anonymous array reference:
my $array_ref = [ 1, 2, 3, 4];
I am trying to construct an array of hashes.

You need to read a decent tutorial or two about this too.
http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perllol.html
http://perldoc.perl.org/perldsc.html
http://perldoc.perl.org/perlref.html
All is good but iterating over
the array there is only one element*S*...and iterating over the element
gives me the hash array*S*. ...
this is what i have
my @ary=
[
{
},
...
{
},
];

This creates an array that contains exactly one element. That one
element is a reference to an anonymous array. That anonymous array
contains multiple references to anonymous hashes.

Changing the outer [ ] to ( ) will be a good start, but you really need
to read those Perldocs to understand multi-level structures in Perl.

Paul Lalli
 
D

David Squire

Oobi said:
Could someone please brief me a little on arrays and hashes.

Perl is your friend. It comes with documentation included so that people
don't have to type this stuff again and again on Usenet. See:

perldoc perldata
perldoc perldsc

If you would prefer to read these online, they are available at
http://www.perl.com/pub/q/documentation
For example:
What is the difference with ()

allows you to construct a list

and {}?

allows you to construct a reference to an anonymous hash.
also:
do I need to enclose each element in an array with []?
No.

I am trying to construct an array of hashes.

There is a whole section on this topic in perldoc perldsc

Here is an example:

----

#!/usr/bin/perl

use strict;
use warnings;

my @array_of_hashes = ( # start defining a list, the contents of the array
{ # start the first element of the list, which is an anonymous hashref
'cat' => 'mammal',
'frog' => 'amphibian',
'snake' => 'reptile',
},
{ # start another list element
'cup' => 'crockery',
'knife' => 'cutlery',
},
);

foreach my $array_element (@array_of_hashes) {
print "###################################\n";
foreach my $key ( keys %{$array_element} ) { # notice we dereference
the element
print "$key: $$array_element{$key}\n";
}
}

----

Output:

###################################
cat: mammal
snake: reptile
frog: amphibian
###################################
knife: cutlery
cup: crockery


DS
 
O

Oobi Van Doobi

Could someone please brief me a little on arrays and hashes.
For example:
What is the difference with () and {}?
also:
do I need to enclose each element in an array with []?

I am trying to construct an array of hashes. All is good but iterating over
the array there is only one element*S*...and iterating over the element
gives me the hash array*S*. ...
this is what i have
my @ary=
[
{
},
...
{
},
];
Thank you,
any advice much appreciated
 
O

Oobi Van Doobi

Paul said:
Oobi said:
Could someone please brief me a little on arrays and hashes.

You should REALLY REALLY read a decent tutorial on Perl for these sorts
of questions. I recommend starting in this case with
http://perldoc.perl.org/perldata.html
For example:
What is the difference with () and {}?

Parentheses are (usually) used to construct a list, which is used to
populate an array or a hash:

my @array = (1, 2, 3, 4);
my %hash = (one => 1, two => 2, three => 3);

Curly-braces have a variety of functions in Perl. They are used to
access a specific value of a hash by enclosing a key:
$hash{four} = 4;
They are used to create an anonymous hash reference:
my $hash_ref = { one => 1, two => 2, three => 3 } ;

The are also used to create blocks and subroutines, as well as delimit
a variable name inside a double-quoted string. There are other uses as
well, but I don't think they apply to your question.
also:
do I need to enclose each element in an array with []?

No. Brackets, like braces, have multiple functions. They are used to
access a specific element from an array, by enclosing an index:

$array[2] = 20;

They are also used to create an anonymous array reference:
my $array_ref = [ 1, 2, 3, 4];
I am trying to construct an array of hashes.

You need to read a decent tutorial or two about this too.
http://perldoc.perl.org/perlreftut.html
http://perldoc.perl.org/perllol.html
http://perldoc.perl.org/perldsc.html
http://perldoc.perl.org/perlref.html
All is good but iterating over
the array there is only one element*S*...and iterating over the element
gives me the hash array*S*. ...
this is what i have
my @ary=
[
{
},
...
{
},
];

This creates an array that contains exactly one element. That one
element is a reference to an anonymous array. That anonymous array
contains multiple references to anonymous hashes.

Changing the outer [ ] to ( ) will be a good start, but you really need
to read those Perldocs to understand multi-level structures in Perl.

Paul Lalli
oh, !!thank's!!
 
P

Paul Lalli

Abigail said:
David Squire ([email protected]) wrote on MMMMDCCLXIII
September MCMXCIII in <URL:;;
;; > What is the difference with ()
;;
;; allows you to construct a list

Eh, not really. Context makes lists - the only place where () is significant
in creating a list is on the left hand side of the x.

Hrm. How does that jive with things such as:

my $x = @foo;
vs
my ($x) = @foo;

The way I understand it, in the first, @foo is evaluated in scalar
context, because we're assigning to a scalar value, and so $x gets the
size of @foo. In the second, @foo is evaluated in list context,
because we're assigning to a list, and so $x gets the first value of
@foo. So didn't the parentheses change "$x" from "a scalar value" to
"a list containing a single scalar value"?

Paul Lalli
 
A

anno4000

Paul Lalli said:
Hrm. How does that jive with things such as:

my $x = @foo;
vs
my ($x) = @foo;

The way I understand it, in the first, @foo is evaluated in scalar
context, because we're assigning to a scalar value, and so $x gets the
size of @foo. In the second, @foo is evaluated in list context,
because we're assigning to a list, and so $x gets the first value of
@foo. So didn't the parentheses change "$x" from "a scalar value" to
"a list containing a single scalar value"?

That is what Abigail said: () creates list context when used on the left
side of an assignment.

Anno
 
P

Paul Lalli

That is what Abigail said: () creates list context when used on the left
side of an assignment.


Okay.... I guess I didn't interpret "left hand side of the x" as "left
side of an assignmen".

Paul Lalli
 
D

David Squire

I am happy to be corrected on this, but the usage I was referring to, in
the context of the OP's original post, is something like this:

my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance flawed?



DS
 
B

Ben Morrow

Quoth David Squire said:
I am happy to be corrected on this, but the usage I was referring to, in
the context of the OP's original post, is something like this:

my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance flawed?

It's not flawed, it's just that it's not the () that create the list.
The comma creates the list; the () simply prevents it from being parsed
as

(my @new_array = 1), 2, 3;

as = binds tighter than , (in that direction).

Ben
 
A

anno4000

David Squire said:
I am happy to be corrected on this, but the usage I was referring to, in
the context of the OP's original post, is something like this:

my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance flawed?

It is correct, it is even necessary, but it has nothing to do with list
context.

"=" binds rather tightly in Perl, so leaving out the () is interpreted
as

( @new_array = 1), 2, 3;

(which is worth a warning).

The parens around ( 1, 2, 3) serve to change the precedence, just like
in "($x + $y)*$z".

Anno
 
C

Ch Lamprecht

David said:
I am happy to be corrected on this, but the usage I was referring to, in
the context of the OP's original post, is something like this:

my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance flawed?

(...) are for precedence in this case as demonstrated by the following piece of
code:

use strict;
use warnings;

my @array;
my $counter = 0;

@array = (getval(),getval(),getval());
@array = getval(),getval(),getval() ;

sub getval{
print "array is now @array\n";
print "getval returns $counter\n";
return $counter++;
}
print "array is now @array\n";



Christoph
 
D

David Squire

David Squire said:
Abigail wrote:
David Squire ([email protected]) wrote on MMMMDCCLXIII
September MCMXCIII in <URL:;;
;; > What is the difference with ()
;;
;; allows you to construct a list

Eh, not really. Context makes lists - the only place where () is significant
in creating a list is on the left hand side of the x.
I am happy to be corrected on this, but the usage I was referring to, in
the context of the OP's original post, is something like this:

my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance flawed?

It is correct, it is even necessary, but it has nothing to do with list
context.

"=" binds rather tightly in Perl, so leaving out the () is interpreted
as

( @new_array = 1), 2, 3;

(which is worth a warning).

The parens around ( 1, 2, 3) serve to change the precedence, just like
in "($x + $y)*$z".

Ah. Thanks very much. I guess it is a happy coincidence that the
parentheses for precedence here make the RHS look like what you would
want if you were inducing list context on the LHS.


DS
 
A

anno4000

David Squire said:
[...]
my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance flawed?

It is correct, it is even necessary, but it has nothing to do with list
context.

"=" binds rather tightly in Perl, so leaving out the () is interpreted
as

( @new_array = 1), 2, 3;

(which is worth a warning).

The parens around ( 1, 2, 3) serve to change the precedence, just like
in "($x + $y)*$z".

Ah. Thanks very much. I guess it is a happy coincidence that the
parentheses for precedence here make the RHS look like what you would
want if you were inducing list context on the LHS.

It is one of the things that make Perl look more consistent than it
is.

Anno
 
D

Dr.Ruud

David Squire schreef:
my @new_array = (1, 2, 3);

as opposed to, say,

my $new_array_ref = [1, 2, 3];

Is my understanding of the usage of (...) in the first instance
flawed?

$ perl -le '@_=1,2,3; print "@_"'
1


$ perl -le '@_=(1,2,3); print "@_"'
1 2 3


$ perl -le '@_=@{[1,2,3]}; print "@_"'
1 2 3


$ perl -le '@_=do{1,2,3}; print "@_"'
1 2 3


$ perl -le '$_=(@_=4,5,6); print "@_\n$_"'
4
6


$ perl -le '$_=(@_=(4,5,6)); print "@_\n$_"'
4 5 6
3
 

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,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top