-----Original Message-----
From: ToddAndMargo [mailto:toddandma...@zoho.com]
Sent: Sunday, July 16, 2017 8:35 PM
To: perl6-users <perl6-us...@perl.org>
Subject: Re: String to array problem

On 07/16/2017 05:16 PM, Mark Devine wrote:
T,

my $x = 'ls -al "Program Files" "Moe Curly Larry"'; my @y = ~($x ~~
m:global/ [ '"' <-[ " ]> * '"' | \S+ ] /);

Mark Devine

-----Original Message-----
From: ToddAndMargo [mailto:toddandma...@zoho.com]
Sent: Sunday, July 16, 2017 7:41 PM
To: perl6-users <perl6-us...@perl.org>
Subject: String to array problem

Hi All,

I have been scratching my head trying to figure out how to turn a string with 
quotes in it into an array.

my $x='ls -al "Program Files" "Moe Curly Larry"';

Desired result:
my @y;
      $y[0] = 'ls';
      $y[1] = '-la';
      $y[2] = 'Program Files';
      $y[3] = 'Moe Curly Larry';

Any words of wisdom?

Many thanks,
-T



It kinda, sorta doesn't work:

<code>
$ cat ./QuoteArrayTest.pl6
#! /usr/bin/env perl6

use strict;

print "\n";
my $x = 'ls -al \"Program Files\" \"Moe Curly Larry\"'; my @y =~($x ~~ m:global/ [ '"' <-[ " 
]> * '"' | \S+ ] /); say "$x\n\@y:";

for @y.kv -> $index, $value { say "\$y[$index] = <$value>"; } print "\n"; 
</code>

ls -al \"Program Files\" \"Moe Curly Larry\"
@y:
$y[0] = <ls -al \"Program Files\" \"Moe Curly Larry\">


What did I do wrong?


On 07/16/2017 05:50 PM, Mark Devine wrote:
> T,
>
> Sorry (newbie, remember). For the first question that did not have backslash double-quotes:
>
> my ($x, @y);
> $x = 'ls -al "Program Files" "Moe Curly Larry"';
> @y = $x ~~ m:global/ [ '"' <-[ " ]> * '"' | \S+ ] /;
> for @y -> $match { say ~$match; }
>
> Don't know about the second question with backslash double-quotes.
>
> Mark


That worked.  Thank you!

<code>
#! /usr/bin/env perl6

use strict;

print "\n";
my $x = 'ls -al "Program Files" "Moe Curly Larry"';
my @y = $x ~~ m:global/ [ '"' <-[ " ]> * '"' | \S+ ] /;
say "$x\n\@y:";

for @y.kv -> $index, $value { say "\$y[$index] = <$value>"; }
print "\n";
</code>


$./QuoteArrayTest.pl6

ls -al "Program Files" "Moe Curly Larry"
@y:
$y[0] = <ls>
$y[1] = <-al>
$y[2] = <"Program Files">
$y[3] = <"Moe Curly Larry">

Reply via email to