Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Problem with regex ...

by jandrew (Chaplain)
on Dec 26, 2012 at 21:06 UTC ( [id://1010422]=note: print w/replies, xml ) Need Help??


in reply to Re: Problem with regex ...
in thread Problem with regex ...

A slightly more extensible version with a complete dispatch array implementation matched to named capture regular expressions for the columns. (Use the same data file.)

#!perl my $table_spliter = qr/^Mod\s*((Ports)|(Sub))/; my @columns = ( [ qw( SLOT PORTAS DESC Model Sw ) ], [ qw( Slot Desc Model Sw Hw Status ) ], ); my @column_spliters = ( qr/ (?<$columns[0]->[0]>.{4}) (?<$columns[0]->[1]>.{6}) (?<$columns[0]->[2]>.{39}) (?<$columns[0]->[3]>.{19}) (?<$columns[0]->[4]>.{10,11}) /sxm, qr/ (?<$columns[1]->[0]>.{5}) (?<$columns[1]->[1]>.{28}) (?<$columns[1]->[2]>.{19}) (?<$columns[1]->[3]>.{12}) (?<$columns[1]->[4]>.{8}) (?<$columns[1]->[5]>.{2,7}) /sxm, ); my @pre_strings = ( '', '<> ' ); my $position; while(<DATA>){ my $line = $_; chomp $line; my @types = $line =~ $table_spliter; for my $x ( 1..$#types ){ if( $types[$x] ){ $position = $x - 1;#Handle '|' in $table_spliter last; } } $line =~ $column_spliters[$position]; my %line_hash = %+; if( !@types and ( keys %line_hash ) and ( %line_hash )[1] !~ /^-/ ){ for my $item ( @{$columns[$position]} ){ my $value = $line_hash{$item}; $value =~ s/^\s*(.*?)\s*$/$1/; print $pre_strings[$position] . $item . ": " . $value . "\n"; } }else{ print "Header/Other: $line\n"; } } close(DATA);

Update: removed un-needed variable declarations, still requires perl 5.10 or higher

Log In?
Username:
Password:

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

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

    No recent polls found