Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: comparing and removing values from an array of hashes

by roboticus (Chancellor)
on Oct 22, 2012 at 14:15 UTC ( #1000349=note: print w/ replies, xml ) Need Help??


in reply to comparing and removing values from an array of hashes

rizzler:

I'd suggest adding another column to your data, and taking two passes through the data:

On the first pass, you can add the new column. Add 0 if there's no value in check, 1 otherwise. Also in this pass, keep track of the maximum quantity and maximum price.

On the second pass, you can use your maxima that you found in pass 1 and selectively turn the new column to 0 if you want to discard the row.

Finally, take a pass through the data and copy the rows that still have a 1 in the new column to your results array.

Strictly, you don't need to add another column for your task, as you could easily perform the missing value test on pass 2. I thought I'd suggest adding it in case you come up with new criteria that may require yet another pass over the data. It's just a simple sifting technique where you keep marking rows to discard. Once you're done, whatever has a 1 in the new column passed all your tests.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


Comment on Re: comparing and removing values from an array of hashes
Replies are listed 'Best First'.
Re^2: comparing and removing values from an array of hashes
by perl_walker (Novice) on Oct 22, 2012 at 14:26 UTC

    Hi rizzler,

    I think this may help you,

    my %quantity=(YEAR => 5, MONTH => 4, WEEK => 3, DAY => 2, HOUR =>1); my $a1="A,Star_1GB,MONTH,1000,0"; my $b1="B,Unlim60,MONTH,1000,6000"; my $b2="B,Unlim60,YEAR,1000,6000"; my $c1="C,Unlim,DAY,50,6000"; my $c2="C,Unlim,HOUR,50,6000"; my $d1="D,,MONTH,500,8000"; my @vals = ($a1, $b1, $b2, $c1, $c2, $d1); @vals = map{$_->[0]} sort{$quantity{$b->[1]} <=> $quantity{$a->[1]}} s +ort{$b->[2] <=> $a->[2]} sort{$b->[3] <=> $a->[3]} map{[$_, /(YEAR|MO +NTH|WEEK|DAY|HOUR),([^,]*),([^,]*)$/i]} @vals; print join("\n",@vals);

    Output will be:

    B,Unlim60,YEAR,1000,6000 B,Unlim60,MONTH,1000,6000 A,Star_1GB,MONTH,1000,0 D,,MONTH,500,8000 C,Unlim,DAY,50,6000 C,Unlim,HOUR,50,6000

      Thanks :) this does almost everthing I needed. Even with my poor skills I can probably work out the rest.

      Cheers,

      Rizz

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2015-07-30 03:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls