How to access elements of an arrya refereneced in a hash of hashes?

P

Petyr David

I have a hash - the key is a filename, the value is a hash. That hash
has 2 values: the file's modification time and a reference to an array.
The array contains lines of text from the fille. I've been playing
around and with this thing for a while and can't figure out how to
access the elemnts of the array. Please review this code first:

# @file_list is an array contain output in this fashion
#
# filename: some pattern which might include a colon
# This works fine

my %file_hash;
my $file_list_cnt = @file_list;
foreach $fil_pat(@file_list){
$fil_pat =~ s/:/XXX/;
@file_and__pattern = split /XXX/, $fil_pat;
$fil_name = $file_and__pattern[0];
@file_stat_info = stat($fil_name);
$mod_time = $file_stat_info[10];
$pattern = $file_and__pattern[1];
if(! $file_hash{$fil_name}){
$file_hash{$fil_name} = {
mod_time => $mod_time,
finds => []
};
}
push (@{$file_hash{$fil_name} => finds} , $pattern);
}

# THIS PART ALSO WORKS FINE:

foreach $file_name ( sort { $file_hash{$b}->{'mod_time'} <=>
$file_hash{$a}->{'mod_time'} } keys %file_hash){
print "<li> " . "<a href=\"/zzz/mirror/" . $file_name . "\">" .
$file_name . "</a><br>";
}

# What I now want to do: for every file name I want to then print each
line in the array named finds.
 
J

John W. Krahn

Petyr said:
I have a hash - the key is a filename, the value is a hash. That hash
has 2 values: the file's modification time and a reference to an array.
The array contains lines of text from the fille. I've been playing
around and with this thing for a while and can't figure out how to
access the elemnts of the array. Please review this code first:

# @file_list is an array contain output in this fashion
#
# filename: some pattern which might include a colon
# This works fine

my %file_hash;
my $file_list_cnt = @file_list;
foreach $fil_pat(@file_list){
$fil_pat =~ s/:/XXX/;
@file_and__pattern = split /XXX/, $fil_pat;
$fil_name = $file_and__pattern[0];
@file_stat_info = stat($fil_name);
$mod_time = $file_stat_info[10];
$pattern = $file_and__pattern[1];
if(! $file_hash{$fil_name}){
$file_hash{$fil_name} = {
mod_time => $mod_time,
finds => []
};
}


Why not just:

my %file_hash;
foreach my $fil_pat ( @file_list ) {
my ( $fil_name, $pattern ) = split /:/, $fil_pat, 2;
unless ( exists $file_hash{ $fil_name } ) {
$file_hash{ $fil_name } = {
mod_time => ( stat $fil_name )[ 10 ],
finds => []
};
}

push (@{$file_hash{$fil_name} => finds} , $pattern);

That should be:

push @{ $file_hash{ $fil_name }{ finds } }, $pattern;

}

# THIS PART ALSO WORKS FINE:

foreach $file_name ( sort { $file_hash{$b}->{'mod_time'} <=>
$file_hash{$a}->{'mod_time'} } keys %file_hash){
print "<li> " . "<a href=\"/zzz/mirror/" . $file_name . "\">" .
$file_name . "</a><br>";
}

# What I now want to do: for every file name I want to then print each
line in the array named finds.

foreach my $file_name ( sort { $file_hash{ $b }{ mod_time } <=> $file_hash{ $a
}{ mod_time } } keys %file_hash ) {
print qq[<li> <a href="/zzz/mirror/$file_name">$file_name</a><br>];
print qq[@{$file_hash{$file_name}{finds}}];
}




John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top