Problems detected in this output:
- It is a very special output format. You can recognize keys and values visually, but is hard to find a pattern for automation. You should probably have to write some code to parse each line in order, one at a time.
- If field names (keys) are always the same, another approach should be to search for them and find the corresponding values from the full form.
- Some values are in a different line than the field name as you said in your post.
- Colon ":" alone cannot be used as a delimiter, because it appears in a timestamp. You already know that!
- What is missing in your approach is a pattern to split multi columns lines, like the first one.
- There are multiple words in some keys.
The last point gave me an idea: Let all the words with exactly one space between them just before a colon be a key. So you have to be sure that a value need many spaces after it not to be considered as part of the key from the next line (trick one). Then, also split everywhere there are many spaces and treat them as another delimiter (trick two).
#!perl
use strict;
use warnings;
use Data::Dumper;
my $string = "";
while(<DATA>) {
$_ =~ s/\n/ /g; # (trick one)
$string .= $_;
}
my %hash = split (/\s*:\s+|\s\s+/, $string); # (trick two)
print Dumper( \%hash );
__DATA__
VLAN : 1 Status : Enabled
FID : 1 Name : DEFAULT VLAN
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
Output:
$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' => 'DEFAULT VLAN',
'VLAN Type' => 'Permanent'
};
You were too close, but now you have to be sure that all possible values of 'VLAN Type' should not be so long to left only one space before 'Last change' field.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.