Hi shadow52,

On Sun, 10 Jul 2011 02:36:34 -0700 (PDT)
shadow52 <ras.collec...@gmail.com> 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);
> 

Try looking at the techniques in:

http://perl-begin.org/uses/text-parsing/

Especially look at /g /c and \G :

You can try doing something like (untested):

<CODE>
my $string = slurp($filename);

pos($string) = 0;
my @results;
while (pos($string) < length($string))
{
        if (my ($field_name) = $string =~ m{\G(\w+)\s*=\s*}g))
        {
                my $value;
                if ($string =~ m{\G"}gc)
                {
                        if (($value) = ($string =~ m{\G([^"]+)"\n}gms))
                        {
                                # Everything is OK.
                        }
                        else
                        {
                                die "Cannot match quoted value.";
                        }
                }
                else
                {
                        if (($value) = ($string =~ m{\G(\S+)\n}g)
                        {
                                # Everything is OK.
                        }
                        else
                        {
                                die "Cannot match single-line/non-whitespace
                                value.";
                        }
                }
                push @results, { name => $field_name, value => $value };
        }
        else
        {
                die "Cannot match field name!";
        }
}
</CODE>

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Optimising Code for Speed - http://shlom.in/optimise

JATFM == “Just answer the fabulous man”

Please reply to list if it's a mailing list post - http://shlom.in/reply .

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