Perl string split question

Joined
Nov 4, 2005
Messages
23
$string = '1, 2 , 5 , "8,9", xy, a'

Is there anyway i can use the Perl Split function and NOT split up the "8,9" when using the comma as a delimiter?

THanks in advance
 
I'm not sure of the right way to do it as I don't know perl or the methods you can use ( I just looked some things up), but here's my evil thing:

Code:
#!/usr/bin/perl
use strict;
use warnings;

my $str = '1, 2 , 5 , "8,9", xy, a';
my @results = split(/(\".+\")/, $str );
my @clean;
foreach my $i(@results) {
    if ( $i =~ m/\"/) {
        push( @clean, $i );
    } else {
        my @temp = split(',', $i );
        foreach my $z(@temp) {
            for ($z) {
                s/^\s+//;
                s/\s+$//;
            }
            if ( $z ) {
                push(@clean, $z );
            }
        }
    }
}

foreach my $count(@clean) {
    print $count;
    print "\n";
}

I assumed spaces between the commas are not consistent and that you wanted white space stripped. I wasn't sure if perl strings have a .empty() function to check if they're empty or if you have to use "" or what.
 
Back
Top