Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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

by AnomalousMonk (Bishop)
on May 16, 2020 at 17:42 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2020-09-30 02:28 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (157 votes). Check out past polls.