SPUG:Split question

Michael R. Wolf MichaelRunningWolf at att.net
Fri Mar 21 23:42:06 CST 2003


"Thane Williams" <thane at fastmail.fm> writes:

> Thanks to everyone who suggested encasing the match in parentheses, this
> is the solution I was looking for.
> 
> To everyone who is baffled why I wouldn't just join it back together:
> I'm splitting on known patterns with unknown contents, and I want to
> preserve the contents.

Like this, I presume.  What does your field processing loop look like?
I've taken a stab at a slice that grabs, then modifies, them to be
qouted.

while ($line = <DATA>) {
    chomp $line;
    @fields = split /([ ,:])/, $line, -1;

    foreach my $field(@fields[grep {$_%2 == 0} 0..$#fields]) {
        $field = qq("$field");
    }

    print join("|", @fields), "\n";
}    
    
__DATA__
comma,separated,line
colon:separated:line
space separated line



Output:
"comma"|,|"separated"|,|"line"
"colon"|:|"separated"|:|"line"
"space"| |"separated"| |"line"


-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRunningWolf at att.net




More information about the spug-list mailing list