Re: Each char / letter --> array from string value
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 28 Dec 2005 11:39:15 -0800
Umesh T G wrote:
> I need to put each letter from a string to an array.
> I am doing it this way and it works. I wanna know if there is any other
> better way to do this / a built-in function.
>
> #! /usr/bin/perl
> $srt ="123";
> $sl = length $srt;
Why are you obtaining the length of $srt? You never use it.
> $val= join (':', split(/ */, $srt) );
Here you're splitting the string up, and then immediately joining it
back together (albeit with a different 'glue' than it originally had).
Why? You want to get at the split-up version.
> @arr = split(/:/,$val);
Here you're taking what you just joined back together, and splitting it
up again.
Here's an equivalent to what you just did:
"I want to find the results of 32 - 10. Here's my algorithm:
$val = (32 - 10) + 15;
$val = $val - 15;
"
If you want to split something to get at it's individual parts, just do
the split once:
my @chars = split //, $srt;
(The null pattern has the same effect as your / */ pattern above, but
is more concise and more customary)
Paul Lalli.
.
- References:
- Each char / letter --> array from string value
- From: Umesh T G
- Each char / letter --> array from string value
- Prev by Date: Re: Processing a web page (or looping over a multi line string)
- Next by Date: Re: Processing a web page (or looping over a multi line string)
- Previous by thread: Re: Each char / letter --> array from string value
- Next by thread: running a short perl script in a windows XP arena
- Index(es):
Relevant Pages
|