string example in perl, python, and ruby

2 views
Skip to first unread message

Robert Citek

unread,
Nov 16, 2009, 9:39:17 AM11/16/09
to stl...@googlegroups.com
Hello all,

I'm showing some examples to some colleagues of the differences in how
perl, python, and ruby do string manipulation. Are the examples below
a fair representation of writing clearly in each of the languages or
could they be written more clearly?

Regards,
- Robert

-----

# a perl example
perl -le '
print "Hello, World" ;
print scalar reverse("Hello, World") ;
print lc(reverse("Hello, World")) ;
print substr(lc(reverse("Hello, World")),-2,2) ;
print substr(lc(reverse("Hello, World")),-2,2) . "?" ;
'

# a python example
python -c '
print "Hello, world"
print "Hello, world"[::-1]
print "Hello, world"[::-1].lower()
print "Hello, world"[::-1].lower()[-2:]
print "Hello, world"[::-1].lower()[-2:] + "?"
'

# a ruby example
ruby -le '
print "Hello, World"
print "Hello, World".reverse
print "Hello, World".reverse.downcase
print "Hello, World".reverse.downcase.slice(-2..-1)
print "Hello, World".reverse.downcase.slice(-2..-1) + "?"
'

Robert Citek

unread,
Nov 16, 2009, 10:02:27 AM11/16/09
to stl...@googlegroups.com
Might as well throw in JavaScript (via Spidermonkey), too:

smjs -e '
print("Hello, World");
print("Hello, World".split("").reverse().join("")) ;
print("Hello, World".split("").reverse().join("").toLowerCase()) ;
print("Hello, World".split("").reverse().join("").toLowerCase().concat(".").slice(-3,-1))
;
print("Hello, World".split("").reverse().join("").toLowerCase().concat(".").slice(-3,-1)
+ "?") ;
'

Regards,
- Robert

Craig Buchek

unread,
Nov 16, 2009, 1:51:12 PM11/16/09
to Saint Louis Ruby Users Group
It would be more idiomatic Ruby to use puts instead of print. Also,
you can slice strings in Ruby similar to Python:

puts 'Hello, World'.reverse.downcase[-2..-1] + '?'

I also consider it more correct (in all these languages) to use single
quotes by default, unless double quotes are required. It doesn't make
a significant difference though -- it's just that the single quote
versions are (very) slightly faster, and faster to type. Of course,
you could make an argument that you need the double quotes here, since
they're inside the single quotes of the enclosing shell line. ;)

Craig

Robert Citek

unread,
Nov 24, 2009, 1:52:55 PM11/24/09
to stl...@googlegroups.com
On Mon, Nov 16, 2009 at 1:51 PM, Craig Buchek <craig....@gmail.com> wrote:
> It would be more idiomatic Ruby to use puts instead of print.
>
> Also, you can slice strings in Ruby similar to Python:
>
>  puts 'Hello, World'.reverse.downcase[-2..-1] + '?'

Yup, lot of variation is possible. I was trying to make each script
the most readable in that language and the most familiar to those not
versed in that language.

> I also consider it more correct (in all these languages) to use single
> quotes by default, unless double quotes are required. It doesn't make
> a significant difference though -- it's just that the single quote
> versions are (very) slightly faster, and faster to type.

Interesting. I usually go the other way, using double quotes by
default unless I need single quotes, mainly because double quotes
allow for interpolation, whereas single quotes do not.

> Of course, you could make an argument that you need the double quotes here, since
> they're inside the single quotes of the enclosing shell line. ;)

That, too. :)

Regards,
- Robert
Reply all
Reply to author
Forward
0 new messages