|
List Info
Thread: Alphabetize a word
|
|
| Alphabetize a word |
  United States |
2007-09-05 08:13:02 |
|
|
|
| Re: Alphabetize a word |
  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="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!-----
__._,_.___
.
__,_._,___
|
| Re: Alphabetize a word |
  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 <tigger io.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 |
  United States |
2007-09-05 09:12:42 |
|
Which gets us to the oneliner:
perl -e ' $word="alphabetize"; print join "", (sort split ( //, $word)), "n"'
1:41pm, greenberg.d%40gmail.com">greenberg.d gmail.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
>
> Sent via BlackBerry by AT&T
>
> -----Original Message-----
> From: Paul Archer < tigger%40io.com">tigger io.com>
>
> Date: Wed, 5 Sep 2007 08:20:52
> To: perl-beginner%40yahoogroups.com">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: http://help.yahoo.com/help/us/groups/groups-32.html
> Yahoo! Groups Links
>
>
>
>
>
__________________________________________________________
"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"__________________
-----11004 days until retirement!-----
__._,_.___
.
__,_._,___
|
[1-4]
|
|