SPUG:Split question

Brian Hatch spug at ifokr.org
Fri Mar 21 16:35:49 CST 2003



> I've often found myself wishing there were an option to split a string
> while retaining the characters you split with. For example:
> 
> $string = "a,b,c,d,e,f,";
> @array = split /,/, $string;
> print "@array\n";
> 
> Would print
> a, b, c, d, e, f,
> instead of
> a b c d e f
> 
> Has anyone else wished this?

Nope.

> Does anyone have any suggestions? I'd find
> this particularly useful when I'm dividing up big files based on
> complicated strings, and I want to preserve those strings in the chunks.

Join them back together when you need the delimiters back:

$string = "a,b,c,d,e,f,";
@array = split /,/, $string;
print join(", ", @array), "\n";


Note that it doesn't capture your last ',' though, because perl
drops any trailing empty elements when using split.


--
Brian Hatch                  They assigned blank.
   Systems and               So I read about blank.
   Security Engineer         So now my mind is filled
http://www.ifokr.org/bri/    with blank.
                             --bree
Every message PGP signed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/spug-list/attachments/20030321/37318c74/attachment.bin


More information about the spug-list mailing list