in reply to Re^2: Hashes, Arrays, and Confusion -- In a bit over my head!
in thread Hashes, Arrays, and Confusion -- In a bit over my head!

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-

Replies are listed 'Best First'.
Re^4: Hashes, Arrays, and Confusion -- In a bit over my head!
by tangent (Parson) on Mar 30, 2012 at 01:38 UTC
    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"; }
Re^4: Hashes, Arrays, and Confusion -- In a bit over my head!
by Riales (Hermit) on Mar 30, 2012 at 03:24 UTC

    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 += $_; }