Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Help with parsing a file

by tybalt89 (Monsignor)
on May 30, 2022 at 18:32 UTC ( [id://11144306]=note: print w/replies, xml ) Need Help??


in reply to Help with parsing a file

Here's a couple of different choices inspired by LanX's Re: Help with parsing a file (hash keys are unique)

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11144249 use warnings; my $file = 'data.11144249'; open my $fh, '<', $file or die "$! opening $file"; local $_ = do{ local $/; <$fh> }; # slurp entire file my %solvent_face; # as requested $solvent_face{ "$3_$1" } = $2 while /\b (F\S+) \h+ ([-\d.]+) (?=.*? (\S+) \h+ 0.00000000 )/gsx; # by solvent, then arrays of each F*** push @{ $solvent_face{ $3 }{ $1 } }, $2 while /\b (F\S+) \h+ ([-\d.]+) (?=.*? (\S+) \h+ 0.00000000 )/gsx; # by F***, then arrays of solvent push @{ $solvent_face{ $1 }{ $3 } }, $2 while /\b (F\S+) \h+ ([-\d.]+) (?=.*? (\S+) \h+ 0.00000000 )/gsx; use Data::Dump 'dd'; dd \%solvent_face;

Outputs:

{ "acetonitrile" => { "F002" => [1.69945785], "F011" => [0.74578421], "F101" => [2.67268035], "F11-1" => [1.64808218], "F111" => [1.95840198], }, "acetonitrile_F002" => 1.69945785, "acetonitrile_F011" => 0.74578421, "acetonitrile_F101" => 2.67268035, "acetonitrile_F11-1" => 1.64808218, "acetonitrile_F111" => 1.95840198, "F002" => { acetonitrile => [1.69945785], h2o => [4.66656083] }, "F011" => { acetonitrile => [0.74578421], h2o => [26.13597035] }, "F101" => { acetonitrile => [2.67268035], h2o => [32.47411476] }, "F11-1" => { acetonitrile => [1.64808218], h2o => [29.58963453] }, "F111" => { acetonitrile => [1.95840198], h2o => [30.24092207] }, "h2o" => { "F002" => [4.66656083], "F011" => [26.13597035], "F101" => [32.47411476], "F11-1" => [29.58963453], "F111" => [30.24092207], }, "h2o_F002" => 4.66656083, "h2o_F011" => 26.13597035, "h2o_F101" => 32.47411476, "h2o_F11-1" => 29.58963453, "h2o_F111" => 30.24092207, }

Replies are listed 'Best First'.
Re^2: Help with parsing a file
by Odar (Novice) on May 31, 2022 at 21:10 UTC

    This one also works perfectly. Thank you!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-24 08:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found