Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Help with parsing a file (updated)

by LanX (Sage)
on May 28, 2022 at 23:03 UTC ( #11144259=note: print w/replies, xml ) Need Help??

in reply to Help with parsing a file


use strict; use warnings; use Data::Dump qw/pp dd/; local $/ = ""; # $INPUT_RECORD_SEPARATOR to +split paragraphs my %res; # result-set while (my $block = <DATA>) { # DATA as file-handle my (%f_num, $prefix); for my $line ( split /\n/, $block ) { my ($k,$v) = split /\s+/, $line; # key=value $f_num{$k} = $v # collect F<num> if $k =~ /^F\d+$/; $prefix = $k # catch <solvent...> = 0 if $k =~ /^solvent/ and $v == 0; # * } $res{"${prefix}_$_"} = $f_num{$_} # copy with prefix for keys %f_num; } pp \%res; # display __DATA__ F001 1.2 F101 3.2 solvent1 0 solvent2 3 F001 2.2 F101 7.2 solvent1 5 solvent2 0

{ solvent1_F001 => 1.2, solvent1_F101 => 3.2, solvent2_F001 => 2.2, solvent2_F101 => 7.2, }

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


*) added $k =~ /^solvent/ and to make it more fault tolerant.

an if-elsif-else chain would be even better to catch errors with unexpected data

) changed from "\n\n" to match longer gaps too


Added comments. If you need further explanation, feel free to ask

Replies are listed 'Best First'.
Re^2: Help with parsing a file (updated)
by Odar (Novice) on May 29, 2022 at 19:36 UTC

    Thank you LanX, works great but I have realised that in my attempt to simplify the example I have missed a key info i.e. the data blocks are not separated by an empty line but by three lines of text with an empty line above and below. Also the blocks can be more than two.I have updated the question.

      Words are not enough, please post a section of your real data that is "long enough" to show all the problems...

        I can post part of the real data file but it will be 2-3 pages long. Could I ask what should I use to add this update to the question i.e. the 'code' or the 'readmore' tag so that it displays properly and follows PerlMonk's best practise?

        I have provided part of the real data file as suggested.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2022-11-26 20:11 GMT
Find Nodes?
    Voting Booth?