@hash{@arr}=();  scalar into array then?

Posted on 2006-04-10
Last Modified: 2008-03-03
Just want to get a handle on what's going on here.  This  
answer was given in response to remove duplicate entires.

Here is the construct under question.

  @arr = keys %hash;

My logic ....

@hash{'key'} is one value.
@hash{qw(A B C D)} is my view of his @hash{@arr}

So, @a = (qw( A B C)) initializes an array
    @a = {qw( A B C)} initializes a hash.

Then, along the same line (my lines), you could do this then

    $a = {qw(A B C)}

    and then

    foreach (@a) {

and get a normal array from that?  Except it
doesn't work.  Why can't you force $a into
an array.  @a = {list} forced @a into %a!

Strighten me out.  Thanks.
Question by:hank1
    LVL 20

    Accepted Solution

    You were doing fine up til the line

    @a = {qw( A B C)} initializes a hash.

    While this is true, in a way, it isn't doing what you think. The construct

     {qw(A B C) }

    returns a reference to an anonymous hash and this reference is taken (even though you didn't write it precisely right) as the single element of the array @a. Data::Dumper will tell you that \@a looks like:

                'A' => 'B',
                'C' => undef

    The constructions that you were given were using hash _slices_.

    @hash{@arr} = ();

    is a shortcut to initialize a hash (using the _slice_) with a set of keys and undefined values. For this purpose, the keys are all that is needed. In initializing the hash, duplicate keys replace any earlier instances of themselves, so to get a list of unique keys you do

    @arr = keys %hash;

    $hash{'key'} is one value.
    @hash{'key'} is a hash slice with only one value. It's equivalent to the simple $hash{'key'} in many (most?) contexts. It's only when there's a multi-item list inside the braces that you must treat it as an array.

    LVL 84

    Expert Comment

    @hash{qw(A B C D)} = ()
    is equivalent to
    ($hash{A},$hash{B},$hash{C},$hash{D}) = ();

    @hash{'key'} = localtime;
    is not equivalent to
    $hash{'key'} = localtime;
    LVL 20

    Expert Comment

    Ozo's right, of course. I just had trouble thinking up the right counterexample (been out of practice). The contexts where @hash{'key'} will be taken as equivalent to $hash{'key'} will fall on the right hand side of an equals sign, but it would be better to use scalars when you want scalars and arrays (or slices) where you want arrays.
    LVL 1

    Author Comment

    Thanks both.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now