Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: perl regex for repeating words

by Perlbotics (Archbishop)
on Mar 11, 2018 at 18:36 UTC ( [id://1210674]=note: print w/replies, xml ) Need Help??


in reply to perl regex for repeating words

I was trying to extract the following words from input file.
But what words? Just guessing:

use strict; use warnings; while ( my $line = <DATA> ) { #-- would print lines starting with either 'f' or 'b': # if ( $line =~ /^(b|f)/) { # print $line; # } #-- not elegant, like the spec... if ( $line =~ /^(b)=(\S+)/ or $line =~ /^(f)\s+(\S+)\s+(\S+)$/ ) { print "$1 = '$2' (", $3 // '-' , ")\n"; } } __DATA__ Input file : (to extract for b and f) 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

Result:

b = '23' (-) b = '71' (-) b = '54' (-) f = 'chk.fin.reg.m_cwr' (0x213) b = '54' (-) b = '40' (-) f = 'ref.grf.pin.clk_trg' (0x0021)

Replies are listed 'Best First'.
Re^2: perl regex for repeating words
by analys (Initiate) on Mar 12, 2018 at 14:04 UTC

    I will need to get whole sentence of b and f and ignore b that are not related to f.

    Expected output :

    b=54 y=0x23 arg=0x78 def=0x2 val=0x65b f chk.fin.reg.m_cwr 0x213 b=40 y=0x90 arg=0x34 def=0x5 val=0x2197 f ref.grf.pin.clk_trg 0x0021

    from the b sentence, I will need to get y, arg, def. from f sentence (naming - chk.fin.reg.m_cwr, ref.grf.pin.clk_trg)

    Ex: From b : y = 0x23 arg = 0x78 def = 0x2 From f : chk.fin.reg.m_cwr
      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found