Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Comparing all keys in a hash

by Helter (Chaplain)
on Mar 20, 2003 at 14:48 UTC ( [id://244601]=note: print w/replies, xml ) Need Help??


in reply to Re: Comparing all keys in a hash
in thread Comparing all keys in a hash

I had a few spare moments, and tried using your code shotgunefx. You have a curly brace at the end of your second line and I assume you mean a paren.

Here's some test code to prove my point:
#!/usr/bin/perl use strict; use warnings; my %h1; my %h2; my %h3; for( my $count = 0, my $char = 'a'; $char ne 'aaaa'; $count++, $char++ + ) { # print "Adding $char with value of $count to h1\n"; $h1{ $char } = $count; } foreach my $key ( keys %h1) { $h2{ $key } = $h1{$key}; } foreach my $key (reverse sort keys %h1) { $h3{ $key } = $h1{$key}; } #foreach my $key (sort keys %h1) { print "h1: key: $key value: $h1{ +$key}\n";} #foreach my $key (sort keys %h2) { print "h2: key: $key value: $h2{ +$key}\n";} #foreach my $key (sort keys %h3) { print "h3: key: $key value: $h2{ +$key}\n";} my ($k1,$k2) = ( join('',sort keys %h1), join('',sort keys %h2) ); my ($k3,$k4) = ( join('',keys %h1), join('',keys %h2) ); my ($k5,$k6) = ( join('',sort keys %h1), join('',sort keys %h3) ); my ($k7,$k8) = ( join('',keys %h1), join('',keys %h3) ); print "inserted same order, keys sorted compare: " . ($k1 eq $k2 ? 1 : + 0) . "\n"; print "inserted same order, keys not sorted compare: " . ($k3 eq $k4 ? + 1 : 0) . "\n"; print "inserted reverse order, keys sorted compare: " . ($k5 eq $k6 ? +1 : 0) . "\n"; print "inserted reverse order, keys not sorted compare: " . ($k7 eq $k +8 ? 1 : 0) . "\n"; __DATA__ Prints: inserted same order, keys sorted compare: 1 inserted same order, keys not sorted compare: 0 inserted reverse order, keys sorted compare: 1 inserted reverse order, keys not sorted compare: 0
So to answer your question about sorting, the sort of the keys is necessary or the compare will not work.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found