Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

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

I don't understand all the business about scoring, but at least this gets the expected fields for the given input strings. Perhaps you can use it as a point of departure to achieve your true goals.

c:\@Work\Perl\monks>perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; use Data::Dump qw(dd); ;; use List::MoreUtils qw(uniq); ;; my @list = ( 'set abcde-efghi 12345', 'set abcde-ijkl 12345', 'clr abcde-efghi+123', 'clr abcde-ijkl 12345', ); ;; my @expected_substrings = ( 'set', 'clr', ' abcde-', 'efghi', 'ijkl', ' 12345', '+123', ); ;; my $rx_fld1 = qr{ [[:alpha:]]{2,} }xms; my $rx_fld2 = qr{ \s [[:alpha:]]{2,} - }xms; my $rx_fld3 = qr{ [[:alpha:]]{3,} }xms; my $rx_fld4 = qr{ [\s+] [[:digit:]]{2,} }xms; ;; my (@flds1, @flds2, @flds3, @flds4); for my $str (@list) { my $parsed = my ($fld1, $fld2, $fld3, $fld4) = $str =~ m{ \A ($rx_fld1) ($rx_fld2) ($rx_fld3) ($rx_fld4) \z }xms; die qq{'$str' parse failed} unless $parsed; ;; push @flds1, $fld1; push @flds2, $fld2; push @flds3, $fld3; push @flds4, $fld4; } ;; my @got_substrings = uniq @flds1, @flds2, @flds3, @flds4; dd \@got_substrings; ;; is_deeply \@got_substrings, \@expected_substrings, 'extracted list ok'; " ["set", "clr", " abcde-", "efghi", "ijkl", " 12345", "+123"] ok 1 - extracted list ok ok 2 - no warnings 1..2
Note: uniq is also in List::Util in up-to-date versions of Perl; I'm testing under version 5.8.9.

Update: It occurs to me that the uniq-ifying step should be done on each sub-set individually before the sub-sets are combined together. This could be done with a
    my @got_substrings = map { uniq @$_ } \(@flds1, @flds2, @flds3, @flds4);
statement. The output list produced is the same.


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


In reply to Re: Divide a list of string into substrings (updated) by AnomalousMonk
in thread Divide a list of string into substrings by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
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 exploiting the Monastery: (5)
As of 2021-05-13 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?