Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Right, but you don't have to presize it. Just push entries onto it as you parse them.

For instance, to invent a stupid-simple data format, and demonstrate parsing it and doing a thing or two with the data once parsed:

#!/usr/bin/env perl5 use strict; use warnings; my @clients; my %tmp; while(<DATA>) { chomp; # Assume a format of "Type Data" my ($type, $data) = split / /; # If it's a MAC address, save off our pre-existing record (if any) +, # and start a new one. if($type eq "MAC") { if(keys %tmp) { # Have to unroll a copy here, otherwise the next line empt +ies # it out. push @clients, { (%tmp) }; %tmp = (); } $tmp{MAC} = $data; } # RSSI and SNR just get saved $tmp{RSSI} = $data if $type eq "RSSI"; $tmp{SNR} = $data if $type eq "SNR"; } # Save the 'last' one if we fell off the end push @clients, \%tmp if keys %tmp; # Show it use Data::Dumper; print Dumper \@clients; # Find a given entry sub showmacsnr { my $mac = shift; my @ba = grep { $_->{MAC} eq $mac } @clients; if(@ba) { my $ent = $ba[0]; 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

So you end up with output like:

% ./tst.pl $VAR1 = [ { 'RSSI' => '12', 'SNR' => '18', 'MAC' => '01:23:45:67:89:ab' }, { 'SNR' => '3', 'RSSI' => '7', 'MAC' => 'ba:98:76:54:32:10' } ]; MAC ba:98:76:54:32:10 has SNR 3 Can't find MAC thi:is:is:nt:re:al

In reply to Re^3: Create Arrays On-the-Fly by fullermd
in thread Create Arrays On-the-Fly by spickles

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2024-04-25 04:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found