Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^3: Parsing issue

by vitoco (Friar)
on Sep 10, 2009 at 19:30 UTC ( #794644=note: print w/replies, xml ) Need Help??

in reply to Re^2: Parsing issue
in thread Parsing issue

Then, you can forget this trick and try some of the other ideas I gave, like parsing each row at a time and cut each record at fixed columns or use some other regexp to get field values.

#!perl use strict; use warnings; use Data::Dumper; my %hash = (); my $string = ""; while(<DATA>) { if (/^(VLAN)\s+:\s(.*?)\s+(Status)\s+:\s(.*?)\s*$/) { ($hash{$1}, $hash{$3}) = ($2, $4); } elsif (/^(FID)\s+:\s(.*?)\s+(Name)\s+:\s(.*?)\s*$/) { ($hash{$1}, $hash{$3}) = ($2, $4); } elsif (/^(VLAN Type):\s(.*?)\s+(Last change):\s(.*?)\s*$/) { ($hash{$1}, $hash{$3}) = ($2, $4); } elsif (/^\s((Forbidden )?(Egress|Untagged) Ports):\s*$/) { $string = $1; } elsif ($string) { /^\s*(.*?)\s*$/; ($hash{$string}, $string) = ($1, ""); } } print Dumper( \%hash ); __DATA__ VLAN : 1 Status : Enabled FID : 1 Name : Some VLAN with spaces VLAN Type: Permanent Last change: 2009-08-31 16:48:45 Egress Ports: host.0.1 Forbidden Egress Ports: ge.3.39 Untagged Ports: host.0.1
$VAR1 = { 'Last change' => '2009-08-31 16:48:45', 'Status' => 'Enabled', 'Forbidden Egress Ports' => 'ge.3.39', 'FID' => '1', 'VLAN' => '1', 'Untagged Ports' => 'host.0.1', 'Egress Ports' => 'host.0.1', 'Name' => 'Some VLAN with spaces', 'VLAN Type' => 'Permanent' };

Here, I used "(.*?)\s*" to get a trimmed value of any type field, but you should change each of them to a specific pattern for dates, integers...

Update: Forgot one field...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://794644]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2018-01-20 01:19 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.