Arrays and Hashes

G

Guy

Is this correct? The type of parenthesis etc?
Guy

@x; Entire Array
%x; Entire Hash

$x[0]; One item of Array
%x{key}; One item of Hash

$r=\@x; Referencing Array variable
$r=\%x; Referencing Hash variable

@$r; De-referecing entire array
De-referecing entire hash ???

$$r[0]; Referencing one array item
$$r{key}; Referencing one hash item

(v1,v2); Anonymous Array (depending on context)
(k1,v1,k2,v2); Anonymous Hash (depending on context)

[v1,v2]; Reference to Anonymous Array
{k1,v1,k2,v2}; Reference to Anonymous Hash

$a=(1,2,3)[0]; Slice of a list
@x=(1,2,3)[0,1]; Slices of a list
($a,$b)=(1,2,3)[0,1]; Slices of a list
@y=@x[0,1]; Slices of array

@x=@y{k1,k2}; Slices of hash (use @ not % because partial hash)
@x=@y{@keys}; Slices of hash (use @ not % because partial hash)

@x{@keys}=@values; Assigning to specific or all keys of hash

$y[0]=\@x; Creating Array of Array (2D Array)
$y[0][0]; Accessing item

$y[0]=\%x; Creating Array of Hash
$y[0]{key}; Accessing item

$y{key}=%x; Creating Hash of Hash
$y{key}{key2}; Accessing item
 
G

Guy

Ben Morrow said:
Quoth "Guy said:
Is this correct? The type of parenthesis etc?
Guy

@x; Entire Array
%x; Entire Hash

$x[0]; One item of Array
%x{key}; One item of Hash

$x{key}

Perl is more consistent than you seem to think. An initial '$' always
indicates a single value, an initial '@' always indicates a list.
$r=\@x; Referencing Array variable
$r=\%x; Referencing Hash variable

One would normally say 'Taking a reference to an array'.
@$r; De-referecing entire array
De-referecing entire hash ???
%$r

$$r[0]; Referencing one array item
$$r{key}; Referencing one hash item

Yes. You simply replace the 'x' part of '$x{key}' with a hashref. If the
hashref is 'too complicated', the normally-optional braces around the
name become required, giving '${$r}{key}'. See perlreftut.

It's usually clearer to use the arrow notation $r->{key}. Again, see
perlreftut.
(v1,v2); Anonymous Array (depending on context)
(k1,v1,k2,v2); Anonymous Hash (depending on context)

No. These are both simply lists. Assigning a list to a hash or array
variable is one way of setting its values.
[v1,v2]; Reference to Anonymous Array
{k1,v1,k2,v2}; Reference to Anonymous Hash

Yes. Note that what is inside the [] or {} can be any list; so something
like

[ function_returning_list() ]

works perfectly well.
$a=(1,2,3)[0]; Slice of a list
@x=(1,2,3)[0,1]; Slices of a list
($a,$b)=(1,2,3)[0,1]; Slices of a list
@y=@x[0,1]; Slices of array
Yes.

@x=@y{k1,k2}; Slices of hash (use @ not % because partial hash)
@x=@y{@keys}; Slices of hash (use @ not % because partial hash)

Yes, except '(use @ not $ because the result is a list not a scalar)'.
@x{@keys}=@values; Assigning to specific or all keys of hash

$y[0]=\@x; Creating Array of Array (2D Array)

Note that here $y[0] will contain a ref to @x, so modifying (say)
$y[0][0] will modify $x[0]. You can copy the array with

$y[0] = [ @x ];

following the rule 'anything that returns a list can go inside the []'.
$y[0][0]; Accessing item

$y[0]=\%x; Creating Array of Hash
$y[0]{key}; Accessing item

$y{key}=%x; Creating Hash of Hash

$y{key} = \%x;
$y{key}{key2}; Accessing item

Which parts of perlreftut were unclear to you?

Ben


Thanks for the corrections. I just want to print myself a -quick- reference
sheet that I can tape to the wall next to my computer as I try to grasp all
this stuff.
Guy
 
N

News123

Guy said:
. . . .


Thanks for the corrections. I just want to print myself a -quick- reference
sheet that I can tape to the wall next to my computer as I try to grasp all
this stuff.
Guy

Yes, it's a good exercise to write ones own quick-reference as it means,
that you ha to think about each entry at least once.

Now that you have written your quick reference you can also look at:

http://perldoc.perl.org/ (you can now select the perl version on the
right hand)
and enter cheat in the search field.
This will return

http://perldoc.perl.org/perlcheat.html

You could use parts of it and complete your quick-reference
 
G

Guy

News123 said:
Guy wrote:

Yes, it's a good exercise to write ones own quick-reference as it means,
that you ha to think about each entry at least once.

Now that you have written your quick reference you can also look at:

http://perldoc.perl.org/ (you can now select the perl version on the
right hand)
and enter cheat in the search field.
This will return

http://perldoc.perl.org/perlcheat.html

You could use parts of it and complete your quick-reference


Thanks, it'll help for sure.
Guy
 
M

Martijn Lievaart

in general it is better to
learn the rules about something rather than a short list of simple
examples. the examples don't explain why things are as they are nor to
they help with more complex examples. rules can be used in all cases,
simple or complex.

Although I agree greatly with the sentiment of your post, people do tend
to learn rules from examples. I personally rather learn the rules and
apply them, but other people just don't learn that way. The trick is to
have a cheat sheet that covers all the rules with great examples....

(Hard to do, but works great)

M4
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top