In search of elegant code: inverting a string

D

David Filmer

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.
 
J

Jay Tilton

(e-mail address 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;
 
M

Malcolm Dew-Jones

David Filmer ([email protected]) 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).
 
M

Martien Verbruggen

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
 
R

Randal L. Schwartz

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. :)
 
B

Bart Lateur

Randal said:
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. ;-)
 
R

Roy Johnson

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)
 
D

David Filmer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top