Arrays and Hashes

Discussion in 'Perl Misc' started by Guy, May 24, 2009.

  1. Guy

    Guy Guest

    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, May 24, 2009
    #1
    1. Advertising

  2. Guy

    Guy Guest

    "Ben Morrow" <> a écrit dans le message de news:
    ...
    >
    > Quoth "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

    >
    > $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
     
    Guy, May 25, 2009
    #2
    1. Advertising

  3. Guy

    News123 Guest

    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
     
    News123, May 25, 2009
    #3
  4. Guy

    Guy Guest

    "News123" <> a écrit dans le message de news:
    4a1a57d4$0$5238$...
    > 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
     
    Guy, May 25, 2009
    #4
  5. 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
     
    Martijn Lievaart, May 26, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    566
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    161
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    277
    Matthias Wächter
    Sep 13, 2007
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    214
  5. Replies:
    3
    Views:
    208
Loading...

Share This Page