For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Each char / letter --> array from string value









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Each char / letter --> array from string value
Umesh T G

2006-01-10, 4:01 am

Hi List,

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;
$val= join (':', split(/ */, $srt) );
@arr = split(/:/,$val);
foreach $i (@arr) {
print $i."\n";
}

This gives me
1
2
3

This works fine for me.
I want an alternative solution, if any.

TIA,

Cheers,
Umesh.

Adriano Ferreira

2006-01-10, 4:01 am

On 12/28/05, Umesh T G <umesh.perl@gmail.com> wrote:
> I need to put each letter from a string to an array.


The usual way to tear apart a string into an array with characters is

@chars =3D split '', $string;

It is documented at C<perldoc -f split>.
Chris Devers

2006-01-10, 4:01 am

Paul Lalli

2006-01-10, 4:01 am

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.

Umesh T G

2006-01-10, 4:01 am

On 12/28/05, Adriano Ferreira <a.r.ferreira@gmail.com>
The usual way to tear apart a string into an array with characters is
@chars = split '', $string;
It is documented at C<perldoc -f split>.

On 12/28/05, Chris Devers <cdevers@pobox.com> wrote:
>
> $ perl -le '$i = "abcd"; @j = split //, $i; print join "\n", @j;'
>


Miles de gracias....

Cheers,
Umesh

Dr.Ruud

2006-01-10, 4:01 am

Chris Devers:

> $ perl -le '$i = "abcd"; @j = split //, $i; print join "\n", @j;'


A good alternative to [print join "\n", @list] is to set the <output
field separator> (see perldoc perlvar) to "\n".

perl -le "$,=qq{\n}; print split //, q{abcd}"

The qq{} is to make it work under CMD.EXE too, the q{abcd} can also be
written as 'abcd'.


Chris, you are still sending multipart/mixed messages.

And your sig is still broken: many of its characters
don't belong to the printable subset of their encoding.
http://en.wikipedia.org/wiki/ISO-8859-1

--
Affijn, Ruud

"Gewoon is een tijger."


Brad Baxter

2006-01-10, 4:02 am

Dr.Ruud wrote:
> Chris Devers:
>
>
> A good alternative to [print join "\n", @list] is to set the <output
> field separator> (see perldoc perlvar) to "\n".
>
> perl -le "$,=qq{\n}; print split //, q{abcd}"
>
> The qq{} is to make it work under CMD.EXE too, the q{abcd} can also be
> written as 'abcd'.


Or

perl -le'$,=$\;print split//,abcd'

Here's an interesting puzzle:

% perl -le'print split//,a'b
ab

This is perl, v5.8.7 built for sun4-solaris

--
Brad

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com