# Arrays and Hashes

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Is this correct? The type of parenthesis etc?
Guy

@x;          Entire Array
%x;          Entire Hash

\$x[0];       One item of Array
%x;     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;    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
;     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;   Slices of hash (use @ not % because partial hash)
@x=@y;   Slices of hash (use @ not % because partial hash)

@x=@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];      Accessing item

\$y=%x;        Creating Hash of Hash
\$y;     Accessing item

## Re: Arrays and Hashes

\$x

Perl is more consistent than you seem to think. An initial '\$' always
indicates a single value, an initial '@' always indicates a list.

One would normally say 'Taking a reference to an array'.

%\$r

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

It's usually clearer to use the arrow notation \$r->. Again, see
perlreftut.

No. These are both simply lists. Assigning a list to a hash or array
variable is one way of setting its values.

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

[ function_returning_list() ]

works perfectly well.

Yes.

Yes, except '(use @ not \$ because the result is a list not a scalar)'.

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 = \%x;

Which parts of perlreftut were unclear to you?

Ben

## Re: Arrays and Hashes

4i1pe6-qt6.ln1@osiris.mauzo.dyndns.org...

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

## Re: Arrays and Hashes

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

you should have to rip that down very soon hopefully. most of what you
have in there is very basic perl and easy to derive from a few
rules. dereferencing anything is just taking a reference and surrounding
it with \${} (or the correct sigil). you can drop the {} in the case
where the ref is a scalar var. that removes several lines from your
cheat sheet. another it arrays and hashes have very similar syntax and
you use [] for arrays and {} for hashes along with the @ vs %
sigils. that means once you learn how to make anon arrays, anon hashes
follow. same for slices and other things. that removes about 1/3 of the
cheat sheet. similar rules can remove most of the rest. 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.

uri

--
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

## Re: Arrays and Hashes

On Sun, 24 May 2009 21:41:04 -0400, Uri Guttman wrote:

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

## Re: Arrays and Hashes

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

## Re: Arrays and Hashes

4a1a57d4\$0\$5238\$426a74cc@news.free.fr...

Thanks, it'll help for sure.
Guy