Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Aaron www.knurdle.com

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Parse comma separated data

Import data in CSV format. Normally an easy task using split but sometimes, the fields will have comma's enclosed by quotes.
Something like.
Aaron, "123 Main St, USA", 555-555-1212
Assuming your string gets put into $text.
    my @new  = ();
    push(@new, $+) while $text =~ m{
        # the first part groups the phrase inside the quotes.
        # see explanation of this pattern in MRE
        "([^\"\\]*(?:\\.[^\"\\]*)*)",?
           |  ([^,]+),?
           | ,
       }gx;
       push(@new, undef) if substr($text, -1,1) eq ',';

Taken from Jeffrey Friedl's Mastering Regular Expressions

Encode and Decode strings from URL.

Encode a string so that it's safe to be passed in a url. Equivalent of urlencode in PHP.
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;


Now, the reverse, decode a string from a url. Equivalent of urldecode in PHP.
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed