[Buffalo-pm] Splitting String Of Length "N" Into Elements Of An Array...
Kevin Eye
eye at buffalo.edu
Thu Dec 29 10:29:18 PST 2005
"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
On Dec 29, 2005, at 11:13 AM, DANIEL MAGNUSZEWSKI wrote:
> All,
>
> I need to take a string of length n, where n is very large and
> unknown,
> and put each letter into an element of an array.
>
> Sudo-Code Example:
>
> $string = 'slkdfj';
> @letters = split /SOME REGEX/, $string;
> foreach (@letters) { print "Element: $_\n"; }
>
> Expected Output:
>
> Element: s
> Element: l
> Element: k
> Element: d
> Element: f
> Element: j
>
> I'm not sure if using split is the proper way to accomplish this,
> or if
> I need to use some kind of stream reader.
>
> Thoughts?
>
> #!/Dan
>
> _______________________________________________
> Buffalo-pm mailing list
> Buffalo-pm at pm.org
> http://mail.pm.org/mailman/listinfo/buffalo-pm
More information about the Buffalo-pm
mailing list