CyberJacob CyberJacob - 6 months ago 8
Perl Question

Perl array not saving all variables

I have a Perl program that's parsing data from an external program and saving it to an array. However, some of the data isn't being saved and comes back blank when trying to retrieve it later.

Here's the code parsing the data, which comes through as

@packetData
:

if(@packetData[0] ne ""){
if(!$detectedClient{"@packetData[0]"}) {
my $rawSignal = average(@packetData[2]);
my $distance = 10**((27.55-(20*logten(2437))+abs($rawSignal))/20);
my @newClient = ($rawSignal, # Signal (dBm)
1, # Count
@packetData[0], # Source MAC
time(), # Last seen
$distance); # Distance (m)
$detectedClient{"@packetData[0]"} = [@newClient];
$uniqueClient++;
print "++ New probe request from @packetData[0] [$rawSignal dBm, $distance m]\n";
} else {
$detectedClient{"@packetData[0]"}[1]++;
$detectedClient{"@packetData[0]"}[3] = time();
}
}


The
print
statement shows the signal and distance fine, but trying to show it from the
detectedClient
array gives with the code below gives a blank space:

for $key2 ( keys %detectedClient) {
#Signal, Count, MAC, Time
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($detectedClient{$key2}[3]);
my $lastSeen = sprintf("%04d/%02d/%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
print STDOUT sprintf("!! %-20s %10s %-20s\n", $detectedClient{$key2}[2], $detectedClient{$key2}[1], $lastSeen, $detectedClient{$key2}[0], $detectedClient{$key2}[4]);
}


Any ideas why this is happening?

Answer

The signal and the distance aren't printed because your pattern ("!! %-20s %10s %-20s\n") doesn't reference them (the 4th and 5th values). Fix:

printf("!! %-20s %5s %-20s %20s %20s\n",
    $detectedClient{$key2}[2],
    $detectedClient{$key2}[1],
    $lastSeen,
    $detectedClient{$key2}[0],
    $detectedClient{$key2}[4],
);

Tested using the following: (Had to change for $key2 to for my $key2 too)

use strict;
use warnings qw( all );

use List::Util qw( sum );

sub average { sum(@{ $_[0] })/@{ $_[0] } }

sub logten { log($_[0])/log(10) }

my @packetData = ("foo", undef, [ 4, 5 ]);

my %detectedClient;
my $uniqueClient;
if(@packetData[0] ne ""){
    if(!$detectedClient{"@packetData[0]"}) {
        my $rawSignal = average(@packetData[2]);
        my $distance  = 10**((27.55-(20*logten(2437))+abs($rawSignal))/20);
        my @newClient = ($rawSignal,   # Signal (dBm)
                       1,              # Count
                       @packetData[0], # Source MAC
                       time(),         # Last seen
                       $distance);     # Distance (m)
        $detectedClient{"@packetData[0]"} = [@newClient];
        $uniqueClient++;
        print "++ New probe request from @packetData[0] [$rawSignal dBm, $distance m]\n";
    } else {
        $detectedClient{"@packetData[0]"}[1]++;
        $detectedClient{"@packetData[0]"}[3] = time();
    }
}

for my $key2 ( keys %detectedClient) {
    #Signal, Count, MAC, Time
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($detectedClient{$key2}[3]);
    my $lastSeen = sprintf("%04d/%02d/%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
    printf("!! %-20s %5s %-20s %20s %20s\n",
        $detectedClient{$key2}[2],
        $detectedClient{$key2}[1],
        $lastSeen, $detectedClient{$key2}[0],
        $detectedClient{$key2}[4],
    );
}

Output:

Scalar value @packetData[0] better written as $packetData[0] at a.pl line 18.
Scalar value @packetData[0] better written as $packetData[0] at a.pl line 19.
Scalar value @packetData[2] better written as $packetData[2] at a.pl line 20.
Scalar value @packetData[0] better written as $packetData[0] at a.pl line 24.
Scalar value @packetData[0] better written as $packetData[0] at a.pl line 27.
Scalar value @packetData[0] better written as $packetData[0] at a.pl line 29.
Scalar value @packetData[0] better written as $packetData[0] at a.pl line 31.
Scalar value @packetData[0] better written as $packetData[0] at a.pl line 32.
++ New probe request from foo [4.5 dBm, 0.0164302613184467 m]
!! foo                      1 2016/05/16 15:19:38                   4.5   0.0164302613184467

Next time, please provide a minimal, runnable demonstration of the problem.