Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This approach is very generalized (some might say over-generalized :) and more verbose, but has the advantage that it is very flexible and can be highly specialized. E.g., patterns for top level key and lower level key/values can be individually specified. The script does a fair amount of data validation. I'm using a slightly different example dataset for testing.

Script extract_HoH_1.pl:

use strict; use warnings; use autodie; use Data::Dumper; my $filename = '11123126_1.dat'; # could take this from command line # ignore blank or # comment lines use constant IGNORE => qr{ ^ \s* (?: [#] .* )? $ }xms; my $rx_identifier = qr{ \w+ }xms; my $rx_toplevel = qr{ $rx_identifier }xms; my $rx_key = qr{ $rx_identifier }xms; my $rx_value = qr{ $rx_identifier }xms; my $toplevel; my %hash; open my $fh, '<', $filename; RECORD: while (my $record = <$fh>) { next RECORD if $record =~ IGNORE; # print Dumper $record; # for debug my $got_toplevel = my ($tl) = $record =~ m{ ^ \s* ($rx_toplevel) \s* : \s* $ }xms; $toplevel = $tl, next RECORD if $got_toplevel; # print Dumper $toplevel; # for debug my $got_key_val = my ($key, $value) = $record =~ m{ ^ \s* ($rx_key) \s* : \s* ($rx_value) \s* $ }xms; # print Dumper $key, $value; # for debug die "no top level seen" unless defined $toplevel; $hash{$toplevel}{$key} = $value, next RECORD if $got_key_val; # no valid record seen. die "bad record '$record'"; } # end while RECORD print Dumper \%hash; close $fh; exit; # subroutines ###################################################### # none for now

Example dataset 11123126_1.dat:

first: this:that here:there when:what firstAsValue:first how:where now:later first:firstAsKey second: this2:that2 secondAsValue:second here2:there2 second:secondAsKey when2:what2 how2:where2 now2:later2

Example run:

Win8 Strawberry 5.8.9.5 (32) Sat 10/24/2020 15:46:02 C:\@Work\Perl\monks\pritesh_ugrankar >perl extract_HoH_1.pl $VAR1 = { 'first' => { 'when' => 'what', 'here' => 'there', 'first' => 'firstAsKey', 'now' => 'later', 'how' => 'where', 'firstAsValue' => 'first', 'this' => 'that' }, 'second' => { 'now2' => 'later2', 'here2' => 'there2', 'how2' => 'where2', 'when2' => 'what2', 'secondAsValue' => 'second', 'this2' => 'that2', 'second' => 'secondAsKey' } };


Give a man a fish:  <%-{-{-{-<


In reply to Re: Applying regex to each line in a record. by AnomalousMonk
in thread Applying regex to each line in a record. by pritesh_ugrankar

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2021-01-16 18:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?