Guest User

Untitled

a guest
Jan 18th, 2019
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. a
  2. 2
  3. b
  4. 6
  5. a
  6. 4
  7. f
  8. 2
  9. b
  10. 1
  11. a
  12. 7
  13.  
  14. %hash = {
  15. a => 2,
  16. b => 6,
  17. a => 4,
  18. f => 2,
  19. b => 1,
  20. a => 7,
  21. };
  22.  
  23. a-->7
  24. b-->6
  25. f-->2
  26.  
  27. unless (exists $hash{$key} and $hash{$key} >= $value)
  28. {
  29. $hash{$key} = $value;
  30. }
  31.  
  32. #Add an element to the array of values for this key.
  33. push @{ $hash{$key} }, $value;
  34.  
  35. use List::Util qw/max/;
  36.  
  37. print max @{ $hash{$key} };
  38.  
  39. %info;
  40.  
  41. # A loop reading two lines.
  42. while( my $key = <> ) {
  43. my $value = <>;
  44.  
  45. # Handle the case where there are an odd number of lines.
  46. die "Odd number of lines" unless (defined $value);
  47.  
  48. # Assuming only non-negative values, we just silently want to compare
  49. # keys not seen before as having value 0. See 'perllexwarn' manual page
  50. no warnings 'uninitialized';
  51.  
  52. $info{$key} = $value if $info{$key} <= $value;
  53. }
  54.  
  55. # Dump the result
  56. say "$_ --> $info{$_} for keys %info;
Add Comment
Please, Sign In to add comment