Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re^3: To Hash or to Array--Uniqueness is the question.

by Nkuvu (Priest)
on Dec 02, 2005 at 17:10 UTC ( #513652=note: print w/replies, xml ) Need Help??

in reply to Re^2: To Hash or to Array--Uniqueness is the question.
in thread To Hash or to Array--Uniqueness is the question.

On the contrary, you're sorting the keys of the hash, but you lost the order of the input. Given the sample data I used in my script (one foo bar one baz two quack baz) the sort method prints out bar, baz, one, quack, foo, two. Of course if one wanted the sorted order then the use of the array is pointless. But if one is looking for the order of the input data, an array (or a module) is, as far as I know, necessary.

Replies are listed 'Best First'.
Re^4: To Hash or to Array--Uniqueness is the question.
by Eimi Metamorphoumai (Deacon) on Dec 02, 2005 at 17:42 UTC
    No, he's using the values of the hash to store the intended order, and sorting on the values instead of the keys. The downside is that instead of just keeping the order around, we're spending time sorting to get the order back.

      Bah, this is what I get for being lazy. ;)

      Instead of creating a file so that I could open it via the $fh filehandle, I used the following snippet:

      # sort method my @values = qw( one foo bar one baz two quack baz ); my %h = (); foreach (@values) { $h{ $_ } ||= $.; } print "\n\nSort method:\n"; print (join ", ", sort { $h{a} cmp $h{b} } keys %h);

      And totally missed the fact that Moron was using $. as the hash value. Thanks for pointing out what I should have already noticed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://513652]
Discipulus SharePoint will insinuate in my $workspace I think i'll need alot of patience..
[erix]: for all the tea in china book
[erix]: about perfide albion
[erix]: I guess that should be Perfidious Albion
Discipulus must prepare some ammunitions: XML::Compile::SOAP but what the heck!

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-06-27 09:00 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (601 votes). Check out past polls.