steph_bow has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

When I print data with my code, I get unexpected results

I stock data in a hash table :

when I try to print my hash table, and make a loop on the keys, instead of seeing all the elements that were pushed in the hash, I see only one per key

Could you tell me why please ?

Here is my code

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Cwd; # input files to work on my $file= "$ARGV[0]"; open INFILE, '<', $file, or die "Can't open $file : $!"; my %aircraftID; while (my $line = <INFILE>){ chomp($line); my @Elements = split ';', $line; # the command shift takes away the first element of the table and +stocks it in a variable my $aircraft_id = shift(@Elements); # the element of the first column (the aircraft_id) has been taken + from Elements # a hash table cannot contain lists # that's why a reference to a list is needed # this reference is called by the antislash \ my $row = \@Elements; push @{$aircraftID{$aircraft_id}}, @{$row}; print STDOUT "la row2 vaut @{$row}\n"; } close INFILE; print STDOUT "\n"; foreach my $cle (keys %aircraftID){ print STDOUT "the value of $cle is : \n"; print STDOUT "@{$aircraftID{$cle}}\n"; }

And here is the file input

DAL11;DAL12;DAL;B772;KATL;EGKK;22:05;1325;05:39;339 SHT8K;SHT9Y;SHT;B752;EGPH;EGLL;18:00;1080;18:55;1135 SHT8K;SHT9P;SHT;B752;EGPH;EGLL;17:42;1062;18:37;1117 SHT8K;SHT9B;SHT;B752;EGPH;EGLL;10:55;655;11:49;709

Replies are listed 'Best First'.
Re: reference and hash
by moritz (Cardinal) on Aug 22, 2007 at 09:41 UTC
    At first glance I don't see your error, but why are you making things so difficult with referencing and derefencing?

    my $id = shift @Elements; push @{$aircraftID{$id}}, @Elements;

    should do the trick.

Re: reference and hash
by FunkyMonk (Bishop) on Aug 22, 2007 at 09:43 UTC
    This is the output I get from your code:

    la row2 vaut DAL12 DAL B772 KATL EGKK 22:05 1325 05:39 339 la row2 vaut SHT9Y SHT B752 EGPH EGLL 18:00 1080 18:55 1135 la row2 vaut SHT9P SHT B752 EGPH EGLL 17:42 1062 18:37 1117 la row2 vaut SHT9B SHT B752 EGPH EGLL 10:55 655 11:49 709 the value of SHT8K is : SHT9Y SHT B752 EGPH EGLL 18:00 1080 18:55 1135 SHT9P SHT B752 EGPH EGL +L 17:42 1062 18:37 1117 SHT9B SHT B752 EGPH EGLL 10:55 655 11:49 709 the value of DAL11 is : DAL12 DAL B772 KATL EGKK 22:05 1325 05:39 339

    Is this not what you want?

      Thanks a lot FynkyMonk

      Well, concerning the first line of results, I only get results from SH9TB.

      For the second line it is OK

      So I can't understand why or is it that my version of Perl is not updated?

      Anyway, thanks

Re: reference and hash
by Razvanica (Novice) on Aug 22, 2007 at 09:57 UTC
    With ActivePerl 5.8 on Windows I get :

    la row2 vaut DAL12 DAL B772 KATL EGKK 22:05 1325 05:39 339
    la row2 vaut SHT9Y SHT B752 EGPH EGLL 18:00 1080 18:55 1135
    la row2 vaut SHT9P SHT B752 EGPH EGLL 17:42 1062 18:37 1117
    la row2 vaut SHT9B SHT B752 EGPH EGLL 10:55 655 11:49 709

    the value of SHT8K is :
    SHT9Y SHT B752 EGPH EGLL 18:00 1080 18:55 1135 SHT9P SHT B752 EGPH EGLL 17:42 1062 18:37 1117 SHT9B SHT B752 EGPH EGLL 10:55 655 11:49 709
    the value of DAL11 is :
    DAL B772 KATL EGKK 22:05 1325 05:39 339

    Which is exactly what the code should do, I think.