Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

multidimensional dilemma

by nmerriweather (Friar)
on Jun 04, 2002 at 00:16 UTC ( [id://171363]=perlquestion: print w/replies, xml ) Need Help??

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

this is a simple question -- but i've spent an hour on it w/no luck, and can't seem to find an answer anywhere
right now, i've got a hash
%ResultStatusCodes = ( 1 => "registered", 2 => "unregistered", 3 => "waiting" )
i've been assigning hashes relating to users 'result status' var, that has one of those numbers. that works all fine.
$resultStatus[1] = 1;
which would mean record 1 ($resultStatus1) is 1. simple enough.
now, i need to seperate theses records by their 'resultStatus'
%ResultsByCode = ( registered => @ of registered ids unregistered => @ of unregistered ids waiting => @ of waiting ids )
or
@ResultsByCode = ( 1 = @ of registered ids 2 = @ of unregistered ids 3 = @ of waiting ids )
problem is -- i can't figure out how to do this. i think it should look something like this, though i know none are right
@resultsByCode = (); for ($i=1;$i<=5;$i++){ $resultsByCode[$i] = ();} for ($i=1;$i<=5;$i++){ push $resultsByCode[$resultStatus{$i}], $i; }
%resultsByCode = (); for ($i=1;$i<=5;$i++){ push $resultsByCode{$resultStatus{$i}}, $i; }
push "resultsByCodecode1 = registered" , record id
i'm at a loss. can someone point me in the right direction?

Replies are listed 'Best First'.
Re: multidimensional dilemma
by Russ (Deacon) on Jun 04, 2002 at 00:40 UTC
    Sounds like a job for grep (my favorite!):
    %ResultsByCode = ( registered => scalar(grep {$_ == 1} @resultStatus), unregistered => scalar(grep {$_ == 2} @resultStatus), waiting => scalar(grep {$_ == 3} @resultStatus), )

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: multidimensional dilemma
by Zaxo (Archbishop) on Jun 04, 2002 at 01:11 UTC

    $resultsByCode will be easiest to populate for numeric keys:

    $resultsByCode{$_}++ for @resultStatus;
    but the other way is not much more difficult:
    $resultsByCode{$resultStatusCodes{$_}}++ for @resultStatus;
    I'd recommend perldata, and perlsyn, and the Tutorials section here, because you show some confusion about hashes and arrays.

    Update: Ahh.. misunderstood, minor modification to get what you want. I don't know where your id numbers really come from, so I'll do like your example and number them from 1 up:

    push @{$resultsByCode{$resultStatus[$_]}},$_+1 for 0..$#resultStatus;
    That beats grep because it's only one pass through the data.

    After Compline,
    Zaxo

Re: multidimensional dilemma
by DelRode (Initiate) on Jun 04, 2002 at 03:12 UTC
    It seems you want two things. An ARRAY not a hash for your result status codes and a hash for your cumulator by name;

    sample perl script

    $resultstatuscodes[1]="registered"; $resultstatuscodes[2]="unregistered"; $resultstatuscodes[3]= "waiting"; my @resultstatus=(); for (my $i=0;$i<300;$i++) { $resultstatus[$i]= 1+ int(3*rand); }; my %results=(); $count=map {$results{$resultstatuscodes[$_]}++} @resultstatus; foreach my $resulttype(keys %results) { printf "%s => %5d (%4.2f%%)\n" ,$resulttype , $results{$resultty +pe}, 100 * $results{$resulttype} / $count;}
Re: multidimensional dilemma
by nmerriweather (Friar) on Jun 04, 2002 at 04:41 UTC
    well, yes.. but what i'm looking for is somthing like this
    input: @results = (); $results[1] = 1; $results[2] = 2; $results[3] = 1; $results[4] = 3; $results[5] = 1; $results[6] = 1; output: @resultsByCode = (); $resultsByCode[1] = (1,3,5,6); $resultsByCode[2] = (2); $resultsByCode[3] = (4);
    is that making any sense?
      nevermind
      push @{$resultsByCode[$resultStatus[$_]]},$_ for 0..5;
      was right -- i just switched to arrays and forgot. d'oh

      thanks again

Re: multidimensional dilemma
by nmerriweather (Friar) on Jun 04, 2002 at 02:20 UTC
    not confused about hashes vs arrays -- i just tend to like using hashes for everything if i can. i think its the curly braces or something. i think that grep will do the trick. forgive my ignorance.. but wouldn't $resultsByCode{$_}++ for @resultStatus; just present a tally of the results?
Re: multidimensional dilemma
by nmerriweather (Friar) on Jun 04, 2002 at 03:03 UTC
    sweet. that updated code does just the trick. thanks a ton!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found