Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
It is probably a good idea to take the plunge and try new modules as other Monks have suggested. However, I have put a script together to show you one way of preserving your insertion order when using hashes. I have used hash references, sometimes called anonymous hashes, which are useful when building complex data structures. Doing $refToHash = {} creates a reference to an empty hash. They are also useful for passing data into and out of subroutines.

I have taken a random subset of the X11 rgb.txt file as data source in order to build a hash of colours. They are in no particular order but the order they are in can be preserved. I have also used Data::Dumper so you can see what the data structure looks like after we have constructed it.

use strict; use warnings; # Use this to show the data structure. # use Data::Dumper; # Initialise a hash reference and a sequence no. # my $rhPreserve = {}; my $insertionOrder = 0; # Read our data line by line, chomping off the newline # and splitting on the ":" and using the colour name # as the key and RGB for the value. # while(<DATA>) { chomp; my ($value, $key) = split /:/; # Make an entry in the hash reference for this colour but # the value is another hash with two key/value pairs, one # for our RGB value and the other the insertion order # which we increment after insertion. # $rhPreserve->{$key} = { value => $value, insertionOrder => $insertionOrder ++ }; } # Use Data::Dumper to print out the hash reference so we can see # what we have constructed. The colour "keys" will come out in an # order determined by the underlying hashing algorith. # my $dd = Data::Dumper->new([$rhPreserve], ["rhPreserve"]); print "Data structure\n", $dd->Dumpxs(); # To use our insertion order, which we have preserved in the hash # reference, we can sort by "insertionOrder" then use the sorted keys # to access the hash reference. # my @keysInInsertionOrder = sort { $rhPreserve->{$a}->{insertionOrder} <=> $rhPreserve->{$b}->{insertionOrder} } keys %$rhPreserve; print "Data in insertion order\n"; foreach (@keysInInsertionOrder) { print "Insertion order - $rhPreserve->{$_}->{insertionOrder}\n", " Key - $_\n", " Value - $rhPreserve->{$_}->{value}\n"; } __END__ 255 250 250:snow 248 248 255:GhostWhite 245 245 245:WhiteSmoke 220 220 220:gainsboro 255 250 240:FloralWhite 253 245 230:OldLace 250 240 230:linen 250 235 215:AntiqueWhite 255 239 213:PapayaWhip 255 235 205:BlanchedAlmond 255 228 196:bisque 255 218 185:PeachPuff 255 222 173:NavajoWhite 255 228 181:moccasin 255 248 220:cornsilk 255 255 240:ivory 255 250 205:LemonChiffon 255 245 238:seashell 240 255 240:honeydew

When run this produces the following

Data structure $rhPreserve = { 'LemonChiffon' => { 'value' => '255 250 205', 'insertionOrder' => 16 }, 'cornsilk' => { 'value' => '255 248 220', 'insertionOrder' => 14 }, 'bisque' => { 'value' => '255 228 196', 'insertionOrder' => 10 }, 'GhostWhite' => { 'value' => '248 248 255', 'insertionOrder' => 1 }, 'WhiteSmoke' => { 'value' => '245 245 245', 'insertionOrder' => 2 }, 'PapayaWhip' => { 'value' => '255 239 213', 'insertionOrder' => 8 }, 'honeydew' => { 'value' => '240 255 240', 'insertionOrder' => 18 }, 'NavajoWhite' => { 'value' => '255 222 173', 'insertionOrder' => 12 }, 'OldLace' => { 'value' => '253 245 230', 'insertionOrder' => 5 }, 'AntiqueWhite' => { 'value' => '250 235 215', 'insertionOrder' => 7 }, 'linen' => { 'value' => '250 240 230', 'insertionOrder' => 6 }, 'ivory' => { 'value' => '255 255 240', 'insertionOrder' => 15 }, 'PeachPuff' => { 'value' => '255 218 185', 'insertionOrder' => 11 }, 'snow' => { 'value' => '255 250 250', 'insertionOrder' => 0 }, 'seashell' => { 'value' => '255 245 238', 'insertionOrder' => 17 }, 'FloralWhite' => { 'value' => '255 250 240', 'insertionOrder' => 4 }, 'moccasin' => { 'value' => '255 228 181', 'insertionOrder' => 13 }, 'gainsboro' => { 'value' => '220 220 220', 'insertionOrder' => 3 }, 'BlanchedAlmond' => { 'value' => '255 235 205', 'insertionOrder' => 9 } }; Data in insertion order Insertion order - 0 Key - snow Value - 255 250 250 Insertion order - 1 Key - GhostWhite Value - 248 248 255 Insertion order - 2 Key - WhiteSmoke Value - 245 245 245 Insertion order - 3 Key - gainsboro Value - 220 220 220 Insertion order - 4 Key - FloralWhite Value - 255 250 240 Insertion order - 5 Key - OldLace Value - 253 245 230 Insertion order - 6 Key - linen Value - 250 240 230 Insertion order - 7 Key - AntiqueWhite Value - 250 235 215 Insertion order - 8 Key - PapayaWhip Value - 255 239 213 Insertion order - 9 Key - BlanchedAlmond Value - 255 235 205 Insertion order - 10 Key - bisque Value - 255 228 196 Insertion order - 11 Key - PeachPuff Value - 255 218 185 Insertion order - 12 Key - NavajoWhite Value - 255 222 173 Insertion order - 13 Key - moccasin Value - 255 228 181 Insertion order - 14 Key - cornsilk Value - 255 248 220 Insertion order - 15 Key - ivory Value - 255 255 240 Insertion order - 16 Key - LemonChiffon Value - 255 250 205 Insertion order - 17 Key - seashell Value - 255 245 238 Insertion order - 18 Key - honeydew Value - 240 255 240

I hope this will help.

Cheers,

JohnGG


In reply to Re: Cutting the top percentage out of a Hash by johngg
in thread Cutting the top percentage out of a Hash by Gavin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-08-11 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?