Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

File parse

by Anonymous Monk
on Dec 05, 2002 at 18:29 UTC ( [id://217843]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Any idea how to parse this file of 4 columns and populate and array with the data?

DATA: 0x130005d 1.253.54.1_MIB 0x230f0 SNMP 0x130009c 0x1006b SnmpPif ($MHandle,$MName,$MTypeHnd,$MTypeName)=split(/\s/g,$_);
Doesn't seem to work with the empty 2nd column

Replies are listed 'Best First'.
Re: File parse
by myocom (Deacon) on Dec 05, 2002 at 18:41 UTC

    This is a perfect job for unpack. The following test code should prove helpful:

    use strict; while (<DATA>) { chomp; my @array = unpack("A12A16A11A*", $_); # @array has 4 elements now print map("'$_'\n", @array); } __DATA__ 0x130005d 1.253.54.1_MIB 0x230f0 SNMP 0x130009c 0x1006b SnmpPif
    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
Re: File parse
by BrowserUk (Patriarch) on Dec 05, 2002 at 18:49 UTC

    Effectively you are passing fixed length records. Try unpack.

    #! perl -slw use strict; while (<DATA>) { my @data = unpack 'A9 xx A14 xx A7 xxx A*', $_; { local $,='|', print @data; } } __DATA__ 0x130005d 1.253.54.1_MIB 0x230f0 SNMP 0x130009c 0x1006b SnmpPif # Gives C:\test>217843 0x130005d|1.253.54.1_MIB|0x230f0|SNMP 0x130009c||0x1006b|SnmpPif C:\test>

    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: File parse
by fruiture (Curate) on Dec 05, 2002 at 18:39 UTC

    Yep, this won't work if the file format is defined to have whitespace-seperated columns.

    However, if you change the definition, it woll work. For example you define the columns by starting-position and width.

    my @columns = ( [ 0 , 9 ], # position,width [ 13, 14], # ... ); while( <$the_file> ){ chomp; my @data = (); for my $spec ( @columns ){ push @data , substr($_,@$spec) } print join('|',@data),"\n"; }

    Programming is a question of definition

    --
    http://fruiture.de

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://217843]
Approved by myocom
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-24 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found