Re: Each char / letter --> array from string value



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.

.



Relevant Pages

  • str.scan
    ... Regexp or a String). ... added to the result array or passed to the block. ... If the pattern ...
    (comp.lang.ruby)
  • Re: Help with RegExp
    ... How would I have to modify the pattern to allow for a maximum of 5 values e.g. ... The string can only contain a maximum of 10 numbers. ... Array(New RegExp, Array(_ ... For Each aTest In aTests ...
    (microsoft.public.scripting.vbscript)
  • Re: Interaction between two strings
    ... then the string XYZC should give a match. ... > string that is supplied separately from the regex. ... $patterns, you splice them all into one very big $pattern. ... Doing this is immensely faster than iterating through an array or the like: ...
    (comp.lang.perl.misc)
  • Re: regular expression question
    ... how would the pattern need look if the name wasn't "" delimited. ... >> into an array of two values. ... > You can use the following RegEx to parse the string: ... > You will not have a match, so yes you will have an empty string returned. ...
    (microsoft.public.dotnet.languages.vb)
  • Passing functions as arguments
    ... I'm just starting out with ruby. ... I'm trying to make an array of arrays containing [a pattern, a string, ...
    (comp.lang.ruby)