Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Hashes, Arrays, and Confusion -- In a bit over my head!

by Riales (Hermit)
on Mar 29, 2012 at 22:00 UTC ( #962488=note: print w/ replies, xml ) Need Help??


in reply to Hashes, Arrays, and Confusion -- In a bit over my head!

I'd say you're on the right track. As long as you apply the regex to tear the 'MONKEY' part out of column 3 before you check to see if you've seen it before, you should do just fine.

I would make one pass through each line to build a hash with the value from column 3 as the key pointing to a hashref with num_false (a count) and col1_vals (an arrayref). Then I would go through each key of that hash and run the calculations you want to run. I'm thinking something like this (assuming your lines are in @lines):

my %seen = (); foreach my $line (@lines) { my @values = split ',', $line; my $col1_val = $values[0]; my $key = $values[2]; my $true_or_false = $values[7]; $key =~ /^LOGGED IN (\w+) GET/; $key = $1; $seen{$key}->{num_false}++ if $true_or_false eq 'false'; push @{$seen{$key}->{col1_vals}}, $col1_val; } foreach my $key (keys %seen) { my $calcd = calculate_stuff($seen{$key}->{col1_vals}); my $num_false = $seen{$key}->{num_false}; print <<HERE; Key: $key Calc'd values: $calcd Num false: $num_false HERE }

I just took a stab at guessing the proper regex (for example: are spaces allowed?) because I don't know what all your input looks like, but the basic idea holds.


Comment on Re: Hashes, Arrays, and Confusion -- In a bit over my head!
Select or Download Code
Re^2: Hashes, Arrays, and Confusion -- In a bit over my head!
by bpthatsme (Novice) on Mar 29, 2012 at 22:21 UTC

    Thanks Riales!

    I am going to work with that a bit and update my code here for reference should anyone else stumble into it (and for criticism).

    In the meantime, further suggestions are ALWAYS welcomed!

    -bp-

      Ok, I had a few minutes to jump into this and have some more questions.

      I used the following code (pardon the ugly vim line numbers):

      183 my %seen = (); 184 185 open (FH, "< $resultsFile"); 186 my @lines = <FH>; 187 foreach my $line (@lines) { 188 my @values = split ',', $line; 189 190 my $col1_val = $values[0]; 191 my $key = $values[2]; 192 my $errors = $values[7]; 193 194 $key =~ s/LOGGED IN //; 195 $key =~ s/ GET //; 196 $key =~ s/ POST //; 197 198 # print "$key\n"; 199 200 $key = $1; 201 202 $seen{$key}->{error_count}++ if $errors eq 'false'; 203 push @{$seen{$key}->{col1_vals}}, $col1_val; 204 205 } 206 207 foreach my $key (keys %seen) { 208 my $error_count = $seen{$key}->{error_count}; 209# my $calcd = 210 print <<HERE; 211 Key: $key 212 Errors: $error_count 213 HERE 214 } 215 216 close (FH); 217 218 die;

      This does not print out any results for key or errors (lines 211 and 212). Perhaps something is being read wrong? The commented out line on 198 does return the values as intended however.

      My second question is regarding the calculations. How do I grab all of the col1_vals together in order to run an average since it is going through line by line?

      Thanks as always!

      -bp-

        This does not print out any results for key or errors (lines 211 and 212)
        Riales used this to get the $key:
        $key =~ /^LOGGED IN (\w+) GET/; $key = $1;
        Whereas you are using this:
        $key =~ s/LOGGED IN //; $key =~ s/ GET //; $key =~ s/ POST //; $key = $1;
        In your version you need to leave out the last line: $key = $1
        How do I grab all of the col1_vals together in order to run an average
        Riales code
        my $calcd = calculate_stuff($seen{$key}->{col1_vals});
        covers this. All of the col1_vals are now in $seen{$key}->{col1_vals} which is an array reference - if you print that out you will see:
        foreach my $key (keys %seen) { my $col1_vals = $seen{$key}->{col1_vals}; print "@$col1_vals\n"; }

        tangent pretty much covered this, but I can expand a little bit more about the arrayref bit:

        Like tangent said, the hashref $seen{$key}->{col1_vals} points to is an arrayref. To sum its contents, you can do something like this:

        my $sum = 0; foreach (@{$seen{$key}->{col1_vals}}) { $sum += $_; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-12-28 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls