You could just modify my previous code to also return the $record from get_record() and print the $record if $f ne '';

Another common technique for parsing a record like this is to return a hash (or hash ref) in case you want to access these individual parameters. The format X=3 is easy to parse, below I show how, with a more extensive code modification. There are of course other formulations of how to do this. I just went along the lines of my original post.

The idea of these Monk posts is to give you some ideas and get you "unstuck". Some effort on the OP is required to understand the technique(s) and adapt it to your specific application. Hope this helps.

Update:
I thought better of the ending condition for the main loop and changed it to end on a blank record. Also as a general point, I typically try to isolate the record parsing into a subroutine. I just made a new code release of a project I've been working on for the past year. During that time, the input format and also how the data is presented of one key file has changed 4 times! I yell at these guys when they do that, but alas, I must adapt. I derive the same info from all 4 formats, but this is not just different formats, but also different algorithms - "data" is not "information". A mess.

#!/usr/bin/perl use warnings; use strict; my ($record,%hash); while ( ($record, %hash)=get_record() and $record ne '' ) { next unless $hash{f} ne ''; print "$record\n"; print "Extra parsed info:\n"; foreach my $key (keys %hash) { print " $key \tvalue=$hash{$key}\n"; } print "\n"; } sub get_record { my $line; my $record=''; while (defined($line=<DATA>) and $line !~ /^\s*$/) { $record .= $line } my %hash = $record =~ /(\w+)=(\w+)/g; ($hash{f}) = $record =~ /f (.*)/; $hash{f} //=""; return ($record, %hash); } =Prints: b=54 y=0x23 arg=0x78 def=0x2 val=0x65b f chk.fin.reg.m_cwr 0x213 Extra parsed info: def value=0x2 val value=0x65b f value=chk.fin.reg.m_cwr 0x213 b value=54 y value=0x23 arg value=0x78 b=40 y=0x90 arg=0x34 def=0x5 val=0x2197 f ref.grf.pin.clk_trg 0x0021 Extra parsed info: arg value=0x34 y value=0x90 f value=ref.grf.pin.clk_trg 0x0021 b value=40 def value=0x5 val value=0x2197 =cut __DATA__ b=23 y=0x11 arg=0x70 def=0x1 val=0x234 checking system b=71 y=0x35 arg=0x87 def=0x3 val=0x76d h=reg.k2.io.chk 0x2001 b=54 y=0x23 arg=0x78 def=0x2 val=0x65b f chk.fin.reg.m_cwr 0x213 b=54 y=0x23 arg=0x78 def=0x2 val=0x65b checking system b=40 y=0x90 arg=0x34 def=0x5 val=0x2197 f ref.grf.pin.clk_trg 0x0021

In reply to Re^3: perl regex for repeating words by Marshall
in thread perl regex for repeating words by analys

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, details, 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, summary, 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.