On 10/07/2011 10:36, shadow52 wrote:
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:

my ( $name, $distributor, $pages, $edition, $Authors ) = split( "\n",
$stanzas);

When I print them out I get the following:

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


So my last to authors are left out.

I have even tried a split like the following:

split( "(\"\$\n|\n", $stanzas); This still did work as I though it
would it seems I am just missing one little thing.


What I was hoping for is where on the net or in a book that I would
need to read to get this to work. I assume I will need to use a regex
in the split command I am hoping for a little guidance in the right
direction of where I need to go.

To do what you describe, I suggest that you use a regex to match either
form of record and look for all ocurrences in the file. The program
below shows my point.

However, I suspect that there is more processing to be done after the
data has been separated, and this may well be better donw while
accumulating each subrecord in a different way.

HTH,

Rob


use strict;
use warnings;

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

my @data = $data =~ /(\w+ \s*=\s* (?: "[^"]*" | .*? ) ) \s*$/mgx;

use Data::Dumper;
print Data::Dumper->Dump([\@data], ['*data']);

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


**OUTPUT**


@data = (
          'name = "Programming Perl"',
          'distributor = "O\'Reilly"',
          'pages = 1077',
          'edition = "2nd"',
          'Authors = "Larry Wall
Tom Christiansen
Jon Orwant"'
        );

--
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