On 11-08-30 01:14 PM, Rob Dixon wrote:
On 30/08/2011 17:54, Emeka wrote:


$s =~ s{(\w+)}{scalar reverse($1)}eg; --- From Shlomi Fish

perl -e'@words=split/\s+/,$ARGV[0];$_=reverse$_
for@words;print"@words\n";' 'abcd efgh ijkl mnop' Shawn Corey

print join ' ', map scalar reverse($_), split ' ', $str; Rob Dixon

It is time we explain our codes ... I don't think that Narasimha will
easily wrap his head around the first two.

(Once again Emeka, please bottom-post your responses and edit what you
are quoting to what is relevant. Thanks :)

I agree. It is a shame that senior and supposedly wise contributors here
feel the need to boost their egos by posting obscure code. It is
primarily a /beginners/ list, and should be treated as such. If you
cannot teach then please hold off from showing us your wares. Perl
already has a reputation for being incomprehensible without people who
should know better proving that they are right.

In particular I believe Perl should be treated as a programming
language. Its flexibility allows a short script to be passed on the
command line but, unless the question requires it, a perl -e "script"
solution is an ugly way to express a solution. In particular it does not
allow us to insist on the mandatory

use strict;
use warnings;

header, and the consequent declaration of lexical variables.

Rob


OK:

#!/usr/bin/env perl

# prevent silly mistakes
use strict;

# issue warnings of possible mistakes
use warnings;

# read from Perl's data file handle, contents appear after __DATA__
while( my $line = <DATA> ){

  # remove trailing newline
  #   a newline is whatever is in $/, the $INPUT_RECORD_SEPARATOR
  #   see `perldoc perlvar` and search for /\$INPUT_RECORD_SEPARATOR/
  chomp $line;

  # split on whitespace, ignore leading and trailing empty elements
  #   see `perldoc -f split`
  my @words = split ' ', $line;

  # do each word, one at a time
  foreach my $word ( @words ){

    # reverse each word w/ Perl's reverse().  See `perldoc -f reverse`
    #   since this is scalar context, the string is reversed
    # since $word is the `for` iteration variable,
    #   any changes to it are stored back in @words
    $word = reverse $word;

  } # end foreach $word

  # use Perl's stringification to join the words back into a line
  #   and print the original line and the modified one
  print "$line  :  @words\n";

} # end while <DATA>


# The lines after the __DATA__ or __END__
#   are read by the <DATA> file handle
__DATA__
abcd efgh ijkl mnop



--
Just my 0.00000002 million dollars worth,
  Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software:  Fail early & often.

Eliminate software piracy:  use only FLOSS.

"Make something worthwhile."  -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to