in reply to Re^3: hash referencing...best approach?
in thread hash referencing...best approach?

I have about 40 possible departments, so I just wrote a small sub routine

sub write_it{ my ($dept,$output)=@_; my $output_file=$dept."_access.log"; open DATA,">> $output_file")||die ("unable to open $output_file $!\n") +; print DATA $output."\n"; close DATA; }


This works pretty great, I was able to read through one of the access logs and create the specific files in about 4 minutes. The one problem I have now is appending the user_code to the end of the access log line.

I tried to change the way I 'write' to the session_hash while reading the session logs to :
$session_hash($sessio_id}{$user}=$dept_code;
But this just screwed me up later down the line when I reading through the access logs. For this part I currently have:
open (HTTP,$access_log)||die ("unable to open $access_log $!\n"); while (my $line2=<HTTP>) { chomp $line2; my @fields=split /\s+/, $line2; my $session=@fields[6]; my $session=substr($session,(index($session,"?")+12),(inde +x($session,"|"))-(index($session,"?")+12) ); if (length($session) ==52) { &write_it($session_hash{$session},$line2); }
I tried to incorporate the user_code part into this and ended up getting the hash address everywhere. In other words, my file names became hash addresses and my user_code values where null. Surely I am missing something minor here. Thanks for all the help thus far, it has proven most superb.

Replies are listed 'Best First'.
Re: ^3+: hash referencing...best approach?
by Roy Johnson (Monsignor) on Nov 25, 2003 at 02:51 UTC
    You don't need $user as part of your key, since $session_id is unique. If you want to retrieve something, store it in the value part of the hash. The key is for stuff you want to look up by. So:
    $session_hash{$session_id} = [$dept_code, $user]; #array ref
    and in write_id, the magical print will be
    print DATA join(',', @$output), "\n"; # Or something like that
    Review perldoc perlreftut to get a better handle on how complex data structures are built in Perl.

    The PerlMonk tr/// Advocate
Re: Re: Re^3: hash referencing...best approach?
by Roger (Parson) on Nov 25, 2003 at 00:25 UTC
    Change your write it line from
    &write_it($session_hash{$session},$line2);
    to -
    write_it($session_hash{$session}{$_},$line2) for keys %{$session_hash{$session}}; # retrieve user names
    And it will work.