Advertisement
kijato

hash_&_array_reference.pl

Jun 26th, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.29 KB | None | 0 0
  1.  
  2. use strict;
  3. use locale;
  4. use Data::Dumper;
  5.  
  6. open FH,$ARGV[0]||die"$!\n";
  7.  
  8. my @AoH = {};
  9. my $rec = {};
  10.  
  11. while ( <FH> ) {
  12.     chomp;
  13.     #for $field ( split(/,/ ) {
  14.         #($key, $value) = split /,/, $field;
  15.         my ($key, $value) = split /,/;
  16.         push @{$rec->{$key}}, $value;
  17.     #}
  18.     #push @AoH, $rec;
  19. }
  20.  
  21. print Dumper $rec;
  22.  
  23. #@AoH = sort keys $rec; # Experimental keys on scalar is now forbidden at hash_&_array_reference.pl line 23.
  24. @AoH = sort keys %$rec;
  25.  
  26. print Dumper \@AoH;
  27.  
  28. #while ( <FH> ) {
  29. #    push @AoH, { split /[\s=]+/ };
  30. #}
  31.  
  32. __END__
  33.  
  34. #
  35. # Input:
  36. #
  37. aaaa,4645
  38. aaaa,7317
  39. aaaa,7155
  40. aaaa,7674
  41. bbbb,183
  42. bbbb,8830
  43. bbbb,8227
  44. ccc,4065
  45. ccc,9486
  46. ccc,5968
  47. ccc,7428
  48. ccc,6868
  49.  
  50. #
  51. # Result:
  52. #
  53. $VAR1 = {
  54.           'ccc' => [
  55.                      '4065',
  56.                      '9486',
  57.                      '5968',
  58.                      '7428',
  59.                      '6868'
  60.                    ],
  61.           'bbbb' => [
  62.                       '183',
  63.                       '8830',
  64.                       '8227'
  65.                     ],
  66.           'aaaa' => [
  67.                       '4645',
  68.                       '7317',
  69.                       '7155',
  70.                       '7674'
  71.                     ]
  72.         };
  73. $VAR1 = [
  74.           'aaaa',
  75.           'bbbb',
  76.           'ccc'
  77.         ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement