Rafael Morales wrote:
> Hi.

Hello,

> My trouble is with a value duplicated value on a nested foreach, these are my 
> variables:
> 
> %names=(
>         1=>'NA'
>         2=>'NCW'
>         3=>'NE'
>         4=>'S'
>         5=>'SE'
>         6=>'SP'
>        );
> 
> %hash{'2005-09-19'}{'1'}{'56'}=788;
> %hash{'2005-09-19'}{'1'}{'43'}=540;
> %hash{'2005-09-19'}{'6'}{'23'}=321;
> %hash{'2005-09-19'}{'3'}{'10'}=975;
> 
> And I want an output like this:
> 2005-09-19 - NA - 56 - 788
> 2005-09-19 - NA - 43 - 540
> 2005-09-19 - NCW - 0 - 0
> 2005-09-19 - NE - 10 - 975
> 2005-09-19 - S - 0 - 0
> 2005-09-19 - SE - 0 - 0
> 2005-09-19 - SP - 23 - 321
> 
> I do it by this way:
> 
> my %final;
> my $match = 0;
> foreach my $date(sort keys %hash) {
>   foreach my $num_name(sort {$a <=> $b}(keys %{$hash{$date}}) ) {
>     foreach my $names(sort {$a <=> $b}(keys %names) ) {
>       if ($num_name == $names) {  
>         $match = 1;
>         foreach my $values(sort {$a <=> $b}(keys %{$hash{$date}{$num_state}}) 
> ) {
>            
> $final{$date}{$names{$names}}{$values}=$hash{$date}{$num_state}{$values}; 
>         } #end values
>       } #end if
>       else {$match = 0;}
>     }end names
>     if ($match == 0){
>        $final{$date}{$names{$names}}{0}=0;
>     }#end if
>   }end num_name
> }end date
> 
> However this is what I get:
> 2005-09-19 - NA - 0 - 0     <----- Repeated value :(
> 2005-09-19 - NA - 56 - 788
> 2005-09-19 - NA - 43 - 540
> 2005-09-19 - NCW - 0 - 0
> 2005-09-19 - NE - 10 - 975
> 2005-09-19 - S - 0 - 0
> 2005-09-19 - SE - 0 - 0
> 2005-09-19 - SP - 23 - 321

It looks like you want something like:

for my $date ( keys %hash ) {
    for my $num_name ( keys %{ $hash{ $date } } ) {
        if ( exists $names{ $num_name } ) {
            $hash{ $date }{ $names{ $num_name } }
                = delete $hash{ $date }{ $num_name };
            }
        }
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to