Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > In search of elegant code: inverting a string

Reply
Thread Tools

In search of elegant code: inverting a string

 
 
David Filmer
Guest
Posts: n/a
 
      10-29-2003
OK, a simple task: I want to invert (reverse) a string, such that
'ABCDEFG' becomes 'GFEDCBA'

I first thought of:

print reverse split //, $foo; # Yikes, did I write THAT?!?!?!

but realized that first thoughts are often FAR more twisted and
convoluted than they ever need to be, so I reconsidered with:

while ($foo) {
print chop $foo; #a new Chinese dish?
}

which is FAR better than my first idea, but still doesn't strike me as
truly elegant.

I wonder how other hacks might code it.
 
Reply With Quote
 
 
 
 
Jay Tilton
Guest
Posts: n/a
 
      10-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (David Filmer) wrote:

: OK, a simple task: I want to invert (reverse) a string, such that
: 'ABCDEFG' becomes 'GFEDCBA'
:
: I first thought of:
:
: print reverse split //, $foo; # Yikes, did I write THAT?!?!?!
:
: but realized that first thoughts are often FAR more twisted and
: convoluted than they ever need to be, so I reconsidered with:
:
: while ($foo) {
: print chop $foo; #a new Chinese dish?
: }
:
: which is FAR better than my first idea, but still doesn't strike me as
: truly elegant.
:
: I wonder how other hacks might code it.

They would simply use reverse() in scalar context.

my $foo = 'ABCDEFG';
print scalar reverse $foo;

 
Reply With Quote
 
 
 
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      10-30-2003
David Filmer ((E-Mail Removed)) wrote:
: OK, a simple task: I want to invert (reverse) a string, such that
: 'ABCDEFG' becomes 'GFEDCBA'

: I first thought of:

: print reverse split //, $foo; # Yikes, did I write THAT?!?!?!

: but realized that first thoughts are often FAR more twisted and
: convoluted than they ever need to be, so I reconsidered with:

: while ($foo) {
: print chop $foo; #a new Chinese dish?
: }

: which is FAR better than my first idea, but still doesn't strike me as
: truly elegant.

: I wonder how other hacks might code it.

I would use reverse $string ;

I am guessing you tried

print reverse 'ABCDEFG' ;

unfortunately that would be misleading. Instead you must type

print scalar reverse 'ABCDEFG';

or

$reversed = reverse 'ABCDEFG';
print $reversed;


Print puts things in list context, so reverse reverses your _list_ of
strings, but there's only one string in your list, so reversing your list
made no difference (if you tried what I suspected).

 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      10-30-2003
On 29 Oct 2003 15:50:48 -0800,
David Filmer <(E-Mail Removed)> wrote:
> OK, a simple task: I want to invert (reverse) a string, such that
> 'ABCDEFG' becomes 'GFEDCBA'
>
> I first thought of:
>
> print reverse split //, $foo; # Yikes, did I write THAT?!?!?!
>
> but realized that first thoughts are often FAR more twisted and
> convoluted than they ever need to be, so I reconsidered with:
>
> while ($foo) {
> print chop $foo; #a new Chinese dish?
> }


I'd probably do

print scalar reverse $foo;

As the documentation explains, reverse() in scalar context returns a
string.

> which is FAR better than my first idea, but still doesn't strike me as
> truly elegant.
>
> I wonder how other hacks might code it.


other hacks might code a subroutine like this:

sub my_reverse
{
my $s = shift;
substr $s, $_, 1, substr $s, -$_ - 1, 1, substr $s, $_, 1
for (0 .. (length $s)/2 - 1);
$s;
}

Martien
--
|
Martien Verbruggen | The four horsemen of the apocalypse are
Trading Post Australia | called Abort, Retry, Ignore and Fail.
|
 
Reply With Quote
 
TDN
Guest
Posts: n/a
 
      10-30-2003

"Purl Gurl" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Amazing Perl Scripts!
> http://www.purlgurl.net/~callgirl/android.html


All I have to say is - Roberta is hot!


 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      10-30-2003
>>>>> "Purl" == Purl Gurl <(E-Mail Removed)> writes:

Purl> Randal wrote you should never use the word "scalar" in Perl code.

No. In the past, I have said that you shouldn't use it if you don't
need it. And most of the time, you don't need it, since most of the
time, you're already in scalar context when you would have used it.

It comes in handy when you have things like:

print "Last modified: ", (scalar localtime $mtime), "\n";

because the alternatives are much uglier.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
Bart Lateur
Guest
Posts: n/a
 
      10-30-2003
Randal L. Schwartz wrote:

>It comes in handy when you have things like:
>
> print "Last modified: ", (scalar localtime $mtime), "\n";
>
>because the alternatives are much uglier.


You mean like using dots instead of the commas? Yeah, lots uglier.

--
Bart.
 
Reply With Quote
 
Roy Johnson
Guest
Posts: n/a
 
      10-30-2003
(E-Mail Removed) (Malcolm Dew-Jones) wrote in message news:<(E-Mail Removed)>...
> I am guessing you tried
> print reverse 'ABCDEFG' ;
> unfortunately that would be misleading. Instead you must type
> print scalar reverse 'ABCDEFG';
> or
> $reversed = reverse 'ABCDEFG';
> print $reversed;


or
print my $reversed=reverse 'ABCDEFG';
or even
print ''.reverse 'ABCDEFG';

or, to avoid reverse (for whatever reason):
$foo='ABCDEFG';
print map(substr($foo,-$_,1), 1..length $foo)
 
Reply With Quote
 
David Filmer
Guest
Posts: n/a
 
      10-30-2003
>
> I would use reverse $string ;
>
> I am guessing you tried
>
> print reverse 'ABCDEFG' ;
>
> unfortunately that would be misleading. Instead you must type
>
> print scalar reverse 'ABCDEFG';
>
> or
>
> $reversed = reverse 'ABCDEFG';
> print $reversed;
>
>
> Print puts things in list context, so reverse reverses your _list_ of
> strings, but there's only one string in your list, so reversing your list
> made no difference (if you tried what I suspected).


Yup, I did try "print reverse 'ABCDEFG' ;" and it didn't work.
'scalar' does the trick. On reflection, I prefer the "print $foo =
reverse $foo;" syntax.

Thanks to everyone who offered these elegant solutions, as well as
those who offered some not-so-elegant but definately thought-provoking
alternatives.
 
Reply With Quote
 
Russ Jones
Guest
Posts: n/a
 
      10-30-2003
Purl Gurl <(E-Mail Removed)> wrote in news:3FA138A4.AB00C340
@purlgurl.net:

> Purl Gurl


I can't take any more. plonk.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Elegant solution for inverting zip? Gregory Brown Ruby 3 07-14-2009 02:48 PM
inverting a string Gary Wessle C++ 2 08-18-2006 05:21 AM
Inverting a Regular Expression in Validator spalding ASP .Net 1 01-28-2005 05:46 PM
Inverting a Regular Expression in Validator MWells ASP .Net 1 01-28-2005 02:44 AM
question on inverting text -> txet arctan Perl 6 10-16-2003 09:09 PM



Advertisments