From: "shadow52" <ras.collec...@gmail.com>

Hello Everyone,

I have finally hit my max times of banging my head on the best way to
parse some data I have like the following below:

name = "Programming Perl"
distributor = "O'Reilly"
pages = 1077
edition = "2nd"
Authors = "Larry Wall
Tom Christiansen
Jon Orwant"

The last line is giving me some trouble it has three newline seprators
which stops me from being able to use a split function like the
following:



You can do something like:

use strict;

my $content = do { local $/; <DATA> };

my %elements = $content =~ /^\s*([^=]+)\s*=\s*"([^"]+)/gsm;

use Data::Dump 'pp';print pp \%elements;

__DATA__
name = "Programming Perl"
distributor = "O'Reilly"
pages = 1077
edition = "2nd"
Authors = "Larry Wall
Tom Christiansen
Jon Orwant"

This will print:

{
 "Authors "     => "Larry Wall\nTom Christiansen\nJon Orwant",
 "distributor " => "O'Reilly",
 "edition "     => "2nd",
 "name "        => "Programming Perl",
}

The important line is:

my %elements = $content =~ /^\s*([^=]+)\s*=\s*"([^"]+)/gsm;

It gets everything from the beginning of the line (but not the eventual spaces at the beginning of the line), until the first "=" sign, as the key for the hash, and the value is everything what's not a '"' char between the first " char and the next " char, as the value for that key.

Where you have a value with more lines, it will remain the same, and then you will be able to split it by those line endings if you will need that.

Octavian


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to