Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

RESOLVED - parse a file TXT similar to XML

by x-lours (Sexton)
on Mar 30, 2022 at 07:52 UTC ( [id://11142528]=perlquestion: print w/replies, xml ) Need Help??

x-lours has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am looking for a way to parse a file TXT with the underlying format. A office colleague praise for Ruby he uses to parse the file in " oneline" .

File.readlines("/home/user/objet_zone.don").reject{|l|}.map{|l|lf=l.st +rip;(lf.size> 0 && lf[0..1]!="--") ? lf : nil}.compact.inject([]){|ar +y,l|ary.last << l if (l=~/^[^=]+=>\s*debut/ && ary<<[])..(l=~/^[^=]+= +>\s*fin/);ary}.map{|ary|ary[1..-2].map{|e|e.split('=>').map{|f|f.stri +p.gsub('"','')}}.to_h}

the aim is to transform in hash every object (array of hash in his maner) for using them in the rest of the script.

he teaches me that the interval cames from Perl but i can't afford to traduce it ...

if (l=~/^[^=]+=>\s*debut/ && ary<<[])..(l=~/^[^=]+=>\s*fin/)

could you help me to proove him Perl is as efficient as Ruby ? (even if it is not a "oneline")

Thanks in advance

example of the file TXT :

objet => debut
index => 1
a => "premiere valeur"
...
z => "dernier mot"
objet => fin
...
objet => debut
index => 77
a => "autre valeur"
...
z => "aurai-je le dernier mot ?"
objet => fin

sorry for my poor english.

Replies are listed 'Best First'.
Re: parse a file TXT similar to XML
by kcott (Archbishop) on Mar 31, 2022 at 05:42 UTC

    G'day x-lours,

    You seem very impressed with 'parse the file in "oneline"'. It is not impressive at all. It's exceptionally difficult to read, a maintenance nightmare, and extremely error-prone. I strongly recommend you avoid code like this.

    You said you wanted to use the result in 'the rest of the script'; for that, you'd want to put the code in a subroutine; possibly in a module for reuse in other scripts. Below, I present a technique for getting the exact result you want: it's a standalone solution which you can adapt for a subroutine or module; it is very straightforward code that's easy to read and maintain; it has a basic sanity check and reports I/O errors.

    Here's pm_11142528_parse_file.pl:

    #!/usr/bin/env perl use strict; use warnings; use autodie; die "Usage: $0 input_file\n" unless @ARGV; my $in_file = $ARGV[0]; my $result = []; my $block_start = 'objet => debut'; my $block_end = 'objet => fin', my $rec_skip = '...'; { open my $fh, '<', $in_file; while (<$fh>) { chomp; next if $_ eq $rec_skip or $_ eq $block_end; if ($_ eq $block_start) { push @$result, {}; next; } my ($key, $value) = split /\s*=>\s*/; $value =~ s/^"?(.*?)"?$/$1/; $result->[-1]{$key} = $value; } } use Data::Dump; dd $result;

    Sanity check:

    $ ./pm_11142528_parse_file.pl Usage: ./pm_11142528_parse_file.pl input_file

    I/O exception handling:

    $ ./pm_11142528_parse_file.pl not_a_file Can't open 'not_a_file' for reading: 'No such file or directory' at ./ +pm_11142528_parse_file.pl line 17

    Here's the input data you provided:

    $ cat pm_11142528_parse_file.txt objet => debut index => 1 a => "premiere valeur" ... z => "dernier mot" objet => fin ... objet => debut index => 77 a => "autre valeur" ... z => "aurai-je le dernier mot ?" objet => fin

    A sample run with expected results:

    $ ./pm_11142528_parse_file.pl pm_11142528_parse_file.txt [ { a => "premiere valeur", index => 1, z => "dernier mot" }, { a => "autre valeur", index => 77, z => "aurai-je le dernier mot ?" + }, ]
    'could you help me to proove him Perl is as efficient as Ruby ? (even if it is not a "oneline")'

    It is a common misconception that it is somehow more efficient to write single lines of code that are hundreds of characters long. Writing code without whitespace is also not more efficient; itjustreducesthereadabilityofthecode.

    Use Benchmark to measure the efficiency of your Perl code. I imagine Ruby has something similar which you could use for a comparison (but I've no idea what that might be).

    — Ken

      You seem very impressed with 'parse the file in "oneline"'. It is not impressive at all. It's exceptionally difficult to read, a maintenance nightmare, and extremely error-prone. I strongly recommend you avoid code like this.

      kudos to kcott!

      From On Coding Standards and Code Reviews, three sobering guidelines to keep in mind before trying to outdo an office colleague in a clever one-liner contest:

      • Correctness, simplicity and clarity come first. Avoid unnecessary cleverness.
      • Favour readability over brevity.
      • Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Thanks for advice. I will keep this in mind ;-)

      thank you for the code and the advice.

      you show me that ".." was useless and i appreciate ;-)
Re: parse a file TXT similar to XML -- flip-flop operator
by Discipulus (Canon) on Mar 30, 2022 at 09:19 UTC
    Hello x-lours,

    it is not so clear to me: perhaps you should post an example of the intended final datructure, but..

    if you are trying to understand the .. part, in this case it is named flip-flop operator described among Range-Operators in the perlop

    To help you understand it run the following code

    use strict; use warnings; my $part = 0; while (<DATA>){ chomp $_; if ( $_ =~ /objet => debut$/ .. /objet => fin$/){ print "TRUE\n"; if ( /objet => debut$/ ){ next; # we skip the opening tag } elsif ( /objet => fin$/ ){ $part++; # we increment our index $part on closing tag } else{ print "\tPART $part: [$_]\n"; # here is the meat: do what +you need } } else{ print "FALSE for line: [$_]\n"; } } __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

    ..and you will see how it works:

    TRUE TRUE PART 0: [index => 1] TRUE PART 0: [a => "premiere valeur"] TRUE PART 0: [z => "dernier mot"] TRUE FALSE for line: [ALIEN LINE AFTER fin] TRUE TRUE PART 1: [index => 77] TRUE PART 1: [a => "autre valeur"] TRUE PART 1: [z => "aurai-je le dernier mot ?"] TRUE

    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.

      thank you for the reply.

      i think that learning "flip flop" (knew knowledge for me) will help me to give a Perl answer to my colleague ;-)

      the final array is sort of

      [{index=>1, a => 'premiere valeur' ... , z => 'dernier mot'}, ..., {index=>77, a => 'autre valeur' ... , z => 'aurais-je le dernier mot'}]
        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.

        You may find Flipin good, or a total flop? helpful. The flip flop operator family is home to dragons.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        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
Re: parse a file TXT similar to XML
by tybalt89 (Monsignor) on Mar 30, 2022 at 10:38 UTC

    Guessing...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11142528 use warnings; my @AoH = do { local $/ = "objet => fin\n"; map { s/^objet => .*\n//gm; +{ split /"?\n(?:\.{3}\n)?| => "?/} } <D +ATA>; }; use Data::Dump 'dd'; dd \@AoH; __DATA__ objet => debut index => 1 a => "premiere valeur" b => 'better example required" z => "dernier mot" objet => fin objet => debut index => 77 a => "autre valeur" y => 'better example required" z => "aurai-je le dernier mot ?" objet => fin

    Outputs:

    [ { a => "premiere valeur", b => "'better example required", index => 1, z => "dernier mot", }, { a => "autre valeur", index => 77, y => "'better example required", z => "aurai-je le dernier mot ?", }, ]

      Exactly what i need !

      Thanks a lot

      post RESOLVED

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-19 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found