Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am very new to pearl and have just written my first program i am trying to decrypt a file which contains acii text and is converted in a caesar shift the key is 45 and i have written the following code but cant get it to ru where am i going wrong?? the code is as follows
#!/usr/bin/perl -w use strict; Open (cypher.txt, "C:\perl\cypher.txt") || die "cannot open cypher: $! + "; Open (results.txt, ">C:\perl\results.txt") || die "cannot write result +s: $! "; while ($l != E_O_F) { $l = getc(cypher.txt, "C:\perl\cypher.txt"); if (item ord ($l) - 45 <= 0) { $l = item chr (128 - (45 - (item ord ($l)); } else { $l = item chr (item ord ($l) - 45); } print "results.txt $l"; } }

Replies are listed 'Best First'.
(Ovid) Re: ascii conversion
by Ovid (Cardinal) on Nov 23, 2000 at 03:51 UTC
    With all due respect, this screams "homework" to me. If so, it's nice if you could let us know so that we can assist you without giving you the answers.

    I'd like to say that if you are new to Perl, I congratulate you on using -w and strict. However, I am surprised that you didn't see the warning about trying to modify a read-only value: $1. The "dollar digit" ($1, $2, $3, etc) variables are only assigned to by what is called a "back reference" in a regular expression. See perlre for details.

    Since I can't tell if this is homework or not, I'll just provide some hints. I would probably use something like the following:

    s/(.)/chr((ord $1)-45)/ge;
    However, if this is homework, your instructor is going to ask you how you figured out how to do that. It also has the problem that it will convert more than letters (which is fine if those are your specs).

    Something closer in spirit to what you want: use a while loop to read from your filehandle and then split each line into an array and iterate over the array. getc is horribly inefficient. Once you've converted the individual characters, join the array back together and write it out to your output file.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      thanks for your help i had had this previously as an assignment but i managed to create a working program in vb but am interested to see how i would do this in perl to help my progression in perl. could you also recommend a good book 4 beginners in perl siting many useful examples as i have to do a web based project later this year using perl in creating cgi scripts. is the perl cookbook any good????

        For good books, check out the site's Reviews section and see what other monks have had to say.

        For a real newbie to Perl, I wholeheartedly recommend Learning Perl.

        Perl Cookbook also rocks, as does Programming Perl ... try O'Reilly's web site and look at their Perl catalog.

        I've heard that there are also good books on Perl by other publishers ... =) (really a joke, see my review of one such book )

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Re: ascii conversion
by repson (Chaplain) on Nov 23, 2000 at 08:32 UTC
    If you want to understand why perl is such a good language for processing data have a look at this sample which (someone correct me if im wrong) seems to do what you are trying to do. Looking into regular expressions (re's) can be very useful but this example is maybe to complex to start from if you are a newbie.
    while(<>) { s/(.)/ (ord($1) <= 0) ? chr(128-(45-ord($1))) : chr(ord($1)-45)/ge; print; {
    Note: you did say that this was not currently for homework, didn't you?
Re: ascii conversion
by arturo (Vicar) on Nov 23, 2000 at 04:11 UTC

    If you could focus your question more, it might help us to help you. What isn't working, exactly? What else have you tried? Have you inserted a few prints to help you in debugging (if you've got that far; under use strict and the -w switch your while loop is going to make the interpreter complain about not having declared $l -- not to mention that it's open not Open) Update I've been assured that it's $ one, not $ ell; I blame my fonts =)

    Here are a couple of things to look at ..

    Check out perldoc perlstyle ( or, on this site, perlman:perlstyle -- it can help you make your code more idiomatic -- in particular your filehandles look strange to a perl-er.)

    A couple other general bits of advice: Perl isn't C; you don't have to treat strings as arrays of characters (strings are 'native' to Perl).

    If you want to traverse a string character-by-character, you might look into using the substr function and a for loop (since you seem to be coming at this from a C background, I don't expect you'll have a huge problem with that =)..

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor