In search of elegant code: inverting a string

Discussion in 'Perl Misc' started by David Filmer, Oct 29, 2003.

  1. David Filmer

    David Filmer Guest

    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.
    David Filmer, Oct 29, 2003
    #1
    1. Advertising

  2. David Filmer

    Jay Tilton Guest

    (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;
    Jay Tilton, Oct 30, 2003
    #2
    1. Advertising

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

    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).
    Malcolm Dew-Jones, Oct 30, 2003
    #3
  4. On 29 Oct 2003 15:50:48 -0800,
    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?
    > }


    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.
    |
    Martien Verbruggen, Oct 30, 2003
    #4
  5. David Filmer

    TDN Guest

    TDN, Oct 30, 2003
    #5
  6. >>>>> "Purl" == Purl Gurl <> 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
    <> <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!
    Randal L. Schwartz, Oct 30, 2003
    #6
  7. David Filmer

    Bart Lateur Guest

    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.
    Bart Lateur, Oct 30, 2003
    #7
  8. David Filmer

    Roy Johnson Guest

    (Malcolm Dew-Jones) wrote in message news:<>...
    > 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)
    Roy Johnson, Oct 30, 2003
    #8
  9. David Filmer

    David Filmer Guest

    >
    > 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.
    David Filmer, Oct 30, 2003
    #9
  10. David Filmer

    Russ Jones Guest

    Purl Gurl <> wrote in news:3FA138A4.AB00C340
    @purlgurl.net:

    > Purl Gurl


    I can't take any more. plonk.
    Russ Jones, Oct 30, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Wessle

    inverting a string

    Gary Wessle, Aug 17, 2006, in forum: C++
    Replies:
    2
    Views:
    451
    Jerry Coffin
    Aug 18, 2006
  2. Gregory Brown

    Elegant solution for inverting zip?

    Gregory Brown, Jul 14, 2009, in forum: Ruby
    Replies:
    3
    Views:
    84
    Brian Candler
    Jul 14, 2009
  3. David Filmer
    Replies:
    9
    Views:
    105
  4. David Filmer
    Replies:
    11
    Views:
    210
    Brad Baxter
    Dec 5, 2003
  5. David Filmer
    Replies:
    8
    Views:
    113
    geoffroy
    Dec 14, 2003
Loading...

Share This Page