List Info

Thread: Re: Alphabetize a word




Re: Alphabetize a word
country flaguser name
Canada
2007-09-05 09:04:01

to get your array in a string, try the join command:

my $letters = join "", letters;

----RJNauman%40uss.com">----RJNaumanuss.com wrote ----

I wrote the following code to alphabetize a word:

sub alphabetize
{
my $word = uc(shift);
my $wordlen = length($word);
my letters;

for (my $i = 0; $i < length $word; $i++)
{
push letters, substr($word, $i, 1);
}

letters = sort letters;
return letters;
}

This function will always only receive one parameter. In this case I do
not know if shift is the best method or not. $_ was not giving me
anything so I went with shift. Alternatively, I think I could've used _.
Does it matter?

If it can be optimized in any way I would appreciate pointers. My
ultimate question though is I want it to return a string instead of the
sorted character array that it is now. I can write it using a for loop
and string concatenation but I think that would be extremely inefficient.
Help appreciated!

[Non-text portions of this message have been removed]

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )