List Info

Thread: Alphabetize a word




Alphabetize a word
country flaguser name
United States
2007-09-05 08:13:02

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]

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Search Ads

Get new customers.

List your web site

in Yahoo! Search.

Find Enlightenment

Yoga groups and

resources on

Yahoo! Groups.

Re: Alphabetize a word
country flaguser name
United States
2007-09-05 08:20:52

If what you are looking for is a list of the letters in a word, in
alphabetical order, then you want something like:

perl -e ' $word=&quot;alphabetize"; array= sort split ( //, $word); print
array, "n&quot;'

And you're doing fine with the 'shift'. No reason not to do it just the way
you have it.

Paul

9:13am, Ryan J Nauman wrote:

> I wrote the following code to alphabetize a word:
&gt;
> 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]
>
>;

---------------------------
404 Error - Item Not Found
&lt;haiku>
You step in the stream,
but the water has moved on.
That page is not here.
&lt;/haiku>;
---------------------------

-----11004 days until retirement!-----

__._,_.___
.

__,_._,___
Re: Alphabetize a word
country flaguser name
United States
2007-09-05 08:41:25
As for returning it as a string instead of an array of
characters, look at the "join" function.  With it,
you can explicitly return a string.

-David

Sent via BlackBerry by AT&T

-----Original Message-----
From: Paul Archer <tiggerio.com>

Date: Wed, 5 Sep 2007 08:20:52 
To:perl-beginner@yahoogroups.com
Subject: Re: [PBML] Alphabetize a word


If what you are looking for is a list of the letters in a
word, in 
 alphabetical order, then you want something like:
 
 perl -e ' $word="alphabetize"; array=
sort split ( //, $word); print 
 array, "n"'
 
 And you're doing fine with the 'shift'. No reason not to do
it just the way 
 you have it.
 
 Paul
 
 9:13am, Ryan J Nauman 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]
 >
 >
 
 ---------------------------
 404 Error - Item Not Found
 <haiku>
 You step in the stream,
 but the water has moved on.
 That page is not here.
 </haiku>
 ---------------------------
 
 -----11004 days until retirement!-----
   

Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://g
roups.yahoo.com/group/perl-beginner/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http
://groups.yahoo.com/group/perl-beginner/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:perl-beginner-digest@yahoogroups.com 
    mailto:perl-beginner-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    perl-beginner-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 

Re: Alphabetize a word
country flaguser name
United States
2007-09-05 09:12:42

Which gets us to the oneliner:
perl -e ' $word=&quot;alphabetize"; print join "&quot;, (sort split ( //, $word)), "n&quot;'

1:41pm, greenberg.d%40gmail.com">greenberg.dgmail.com wrote:

> As for returning it as a string instead of an array of characters, look at the "join" function. With it, you can explicitly return a string.
>
> -David
&gt;
> Sent via BlackBerry by AT&T
>
>; -----Original Message-----
> From: Paul Archer < tigger%40io.com">tiggerio.com>;
>
> Date: Wed, 5 Sep 2007 08:20:52
> To: perl-beginner%40yahoogroups.com">perl-beginneryahoogroups.com
> Subject: Re: [PBML] Alphabetize a word
>;
>
> If what you are looking for is a list of the letters in a word, in
> alphabetical order, then you want something like:
&gt;
> perl -e ' $word=&quot;alphabetize"; array= sort split ( //, $word); print
&gt; array, "n&quot;'
>
> And you're doing fine with the 'shift'. No reason not to do it just the way
> you have it.
>
> Paul
>;
> 9:13am, Ryan J Nauman wrote:
&gt;
> > I wrote the following code to alphabetize a word:
&gt; >
>; > 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]
> >
>; >
>;
> ---------------------------
> 404 Error - Item Not Found
&gt; <haiku&gt;
> You step in the stream,
> but the water has moved on.
> That page is not here.
&gt; </haiku>
> ---------------------------
>
> -----11004 days until retirement!-----
&gt;
>
> Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html
> Yahoo! Groups Links
&gt;
>
>
>;
>

__________________________________________________________
"Can't you recognize bullshit? Don't you think it would be a
useful item to add to your intellectual toolkits to be capable
of saying, when a ton of wet steaming bullshit lands on your
head, 'My goodness, this appears to be bullshit'?
_____________Neal Stephenson, "Cryptonomicon&quot;__________________

-----11004 days until retirement!-----

__._,_.___
.

__,_._,___
[1-4]

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