[ale] One for the perl hackers

Michael Potter michael at potter.name
Sat Oct 5 17:56:15 EDT 2013


If you are unable to install cpan packages, then this is an option:

sub mysplit
{
   my $Line = shift;

   my @new  = ();
   push(@new, $+) while $Line =~ m{
         # the first part groups the phrase inside the quotes.
         # see explanation of this pattern in MRE
         "([^\"\\]*(?:\\.[^\"\\]*)*)",?
            |  ([^,]+),?
            | ,
        }gx;
   @new = map { defined $_ ? $_ : "" } @new;
   push(@new, "") if substr($Line, -1, 1) eq ',';
   return(@new);
}

I picked that up off the internet some time ago and it has served me well.

I have never bothered to try to figure out how it works.


On Sat, Oct 5, 2013 at 1:19 AM, Doug Hall <doughalldev at gmail.com> wrote:
> One way would be to use a two-step process. On the first pass, strip out the
> double-quoted components. Then, in the second pass, parse only the
> components which aren't quoted.
>
> So, in your example, the first pass would return an array of three items:
>
>> component1,component2,
>> "This is my, test string"
>> , component4
>
> The second pass would remove the beginning and trailing commas, and then
> split what's left at the remaining commas.
>
> Doug
>
>
> On Thu, Oct 3, 2013 at 4:22 PM, Robert L. Harris <robert.l.harris at gmail.com>
> wrote:
>>
>>
>> *Sorry for the OT but my only perl lists have gone dark a long time ago*
>>
>>
>> $String='component1,component2,"This is my, test string", component4';
>>
>> ($C1, $C2, $Str, $C4) = split(',', $String);
>>
>> I'm only getting "This is my" in $Str and $C4 does not contain
>> "component4".  Is there a graceful way of handling this?  It's a 1 in 10000
>> chance it'll break but accuracy counts.
>>
>> Robert
>>
>>
>> --
>> :wq!
>>
>> ---------------------------------------------------------------------------
>> Robert L. Harris
>>
>> DISCLAIMER:
>>       These are MY OPINIONS             With Dreams To Be A King,
>>        ALONE.  I speak for                      First One Should Be A Man
>>        no-one else.                                     - Manowar
>>
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> http://mail.ale.org/mailman/listinfo/ale
>> See JOBS, ANNOUNCE and SCHOOLS lists at
>> http://mail.ale.org/mailman/listinfo
>>
>
>
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
Michael Potter
  Tapp Solutions, LLC
  Replatform Technologies, LLC
+1 770 815 6142  ** Atlanta ** michael at potter.name  **
www.linkedin.com/in/michaelpotter


More information about the Ale mailing list