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 (Archbishop) 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:  <%-{-{-{-<