in reply to Split into array in a somewhat unconventional manner

I believe that
split(/"|'/,<your funny string goes here)
will do what you want. Here, let's test that supposition:
#! /usr/local/bin/perl use strict; use warnings; my $str =qq(jfjsa as,.n d"fdsafjl"jop'fdsjklf fds'457"fjdsklaoir"jkl45 +;fs ier987543" fsdjkal"); my @arr = split(/'|"/,$str); print("Number of bits: ", scalar(@arr),"\n"); print("Resultant array: " , join(' ', @arr), "\n"); exit(0)
prints
Resultant array: jfjsa as,.n d fdsafjl jop fdsjklf fds 457 fjdskl +aoir jkl45;fs ier987543 fsdjkal

----
I Go Back to Sleep, Now.

OGB

Replies are listed 'Best First'.
Re^2: Split into array in a somewhat unconventional manner
by johngg (Canon) on Aug 18, 2009 at 21:37 UTC

    I don't think that your code does quite what the OP wanted. The contents of the wanted array as posted indicate that the single- and double-quotes should be preserved around the text they are quoting. This could be achieved using a capture in the regex used to split the string.

    #!/usr/bin/perl -l # use strict; use warnings; my $str = q{jfjsa as,.n d"fdsafjl"jop'fdsjklf fds'457"fjdsklaoir"jkl45;fs ier +987543" fsdjkal"}; my $rxSplit = qr {(?x) # Use extended syntax ( # Open capture group (?: # Open group for alternation of "[^"']*" # "whatever" | # or '[^"']*' # 'some other thing' ) # Close alternation group ) # Close capture group }; my @arr = split m{$rxSplit}, $str; print for @arr;

    The output.

    jfjsa as,.n d "fdsafjl" jop 'fdsjklf fds' 457 "fjdsklaoir" jkl45;fs ier987543 " fsdjkal"

    I hope this is of interest.

    Cheers,

    JohnGG

      That creates empty elements if you have two delimited strings in a row. split really isn't the right tool.