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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

break out STDIN into single letters 1

Status
Not open for further replies.

polka4ever

Technical User
Jan 25, 2006
42
US
Hello, I am new to perl and working my way through the Learning Perl book. I'm only on page 37, so bear with me!!!

I want to take STDIN and break it into letters. It's a decoder program. Like, for example, if someone types in: LC8Q4 CBDSN XQRHD NKJXB HR into STDIN ... it will come out as BECAR FULWH ATYOU WISHF OR.

I know I can do :

$line = <STDIN>;
if ($line eq "L") {
print "B"; } else {
...

But i need to break the line into its components. know what i mean? I probably have to load it into an array ... like @line = <STDIN> or something???

any hints?
 
Code:
@letters=split (//, $line);

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
If you're decoding and it's a one-to-one relationship, why break up the source?
You could simply create a translate string and use "tr" to convert from the input to the output.

From what I can see, though, you either have a more complex relationship than one-to-one in your example or you have a slight error. You have C translating to E and later to F.


Trojan.
 
If you want to read char by char from stdin...
Code:
#! perl

binmode STDIN;
binmode STDOUT;

my $ch = "";

while( read( STDIN, $ch, 1) ) {    
    last if( "\r" eq $ch or "\n" eq $ch);
    # simple encoder/decoder
    $ch = chr(ord($ch) + 1);    
    print STDOUT $ch.",";
}
 
Hello! thank you so much for the valuable input. Forgive the slowness of my brain - this is not something I particularly EXCEL at (at least not yet - let's hope that changes). I *would* use the "tr" option - but it's only mentioned very briefly in 'Learning Perl' and I'm not sure I can really figure that out yet ...

So, I think I'd like to continue on with loading each letter into an array and reading it that way. So, I guess what I need to do next is - now that I've used Paul's advice to split it out, is loop through the values and convert them to their new values. Easy peasy, right???

The values are static ... so A will always equal 5, B will always equal F and C will always equal E.

I've got to get the 'if' statement correct, array's are in chapter 3 and i'm only on chapter 2 right now. GOD it's overwhelming and i have a lot to learn, but a journey of a thousand miles begins with a single step, I am told ;-)

how do i go about looping through the values?

n=0;
while (@letters) {
$a += $n;
if $letters[$a] = 'A' {
print '5';]} else {
if $letters[$a] = 'B' {
print 'F';]} else {
if $letters[$a] = 'C' {
print 'E';}
}

this isn't quite right ... any more hints???
 
Aaaaaargh!
Firstly you're gonna break it up in to characters.
Then it turns out there IS a one-to-one relationship.
Finally, to really scare me senseless you're gonna use "if" "else" clauses!
ok, to explain "tr" to you a little.
Lets take your example where A=5, B=F and C=E.
To convert that you simply:
Code:
$data =~ tr[ABC][5FE];
print "Result is [$data]\n";
The "from" characters go in the first square brackets.
The "to" characters go in the second square brackets.
That's it!
That simple.
String converted.
Have a think about it and I'm sure you'll see things from my point of view! ;-)



Trojan.
 
holy crap! that *IS* simple :) thanks Trojan and sorry for the scare ;-)
 
HEY! can i ask another question? So i wrote out an decoder and an encoder. But with the encoder - i want the blocks to be spaced in groups of 5. So if i wanted to say "I wrote a perl to encode and decode" - the program would return this to me:

KN4H5 CQ9C4 S5HCO 8HFCQ OFFC8 HFC

instead of this:

K N4H5C Q 9C4S 5H CO8HFC QOF FC8HFC

know what i mean?

also - i'm doing this on WINDOWS - (i know, BLECH!) and the command line environment doesn't let me select and copy easily (like I can't grab the output from the screen by selecting it with my mouse and copying it) - is there a more UNIX-like (like secure CRT or puTTY) type cmd line utility for WINDOWS/DOS that you could share with me ???

thank you so MUCH For all the help!!!
 
i just tried Cygwin, but i still can't copy and paste from the screen ... whats up with that?
 
You can copy and paste out of the cmd shell (and the command shell as well.)

Right click in the cmd/command window, select mark, highlight the lines you want and (at least with the cmd shell) you hit enter and it will copy all the lines you selected to the clipboard - then paste away!
 
nice! thanks rharsh for the tip!

any thoughts on how to break it up into groups of 5, putting a space in-between?
 
Read up on cryptograhy ... seriously ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
If you're trying to encode anything that needs to be secure, I'd take Paul's suggestion. But, for what you're trying to do, here's a similar application that implements ROT13.
Code:
my $input = 'I wrote a perl to encode and decode';
print "Message: $input\n";

my @encode = map {rot13_encode($_)} split(//, uc($input));
print "Encoded: ";
for (my $i = 0; $i <= $#encode; $i++) {
    print $encode[$i];
    print " " unless ($i+1) % 5;
}
print "\n";

my @decode = map {rot13_encode(uc($_))}  @encode ;
print "Decoded: ";
for (my $i = 0; $i <= $#decode; $i++) {
    print $decode[$i];
    print " " unless ($i+1) % 5;
}
print "\n";

sub rot13_encode {
    return if not /[A-Z]/;
    tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/NOPQRSTUVWXYZABCDEFGHIJKLM/;
    return $_;
}
 
Block of 5 is trivial:
Code:
echo 'abcdefghijklmnopqrstuvwxyz' | perl -lne 's/(.....)/$1 /g;print;'
In a Unix or Linux environment generates:
Code:
abcde fghij klmno pqrst uvwxy z
It's the regex that's relevant to you but I thought I'd show you a "one-liner" for completeness. :)
Code:
s/(.....)/$1 /g

Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top