Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

command-line Perl statement 1

Status
Not open for further replies.

OieL

MIS
Sep 6, 2002
17
0
0
US
Hi,

how can I do this in a command line Perl statement ... print in reverse order sets of characters A - Z and a - z . with this sample output ..

Z
Y
X
.....

c
b
a

thanks

 
Man, you gotta be kidding. It's that easy? Look at the program I just wrote to post here. It's not command line, but I figured it would help. Guess I was wrong:

#!/usr/bin/perl -w
#AUTHOR: Greg Norris (Greg@GregNorris.net)
#FILE: print_reverse_string - Prints a list of characters in reverse order.

use strict;
use warnings;

my ($string, $length, $lastCharIndex, $lastChar );

$string = "ABCDEFG";
$length = length($string);
$lastCharIndex = ($length -1);

while ($string)
{
$lastChar = substr($string, $lastCharIndex, 1);
print "$lastChar \n";
chop $string;
$lastCharIndex --;
}
exit;

Oh well, I had fun with it.

-Greg

PS- Oiel, I hope you gave justice41 a vote. I did. _______________________________________
constructed from 100% recycled electrons
 
By the way, how would you do that in a script? I'm not able to get it to work. _______________________________________
constructed from 100% recycled electrons
 
There are several ways to write this as a script. A 'normal' way (normal for Perl, anyway) would be
Code:
foreach $chr ( reverse 'a'..'z','A'..'Z' ) {

    print "$chr\n"
}
(quotes around the chars were added so it would run under the strict pragma)

jaa
 
Wow, that's so cool! Thanks a bunch. And I use the strict pragma, so I appreciate you mentioning that. _______________________________________
constructed from 100% recycled electrons
 
The command line: perl -le 'print for reverse a..z,A..Z' seems to be printing in one line. How do you print each character in its own line?
 
The '-l' switch is used to set the output line terminator. If no (octal) value is given after the '-l', then it sets it to the input record separator, which is the newline by default. If the bare '-l' doesn't work for you, you can set it explicitly by adding the octal value of your favorite line terminator character after the '-l'
Code:
perl -l012 -e 'print for reverse a..z,A..Z'
jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top