1
votes

I have a hash with 5 keys, each of these keys have 5 values

foreach $a(@mass){
   if($a=~some regex){
       @value=($1,$2,$3,$4,$5);
       $hash{"keysname$c"}="@value";
       c++;
    }
}

Each scalar is a value of different parameters , I have to determinate the highest value of the first array for the all keys in hash

Edit:

Code must compare first value of key1 with first value of key2, key3...key5 and print the highest one

3
Your example doesn't show five keys, nor array as hash value.mpapec
This code is in the loop which collects values from regexpt and creates array with those collected values, so my loop creates 5 keys with 5 values in the %hashPYPL
This code does not. It creates one key, with a string as value.TLP
The actual code is $hash{"name$c"}="@value"; c++;.. So it doesPYPL
Yes, now it does, but only because you changed it. "@value" is still not an array, though, it is a string. If you assigned \@value then it would be an array, but then you would need to use a lexically scoped array.TLP

3 Answers

3
votes

If I understand your question correctly (and it's a little unclear) then I think you want something like this:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use List::Util 'max';

my (@data, @max);

while (<DATA>) {
  chomp;
  push @data, [split];
}

for my $i (0 .. $#{$data[0]}) {
  push @max, max map { $_->[$i] } @data;
}

say "@max";

__DATA__
93 3 26 87 7
66 96 46 77 42
26 3 71 64 91
31 27 14 40 86
82 72 71 34 7
3
votes

This will print max value for structure like

my %hash = ( k1 => [6,4,1], k2 => [16,14,11] );

use List::Util qw(max);

# longest array
my $n = max map $#$_, values %hash;

for my $i (0 .. $n) {

  my $max = max map $_->[$i], values %hash;
  print "max value on position $i is $max\n";
}

and for strings,

my %hash = ( k1 => "6 4 1", k2 => "16 14 11" );

use List::Util qw(max);

# longest array
my $n = max map $#{[ split ]}, values %hash;

for my $i (0 .. $n) {

  my $max = max map [split]->[$i], values %hash;
  print "max value on position $i is $max\n";
}
1
votes

try this

    map {push @temp, @{$_}} values %hash;
    @desc_sorted= sort {$b <=> $a} @temp;
    print $desc_sorted[0],"\n";

map will consolidate all lists to a single list and sort will sort that consolidated array in descending order.