0

I have a script that looped over to get the values ​​$mac and $snr. The result of the loop is as follows

133.3133.5132.9133.1132.9131.8234.4233.7234.7230.3232.3230.8331.9332.0331.6331.6330.9330.2 itd..... 9437.39437.09436.79436.89435.19433.09535.29535.

result from $mac is bolt result form $snr is next

how create %hash where result will be

  my %hash = ( mac1 => [’snr1', ‘snr2', ‘snr3', ‘snr4', ‘snr5', ‘snr6'],
                 mac2 =>  [’snr1', ‘snr2', ‘snr3', ‘snr4', ‘snr5', ‘snr6'],
                 mac3 => [’snr1', ‘snr2', ‘snr3', ‘snr4', ‘snr5', ‘snr6'],
                 );

result with value

 my %hash = ( 1 => [’33.3', ‘33.5', ‘32.9', ‘33.1', ‘32.9','31.8' ‘'],
             2 =>  [’34.4', ‘33.7', ‘34.7', ‘30.3', ‘32.3', ‘30.8'],
             3 => [’31.9', ‘32.0', ‘31.6', ‘31.6', ‘snr5', ‘snr6'],
             );

I can put only part code: I can put only part code:

my $db_snr = DBI->connect("DBI:mysql:$baza:$host", $user, $pass, { RaiseError=>'0', AutoCommit=>'1'} );
        if(! $db_snr){
                print LOG  "Can't connect to mysql serwer at $host, skipping!\n";
                exit 0;
        }
foreach $upstrim (sort keys %interface){
        $typ = $interface{$upstrim}[50];
        $mac=$interface{$upstrim}[0];
        $mac=~ s/MAC//;
        print($snr)
        print($mac) # here I woul like create hash witch array
   if(($typ eq 'u') && ($mac =~ m/^\d/)){
        my $snr = $interface{$upstrim}[30];

        if (($snr < 11) && ($snr > 0)) {
                print LOG "Test SNR nadajcy $cmts:$mac:$snr:$typ\n";
                $db_snr->do("insert into snr values ('$cmts','$mac',NOW(),NOW(),'$snr','1','0','$upstrim') ON DUPLICATE KEY update snr='$snr' ,data_last=NOW(), licznik=licznik+1 ") or warn "Can't prepare: $DBI::errstr";
        }
   }
}
$db_snr->disconnect;

close(LOG);
13
  • What is 133.3133.5132.9133.1132.9131.8234.4233.7234.7230.3232.3230.8331.9332.0331.6331.6330.9330.2 itd..... 9437.39437.09436.79436.89435.19433.09535.29535.? Do you mean to say that all the numbers come mashed together into one long line? Why don't you just show the code you are using? Commented Aug 8, 2022 at 12:22
  • Yes, when I print($mac) I have only result Commented Aug 8, 2022 at 12:26
  • Show the code.. Commented Aug 8, 2022 at 12:27
  • Yes, together into long line if I print $mac I have reuslt 111111222222333333444444555555666666777777888888999999101010101010111111111111121212121212131313131313 itd. Commented Aug 8, 2022 at 12:36
  • if I print $snr I have 33.433.533.133.232.931.834.534.834.534.134.132.731.932.131.931.831.530.631.031.331.131.131.030.133.733.733.533.533.332.932.632.732.532.332.031.334.734.8 Commented Aug 8, 2022 at 12:37

1 Answer 1

4

You can just loop over the values, split the line, then use a push statement to add numbers to the hash value, like below. Note that I am using the infile handle DATA for simplicity here, you have to use your own file handle, or the <> operator.

use strict;
use warnings;
use Data::Dumper;

my %hash;
while (<DATA>) {
    my ($mac, $snr) = split;
    push @{ $hash{$mac} }, $snr;
}
print Dumper \%hash;

__DATA__
1 33,3 
1 33.2
1 33.3
1 32.7
1 32.9
1 32.5
1 31.7
2 34.4
2 34.9
2 34.6
2 34.3
2 33.5
2 30.8
3 31.9
3 32.0
3 31.8
3 31.7
3 31.4
3 30.4
95 34.8
96 30.6
96 31.8
96 33.4
96 34.2
96 34.0
96 29.5

Output:

$VAR1 = {
          '3' => [
                   '31.9',
                   '32.0',
                   '31.8',
                   '31.7',
                   '31.4',
                   '30.4'
                 ],
          '1' => [
                   '33,3',
                   '33.2',
                   '33.3',
                   '32.7',
                   '32.9',
                   '32.5',
                   '31.7'
                 ],
          '2' => [
                   '34.4',
                   '34.9',
                   '34.6',
                   '34.3',
                   '33.5',
                   '30.8'
                 ],
          '96' => [
                    '30.6',
                    '31.8',
                    '33.4',
                    '34.2',
                    '34.0',
                    '29.5'
                  ],
          '95' => [
                    '34.8'
                  ]
        };
Sign up to request clarification or add additional context in comments.

3 Comments

the problem is that the values ​​of both variables are obtained in a loop and so the variable mac goes every 6 snr values ​​up to 96 –
such values ​​are created in the loop 133.3133.5132.9133.1132.9131.8234.4233.7234.7230.3232.3230.8331.9332.0331.6331.6330.9330.2430.9431.3431.1431.1430.9430.
And the difference is...? You're going to have to be more clear about what you mean. Edit the question to add new info if you have it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.