Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Multi-Dimensional Arrays

Status
Not open for further replies.

rseals

MIS
Dec 19, 2001
8
0
0
US
I'm familar with using a hash to parse a file and keep a count of the number of instances something shows up.

while (<INPUTLOG>) {
chop($_);

($timestamp,$sfcat,$sfresult,$username,$clientip,$csuri)=split (/,/,$_);
if ($sfresult eq 1 ){
$count{$sfcat}++;
$count2{$username}++;
$count3{$csuri}++;
}
}

foreach $k (sort {$count{$b} <=> $count{$a}} keys %count) {
print &quot;$k \t$count{$k}\n&quot;;
}

I want to parse a file and keep track of multiple things. So instead of just seeing the result: &quot;bsmith 100&quot; I would like to keep track of &quot;bsmith 100&quot;.

I've tried $count{$username,$csuri}++ but when it prints out $k I just get a blank.
 
Oh, I though maybe using a multi-dimensional array might be the way to go....
 
After doing some head banging and trying to wrap my head around these things I'm still running into one small problem.

%<---------------------------------------------------------->%

while (<INPUTLOG>) {
chop($_);

($timestamp,$sfcat,$sfresult,$username,$clientip,$csuri)=split (/,/,$_);
if ($sfresult eq 1 ){
$hash{$username}{$csuri}++;
}
}

while (( $key, $value) = %hash) {
print &quot;$key = $value \n&quot;;
}
%<------------------------------------------------------->%

This prints out the username = HASH(0x8067e70).

What am I missing here?
 
Something like this might work:
Code:
while ( my ( $key, $value ) = %hash ) {
    print &quot;$key:\n&quot;;
    while ( my ( $subkey, $subvalue ) = %{$key{$value}} ) {
        print &quot;\t$subkey = $subvalue\n&quot;;
    }
}
Have a look at complex data structures with the Data::Dumper module. It helps understand what you have created:
Code:
use Data::Dumper;
print Dumper(\%hash);
Sorry, I can't test any of this as I don't have access to a box with perl on it at the moment.

Will. will@hellacool.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top