Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

why does

by gumpu (Friar)
on Aug 28, 2000 at 21:36 UTC ( #30013=perlquestion: print w/replies, xml ) Need Help??

gumpu has asked for the wisdom of the Perl Monks concerning the following question:

The following code puzzles me

#!/usr/bin/perl -w use strict; my %hash; $hash{"key1"} = "value1"; $hash{"key2"} = "value2"; $hash{"key3"} = "value3"; # (1) this works... my %inverse_hash; %inverse_hash = reverse(%hash); print keys (%inverse_hash); # (2) this does not print keys (reverse (%hash));

Why does (1) work and (2) not. Both seem to do the same.

Sniplet (2) gives the error; "Type of arg 1 to keys must be hash (not reverse)", however doesn't reverse return a hash?

Have Fun

Replies are listed 'Best First'.
Re: why does
by btrott (Parson) on Aug 28, 2000 at 21:38 UTC
    No, reverse returns a list. When you assign that list to a hash, you coerce that list *into* a hash. Without that explicit coercion, you'll get an error.

    This works:

    print keys %{ { reverse %hash } };
    but it's way ugly, and it's probably slower, since it takes an anonymous hash ref to the list returned by reverse, then dereferences that hash ref into a hash.

      Thanks

      On a side note, the full tittle of this post was supposed to be 'why does "keys reverse %hash" not work' both somehow everything after " dissapeared in thin air :)

      Have Fun

        I'll just address this quickly -- you lost everything after the double quote because you inadvertently screwed up how the browser reads the attribute values of the HTML. PerlMonks uses forms to collect input and send it back to the user. A well-coded text-box would look something like:

        <input type="text" name="title" value="some title">

        So the problem is, you've now tried to submit something like:

        <input type="text" name="title" value="why does "keys reverse %hash"">

        Obviously, whatever system PerlMonks is using on the backend barfs on the multiple double-quotes and just parses up to what it believes to be the closing quote.

        I'd imagine there's some good reason they can't perform a regexp and substitute a double quote out -- maybe they haven't had time, or maybe there's some other reason of which we are unaware.

        Regardless:

        &quot;

        (that's &-q-u-o-t-;) should work in titles.

        That, and other valuable pieces of info can be found at:

        Need Help?.

        Later.

      And, intuitivley, it should work. I helped suggest that keys(), values() and each() work on list in a DWIM manner for Perl 6 but, sadly, was shot down miserably.

      Why? Mostly because of prototype issues and supposed huge spead decreases as a result. I don't believe them fully, it could be done.

      Oh well... At least I found someone who uses Perl like I do, that's two for Tom ;-)

      --
      Casey
      
      Why is %{{reverse %hash}} a "hash ref" rather than a hash? I understand that the Camel book on pg 246 uses this terminology. But it seems to me that %{{reverse %hash}} is an actual hash.
        You're right. The hash reference I was referring to is the intermediate result in that statement, created by
        { reverse %hash }
        That's an anonymous hash reference. The outer brackets not in the above statement then dereference that to create a hash.
RE (tilly) 1: why does
by tilly (Archbishop) on Aug 28, 2000 at 21:51 UTC
    btrott nailed the problem, but let me add that from your snippet it seems that you didn't know about the values function.

    BTW browsing perlman:perlfunc:categorical is very worthwhile for learning about these nice built-in functions. It won't match exactly what you have installed, for that you will need to copy the table that you will find in perldoc perlfunc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://30013]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2022-12-09 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?