Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Need your help in a pattern based text manipulation algorithm

by AnomalousMonk (Archbishop)
on May 16, 2020 at 17:42 UTC ( [id://11116844]=note: print w/replies, xml ) Need Help??


in reply to Need your help in a pattern based text manipulation algorithm

Without bothering with the GUI framework, here's a possible general approach to handling the VALUEs file:

c:\@Work\Perl\monks>perl use strict; use warnings; use autodie; use Data::Dump qw(dd); my @tags = qw(NAME TASK CAPS PKG_TYPE STACK SHIP); my $rx_value_intro = qr{ \s* [|] \s* VALUE \s+ = \s+ }xms; my $rx_value = qr{ [[:alnum:]]+ }xms; my ($rx_tag) = map qr{ \b (?: $_) \b }xms, join ' | ', map quotemeta, reverse sort @tags ; print "rx_tag $rx_tag \n"; # for debug my $data_file = <<'EOF'; NAME|VALUE = a TASK|VALUE = copy CAPS|VALUE = 0 PKG_TYPE|VALUE = premium NAME|VALUE = z TASK|VALUE = cut CAPS|VALUE = 0 PKG_TYPE|VALUE = premium NAME|VALUE = c TASK|VALUE = paste STACK|VALUE = 2 SHIP|VALUE = lowtier EOF open my $fh_input, '<', \$data_file; local $/ = ''; while (my $record = <$fh_input>) { my $got_params = my %params = $record =~ m{ \G \s* ($rx_tag) $rx_value_intro ($rx_value) \s+ }xmsg; die "bad params record '$record'" unless $got_params; # dd \%params; # for debug process_params(%params); } sub process_params { my (%params, ) = @_; print "processing params: @{[ %params ]} \n"; } __END__ rx_tag (?msx-i: \b (?: TASK | STACK | SHIP | PKG_TYPE | NAME | CAPS) \ +b ) processing params: NAME a PKG_TYPE premium TASK copy CAPS 0 processing params: NAME z PKG_TYPE premium TASK cut CAPS 0 processing params: STACK 2 NAME c SHIP lowtier TASK paste
See haukex's article Building Regex Alternations Dynamically. Of course, much testing of the code is needed.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found