Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Extract delimited words from string

by Discipulus (Canon)
on Dec 06, 2022 at 10:01 UTC ( [id://11148601]=note: print w/replies, xml ) Need Help??


in reply to Extract delimited words from string

Hello, if this is a real example data you can use regex. What did you tried and searched for so far?

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Extract delimited words from string
by Anonymous Monk on Dec 06, 2022 at 11:04 UTC
    I thought that Text::Balanced module could help, but I got lost in the documentation...

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found