Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Extending Array

by VingInMedina (Acolyte)
on Mar 21, 2012 at 14:39 UTC ( #960781=perlquestion: print w/replies, xml ) Need Help??
VingInMedina has asked for the wisdom of the Perl Monks concerning the following question:

I was wondering if it is at all possible to do something like this? (I know this doesn't work)

I am trying to extend the values in the $thing array.

#!/usr/bin/perl use strict; use warnings; my $thing; $thing = { 'A' => 'hello', 'B' => 'there' }; $thing = { 'C' => 'cutie', 'D' => 'pie' }; print "$thing->{'A'} $thing->{'B'} $thing->{'C'} $thing->{'D'}\n";

Replies are listed 'Best First'.
Re: Extending Array
by moritz (Cardinal) on Mar 21, 2012 at 14:57 UTC

    $thing is a hash reference, not an array. See perldata and perlreftut for some more information.

    Two ways to extend it:

    $thing = { %$thing, 'C' => 'cutie', 'D' => 'pie' }; # OR $thing->{C} = 'cutie'; $thing->{D} = 'pie';

    The first one generates a new hash and a new reference to it, the second adds items to the existing hash. Which one you should use depends on your use case (and is only relevant if there are other references to the same hash somewhere).

      Why do you want to create a new hash? Just adding to the old hash ref won't work?

      Can you give a scenario where do you use 1st case?

        Can you give a scenario where do you use 1st case?

        If I write a subroutine that receives a hash reference as an argument, I wouldn't modify it in-place, because the caller might want to re-use it.

        Simplified example:

        # caller: my %h = (a => 1, b => 2); something(\%h); say $_ for keys %h; # callee sub something { my $arg = shift; $arg = { %arg, c => 3, d => 4, }; # do more stuff with $arg here }

        If you use $arg->{c} = 3 in sub something, %h changes in the caller's code, which is very likely to cause confusion.

Re: Extending Array
by jandrew (Chaplain) on Mar 21, 2012 at 15:12 UTC

    An a additional possibility is to turn the HashRef into a hash variable and call a hash slice to add the next two key / value pairs. Read the perlvar detail on $; for more information

Re: Extending Array
by VingInMedina (Acolyte) on Mar 21, 2012 at 15:34 UTC

    Yes, I was thinking associative array when I wrote that, so I was incorrect.

    Anyway, thank you for the answer. It's so obvious, I don't know why I didn't manage to come up with it myself.

      The person that concocted the term "associative array" should be ... excommunicated. This misguided attempt at creating an "easier to understand" term has caused so much confusion ...

      Say NO to associative array!

      Enoch was right!
      Enjoy the last years of Rome.

        The person that concocted the term "associative array" should be ... excommunicated.

        The term 'associative array' way pre-dates the overloading of the term 'hash' as a name for 'content addressable storage'.

        • SNOBOL: circa 1962 had associative arrays implemented using a form of hashing and called them 'tables'.
        • MUMPS: circa 1966 had associative arrays implemented using sparse B-trees and just called them 'arrays'.
        • REXX: circa 1979 had associative arrays implemented using binary trees and called them 'stems'.

        The term is a description of the abstract mechanism of addressing data by what it contains (the value is associated with its key); rather than by where it is (a standard C-type array); or how that abstraction is implemented (Perl's hashes).

        Silly to condemn a term (or its unknown coiner).

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      I prefer dictionary because I get to say Richard :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://960781]
Approved by Corion
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-03-20 22:23 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (260 votes). Check out past polls.