Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Extract delimited words from string

by Anonymous Monk
on Dec 06, 2022 at 11:04 UTC ( [id://11148605]=note: print w/replies, xml ) Need Help??


in reply to Re: Extract delimited words from string
in thread Extract delimited words from string

I thought that Text::Balanced module could help, but I got lost in the documentation...

Replies are listed 'Best First'.
Re^3: Extract delimited words from string
by atcroft (Abbot) on Dec 08, 2022 at 06:05 UTC

    Yes, Text::Balanced will work (and it is a core module since 5.7.3).

    Code:

    #!/usr/bin/env perl use strict; use warnings; use Text::Balanced qw/ extract_multiple extract_quotelike /; my @data = ( q{50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 A255" + 0}, q{70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 G12 B12 A255" "R12 G12 B +12 A255" 0}, ); my @extracted; foreach my $str (@data) { @extracted = extract_multiple( $str, [ \&extract_quotelike, ], ); print q{Input: }, $str, qq{\n}; print q{Output: }, qq{\n}; print qq{\t}; print join qq{\n\t}, grep { !(m/^\s*$/) and length $_ > 0 } @extracted; print qq{\n}; }

    Output:

    Input: 50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 + A255" 0 Output: 50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 A255" 0 Input: 70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 G12 B12 A255" "R12 + G12 B12 A255" 0 Output: 70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 G12 B12 A255" "R12 G12 B12 A255" 0

    Hope that helps.

      First of all that's great. Thanks! Those old modules often have uncommon documentation.

      Just wanna note that this is answering a third interpretation of the question.

      • for you '50 0' is one field
      • for hippo unquoted fields are ignored
      • for me '50' and '0' are two fields
      Probably Text::Balanced can handle all of those?

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        Simple enough-the extract_multiple() function can apply multiple functions or regular expressions to do so. Changing the extract_multiple() line to the following:
        @extracted = extract_multiple( $str, [ \&extract_quotelike, qr/\s+/, ], );
        results in the following output:

        Input: 50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 + A255" 0 Output: 50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 A255" 0 Input: 70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 G12 B12 A255" "R12 + G12 B12 A255" 0 Output: 70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 G12 B12 A255" "R12 G12 B12 A255" 0

        (My original code was just extracting based on the quotation marks as delimiters.)

        Hope that helps.

Log In?
Username:
Password:

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

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

    No recent polls found