Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Extract delimited words from string

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


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

Thanks, how this would translate in a Perl program? Like in a loop on each line of the file:
while (@lines) { my $line = shift @lines; my @string_pieces = ... instruction here }

Replies are listed 'Best First'.
Re^3: Extract delimited words from string
by hippo (Bishop) on Dec 06, 2022 at 14:09 UTC

    If you don't fancy reading the excellent perlrun to find out what the flags do, then just use B::Deparse to find out:

    $ perl -MO=Deparse -F'"' -lE 'say $F[$_ * 2 + 1] for 0 .. $#F/2' BEGIN { $/ = "\n"; $\ = "\n"; } use feature 'current_sub', 'bitwise', 'evalbytes', 'fc', 'postderef_qq +', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval'; LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(/"/u, $_, 0); say $F[$_ * 2 + 1] foreach (0 .. $#F / 2); } -e syntax OK

    (Updated to match the original code)


    🦛

Re^3: Extract delimited words from string
by AnomalousMonk (Archbishop) on Dec 06, 2022 at 23:25 UTC

    A dedicated module is better, but if you're shy of that, another approach:

    c:\@Work\Perl\monks>perl use strict; use warnings; my @lines = ( '50 0 "R0 G255 B0 A255" "Solid" 118 1 "R0 G0 B0 A255" "R0 G0 B0 A255 +" 0', '70 0 "R0 G255 B255 A255" "Solid" 118 1 "R12 \"G12\" B12 A255" "R12 +G12 B12 A255" 0', ); my $rx_dq = qr{ " [^"\\]* (?: \\. [^"\\]*)* " }xms; for my $line (@lines) { my @d_quoted = $line =~ m{ $rx_dq }xmsg; print "@d_quoted \n"; } ^Z "R0 G255 B0 A255" "Solid" "R0 G0 B0 A255" "R0 G0 B0 A255" "R0 G255 B255 A255" "Solid" "R12 \"G12\" B12 A255" "R12 G12 B12 A255"


    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://11148604]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-25 09:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found