[Buffalo-pm] Splitting String Of Length "N" Into ElementsOf An Array...
DANIEL MAGNUSZEWSKI
dmagnuszewski at mandtbank.com
Fri Dec 30 07:24:56 PST 2005
doh!
Thanks. I figured it was something small that I was forgetting.
>>> "Kevin Eye" <eye at buffalo.edu> 12/29/05 1:29 PM >>>
"SOME REGEX" can be blank (e.g. split //, $string), which will split
on every character like you want. I think split is a good way to do
that if you need every character in an array.
If the string is actually long (a whole file, maybe; something more
than a few hundred characters), it would probably be a lot more
efficient to avoid making a hundred- or thousand-character array of
one character each.
To work with it without making an array, I'd do something like this:
$string = 'slkdfj';
foreach (0..length($string)-1) { print "Element: ".substr($string,
$_, 1)."\n"; }
I can't bring myself to use the C-style for(i=0;i<something;i++)
syntax. It's too much thinking for perl for something that's so
simple, and I've heard that the above "foreach (0..x)" is internally
optimized into something just as efficient.
- Kevin
More information about the Buffalo-pm
mailing list