Hi, Nathalie

You could try the attached code.

Best

gil

On 09/09/16 12:54, Nathalie Conte wrote:
> Hello, 
> I have a question about making a calculation within a loop
> 
> I have a hash of hashes
> ##############
> #!/usr/bin/perl
> use strict;
> use warnings;
>  
> use Data::Dumper qw(Dumper);
>  
> my %grades;
> $grades{"Foo "}{1}   = 97;
> $grades{"Foo "}{2}    = 107;
> $grades{"Peti "}{1}   = 88;
> $grades{"Peti "}{3}  = 89;
> $grades{"Peti "}{4}          = 99;
>  
> print Dumper \%grades;
> print "----------------\n";
>  
> foreach my $name (  keys %grades) {
>     foreach my $subject (sort {$a <=> $b} keys %{ $grades{$name} }) {
>         print "$name, $subject: $grades{$name}{$subject}\n";
>     }
> }
> 
> ##############
> output is 
> $VAR1 = {
>           'Peti ' => {
>                        '4' => 99,
>                        '1' => 88,
>                        '3' => 89
>                      },
>           'Foo ' => {
>                       '1' => 97,
>                       '2' => 107
>                     }
>         };
> ----------------
> Peti , 1: 88
> Peti , 3: 89
> Peti , 4: 99
> Foo , 1: 97
> Foo , 2: 107
> ###############
> Now, what I would like to achieve:
> I want to make a calculation, in each $name (Peti and Foo), calculate:
> for Peti:
> first line : no action
> second line -(minus) 1st line:
> print subject 3-1=2, 89-88=1
> third line - 2nd line:
> print subject 4-3=1, 99-89=10
> 
> for foo:
> first line : no action
> second line -(minus) 1st line:
> print subject 2-1=2, 107-97=10
> 
> 
> Many thanks for any advice
> Best
> Nathalie
> 
> 

Attachment: q.pl
Description: Perl program

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to