Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: parse a file TXT similar to XML -- flip-flop operator

by Discipulus (Canon)
on Mar 30, 2022 at 10:48 UTC ( [id://11142540]=note: print w/replies, xml ) Need Help??


in reply to Re^2: parse a file TXT similar to XML -- flip-flop operator
in thread RESOLVED - parse a file TXT similar to XML

Hello again,

you can easily modify the above code to have something like:

use strict; use warnings; use Data::Dumper; my @AoH ; my $current_hash = {}; while (<DATA>){ chomp $_; if ( $_ =~ /objet => debut$/ .. /objet => fin$/){ if ( /objet => debut$/ ){ next; } elsif ( /objet => fin$/ ){ push @AoH, $current_hash; $current_hash = {}; } else{ my ($key,$value) = split /\s+=>\s+/,$_; $current_hash->{ $key } = $value; } } else{ print "unxpected content outside tags in line: [$_]\n"; } } print Dumper \@AoH; __DATA__ objet => debut index => 1 a => "premiere valeur" z => "dernier mot" objet => fin ALIEN LINE AFTER fin objet => debut index => 77 a => "autre valeur" z => "aurai-je le dernier mot ?" objet => fin

The above code use $current_hash as temporary container for key values pairs and need to be reset when fin is encountered. It outputs:

unxpected content outside tags in line: [ALIEN LINE AFTER fin] $VAR1 = [ { 'a' => '"premiere valeur"', 'index' => '1', 'z' => '"dernier mot"' }, { 'z' => '"aurai-je le dernier mot ?"', 'a' => '"autre valeur"', 'index' => '77' } ];

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^4: parse a file TXT similar to XML -- flip-flop operator
by x-lours (Sexton) on Mar 31, 2022 at 07:56 UTC

    Thanks for the explanations

    very helpful to understand the flip-flop tool

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11142540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-16 09:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found