Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Parsing issue

by vitoco (Pilgrim)
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...


Comment on Re^3: Parsing issue
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-07-26 15:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls