Perl Mail User wrote:
>
> Hello All,
Hello,
> I have a question - I am new to perl and I would like to
> parse a file that I have and gather some data from that
> for use. What I am looking to do is to search the file
> for a set of { } once I reach that set of { } I want to
> parse the data in the brackets.
>
> Example:
>
> Device Capacity
> {
> Cylinders : 18590
> Tracks : 278850
> 512-byte Blocks : 17846400
> MegaBytes : 8714
> KiloBytes : 8923200
> }
>
> Each set of brackets, { }, has its unique title or header as
> show above. I want to find the unique title and then parse
> the data within the { } and then move on to the next unique
> header / title and do the same thing. So from the above I
> would like to find "Device Capacity" and then within the { }
> assign each sub catagory its appropriate value.
>
> So what I think that I would do is to create a HoH. But the
> question is how do I ensure that I only grab the data within
> the brackets.
>
> What the HoH would look like is this, if I am correct:
>
> DeviceCapacity{Cylinders} = 18590
> DeviceCapacity{Tracks} = 278850
> DeviceCapacity{512-byteBlock} = 278850
> DeviceCapacity{MegaBytes} = 8714
> DeviceCapacity{KiloBytes} = 8714
>
> So I just need some assistance on how to parse the data from within
> the brackets and assign that data to the HoH.
It seems simple enough. :-)
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
$/ = '}';
my %data;
while ( <> ) {
next unless my ( $title, $data ) =
/^\s*(.*?\S)\s*{\s*(?s:(.*?\S))\s*}/m;
$data{ $title } = { split /\s*[:\n]\s*/, $data };
}
print Dumper \%data;
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>