Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Create Arrays On-the-Fly

by fullermd (Priest)
on Aug 27, 2009 at 04:39 UTC ( #791523=note: print w/replies, xml ) Need Help??


in reply to Re^2: Create Arrays On-the-Fly
in thread Create Arrays On-the-Fly

Of course, if you're always looking up data by MAC, it could make more sense to structure it as a giant hash, keyed by the MAC, of hash (refs) of the data. That would make looking up particular MAC's faster and a bit cleaner. It may be easier to read too, depending on your own preferences.

#!/usr/bin/env perl5 use strict; use warnings; my %clients; my $curmac; while(<DATA>) { chomp; # Assume a format of "Type Data" my ($type, $data) = split / /; # If it's a MAC address, assume later lines refer to this MAC if($type eq "MAC") { $curmac = $data; next; } # RSSI and SNR just get saved $clients{$curmac}{RSSI} = $data if $type eq "RSSI"; $clients{$curmac}{SNR} = $data if $type eq "SNR"; } # Show it use Data::Dumper; print Dumper \%clients; # Find a given entry sub showmacsnr { my $mac = shift; my $ent = $clients{$mac}; if($ent) { print "MAC $mac has SNR $ent->{SNR}\n"; } else { print "Can't find MAC $mac\n"; } } showmacsnr("ba:98:76:54:32:10"); showmacsnr("thi:is:is:nt:re:al"); # Sample data __DATA__ MAC 01:23:45:67:89:ab RSSI 12 SNR 18 MAC ba:98:76:54:32:10 RSSI 7 SNR 3
% ./tst.pl $VAR1 = { 'ba:98:76:54:32:10' => { 'SNR' => '3', 'RSSI' => '7' }, '01:23:45:67:89:ab' => { 'SNR' => '18', 'RSSI' => '12' } }; MAC ba:98:76:54:32:10 has SNR 3 Can't find MAC thi:is:is:nt:re:al

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://791523]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2020-11-24 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?