Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: regex help

by ph0enix (Friar)
on Sep 16, 2009 at 13:20 UTC ( [id://795606]=note: print w/replies, xml ) Need Help??

in reply to regex help

Try this if you need to use regexp ;-) Following code will create two-dimensional array from your data. Header is skipped and line with dashes (delimiter between header and data) is used for regexp pattern creation.

#!/usr/bin/perl -w # use strict; use warnings; use Data::Dumper; my @data; my $data = 0; my $line; my @size; my $re; while ($line = <DATA>) { chomp $line; # remove EOL next if ($line =~ /^\s*$/); # skip empty lines if any if ($data) { # process data using regexp push @data, [ map { s/(^\s+|\s+$)//; $_ } ($line =~ /$ +re/) ]; } elsif ($line =~ /^-/) { # create regexp from pattern $data = 1; # end of header @size = map { length } split(/\s+/, $line); pop @size; $re = '^' . join(' ', map { "(.{$_})" } @size) . ' + (.*)'; } } print Data::Dumper::Dumper(\@data); __DATA__ Port Alias Oper Admin Speed Duplex Type (truncated) Status Status (bps) ------------ ---------------- -------- ------- ------ ------- -------- +---------- vlan.0.4094 up up other host.0.1 up up 10.0M other ge.1.1 down up 10.0M half 10-t + rj45 ge.1.2 down up 10.0M half 10-t + rj45 ge.1.3 down up 10.0M half 10-t + rj45 ge.1.4 down up 10.0M half 10-t + rj45 ge.1.5 down up 10.0M half 10-t + rj45

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-06-19 13:39 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.