If the output is an Array Of Hash, I don't see the need for the flip-flop operator, as cool as that thing is. Taking your DATA verbatim and assuming that 3 dots is the record separator, the code appears to me to be straightforward. This is not "one line", but at least for me, the code is very easy to understand. Please enlighten me if the Ruby complicated thing does something that this does not?

use strict; use warnings; use Data::Dumper; my @structure; my %temp; while (<DATA>) { next unless /\S/; # skip blank lines if (/^\.\.\./) # 3 dots ends a record { push (@structure, {%temp}); %temp=(); } else { chomp; my ($key, $value) = split /\s+=>\s+/,$_; $temp{$key}=$value; } } push (@structure, {%temp}) if (%temp); # possible last record print Dumper \@structure; =Output $VAR1 = [ { 'a' => '"premiere valeur"', 'objet' => 'debut1', 'index' => '1' }, { 'objet' => 'fin', 'z' => '"dernier mot"' }, { 'objet' => 'debut2', 'index' => '77', 'a' => '"autre valeur"' }, { 'z' => '"aurai-je le dernier mot ?"', 'objet' => 'fin' } ]; =cut __DATA__ objet => debut1 index => 1 a => "premiere valeur" ... z => "dernier mot" objet => fin ... objet => debut2 index => 77 a => "autre valeur" ... z => "aurai-je le dernier mot ?" objet => fin
UPDATE: I looked at this thing again and I think I see a possible interpretation error of your DATA on my part. I work with some XML formats and this idea of using a specific key to indicate the start or the end of the record is my opinion, ill advised. Usually XML keys usually are not guaranteed to come in a specific order. However usually there will be clear "end of record" indication. In some files I parse, that is "\n". Here I assumed just a blank line, or end of file.

so here is updated code:

use strict; use warnings; use Data::Dumper; my @structure; my %temp; while (<DATA>) { if (!/\S/) # blank line ends a record { push (@structure, {%temp}) if (%temp); # possible null record %temp=(); } else { chomp; my ($key, $value) = split /\s+=>\s+/,$_; $value =~ tr/"//d; # remove double quotes $temp{$key}=$value; } } push (@structure, {%temp}) if (%temp); # possible last record print Dumper \@structure; =Output $VAR1 = [ { 'a' => 'premiere valeur', 'b' => 'some value', 'index' => '1', 'z' => 'dernier mot', 'objet' => 'fin' }, { 'a' => 'autre valeur', 'index' => '77', 'objet' => 'fin', 'z' => 'aurai-je le dernier mot ?' } ]; =cut __DATA__ objet => debut index => 1 a => "premiere valeur" b => "some value" z => "dernier mot" objet => fin objet => debut index => 77 a => "autre valeur" z => "aurai-je le dernier mot ?" objet => fin

In reply to Re^3: parse a file TXT similar to XML -- flip-flop operator by Marshall
in thread RESOLVED - parse a file TXT similar to XML by x-lours

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.