Velocity Reviews > Perl > Arrays and Hashes

# Arrays and Hashes

Guy
Guest
Posts: n/a

 05-24-2009
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

Guy
Guest
Posts: n/a

 05-25-2009
"Ben Morrow" <(E-Mail Removed)> a écrit dans le message de news:
http://www.velocityreviews.com/forums/(E-Mail Removed)...
>
> Quoth "Guy" <(E-Mail Removed)>:
>> 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

News123
Guest
Posts: n/a

 05-25-2009
Guy wrote:

>>> Is this correct? The type of parenthesis etc?
>>> Guy
>>>
>>> @x; Entire Array
>>> %x; Entire Hash
>>>

> . . . .
>
>
> 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

Guy
Guest
Posts: n/a

 05-25-2009
"News123" <(E-Mail Removed)> a écrit dans le message de news:
4a1a57d4\$0\$5238\$(E-Mail Removed)...
> 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

Martijn Lievaart
Guest
Posts: n/a

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

> 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

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post kazaam Ruby 12 09-13-2007 01:30 PM ernestm@mindspring.com Perl Misc 3 01-31-2006 04:40 AM Steven Arnold Ruby 3 11-23-2005 03:25 PM Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM Ben Holness Perl 8 10-08-2003 06:57 AM

Advertisments