in reply to Lewis Carroll's Code

Here are some thoughts I had about my code:
--
($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Replies are listed 'Best First'.
Re: Re: Lewis Carroll's Code
by sauoq (Abbot) on Jul 18, 2002 at 04:10 UTC
    You might want to consider something like:
    my @abc = ('a'..'z'); my %abc = map { ($abc[$_],$_) } (0..25); my @key = map { $abc{ lc() } } $key =~ /([a-z])/ig;
    To use it, loop through the characters in your message any way you like. The following assumes you have the current letter in $c , that $i holds your position in @key , and that the result will be in $result . . .
    my $new_c; if ($c =~ /[a-z]/i) { $new_c = $abc[($abc{lc($c)} + $key[$i]) % 26]; $new_c = uc($new_c) if ($c =~ /[A-Z]/); $result .= $new_c; $i = ($i + 1) % @key; } else { $result .= $c; }
    You could use the same code above for decoding if you changed @key to hold negative numbers instead of positives.
    
    -sauoq
    
    "My two cents aren't worth a dime.";
    
Re: Re: Lewis Carroll's Code
by cfreak (Chaplain) on Jul 18, 2002 at 04:15 UTC
    grep may be what you are looking for. it uses a regex rather than a string though:

    From the perldoc:

    grep BLOCK LIST grep EXPR,LIST This is similar in spirit to, but not the same as, grep(1) and its relatives. In particular, it is not limited to using regular expressions. Evaluates the BLOCK or EXPR for each element of LIST (locally setting "$_" to each element) and returns the list value consisting of those ele­ments for which the expression evaluated to true. In scalar context, returns the number of times the expression was true. @foo = grep(!/^#/, @bar); # weed out comments or equivalently, @foo = grep {!/^#/} @bar; # weed out comments

    Hope that helps
    Chris

    Lobster Aliens Are attacking the world!
      I'm confused -- what is it you want me to use GREP for?
      --
      ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

        Sorry, I was saying that you could use grep() on an array like you would use index() on a string, its not exactly the same though.... I think I better go to bed I'm not making much sense and I'm getting myself downvoted :)

        Lobster Aliens Are attacking the world!