Re: Process string as Array
by duff (Parson) on May 28, 2004 at 19:15 UTC
|
perldoc -f substr
Alternatively you can use split but if you want changes to the array to be reflected back in the string, you'll need to reform the string:
@string = split //, $string;
# manipulate @string all you want
$string = join '', @string;
update: BTW, I forgot to mention, if you're trying to validate your data as something generally well known, there are modules on CPAN that can probably do it for you.
| [reply] [d/l] |
Re: Process string as Array
by Fletch (Bishop) on May 28, 2004 at 19:30 UTC
|
Many times if you're trying to muck with a string character by character you're probably not thinking in a perl-y manner yet (you're thinking C-ish and trying to iterate over a string rather than just using m// or s/// to do whatever in a single operation). If you're really set on doing it char by char, you want substr, split, or maybe something like Tie::CharArray.
| [reply] [d/l] [select] |
Re: Process string as Array
by BrowserUk (Patriarch) on May 28, 2004 at 20:12 UTC
|
As others have said, it's very rare that you need to process string byte by byte in perl, but if you really do have to and you have any volume of strings to process then use @bytes = unpack 'C*', $string; in preference to @bytes = split '', $string;. It's an order of magnitude quicker.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
| [reply] [d/l] [select] |
Re: Process string as Array
by dragonchild (Archbishop) on May 28, 2004 at 19:29 UTC
|
my @string_as_array = split( '', $mystring );
# Do stuff here
$mystring = join( '', @string_as_array );
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
| [reply] [d/l] |
Re: Process string as Array
by waswas-fng (Curate) on May 28, 2004 at 19:27 UTC
|
What are you trying to validate? Most of the time you can use a regex to validate without breaking the string. Give us some examples and you may get a "better" way to do it. | [reply] |
Re: Process string as Array
by ambrus (Abbot) on May 29, 2004 at 10:04 UTC
|
while ($mystring=~ /(.)/gs) {
my $char= $1;
&do_whatever_with ($char);
}
provided you mean character-by-character if the string is unicode.
| [reply] [d/l] |
Re: Process string as Array
by zentara (Archbishop) on May 29, 2004 at 13:33 UTC
|
Here's a handy code fragment I was just looking at today.
$string = 'foo' x 2048;
$n = 1024; # $n is group size.
@groups = unpack "a$n" x ((length($string)/$n)-1) . "a*", $string;
print join("\n\n\n",@groups),"\n\n";
I'm not really a human, but I play one on earth.
flash japh
| [reply] [d/l] |
|
$string = "foo" x 3 . "fo";
print join(" -- ", unpack "(A3)*", $string), "\n";
Though your code is slightly different; the final "fo" is tacked onto the end of the last element, whereas with the above it's a new element.
| [reply] [d/l] |