Simple Encryption Script

T

Tad McClellan

Nikos said:
Iam also using this but i cant quite understab how it works:

i mean how split manages to split $text's each letter? what // really does?


// matches the empty string.

letters in $text dont really have a space between them


but they do all have empty strings between them.
 
T

Tad McClellan

Nikos said:
foreach ( @letters ) <-- this can also be writen as

foreach $_( $letters )

foreach ( $letters[0]..$letters[@letters] )

right?


What happened when you tried it?
 
N

Nikos

A. Sinan Unur said:
ah, i see. I thought so far that $_ just copied itself the current
element of array @letters but it was just a copy, meaning that if any
change were applied to $_ it wouldnt impact at all each of the
elements.


From perldoc perlsyn

Foreach Loops
...
If any element of LIST is an lvalue, you can modify it by modifying VAR
inside the loop. Conversely, if any element of LIST is NOT an lvalue,
any attempt to modify that element will fail. In other words, the
"foreach" loop index variable is an implicit alias for each item in the
list that you're looping over.

foreach ( @letters ) <-- this can also be writen as

foreach $_( $letters )

foreach ( $letters[0]..$letters[@letters] )

right?


Wrong.

use strict;
use warnings;

my @x = 1, 2, 3;

for ($x[0] .. $x[@x]) {
print "$_\n";
}

__END__

D:\Home> f
Use of uninitialized value in foreach loop entry at D:\Home\f.pl line 6.

Guess why.

Sinan
I cant guess. it seems logical to me. i dont knwo why it aint working.
 
S

Spock

You might want to actually, y'know, try it before you state it doesn't
work. Or you could read the documentation for foreach.

But i did, because you are adding chars it didnt work.
 
A

Anno Siegel

John W. Krahn said:
You are adding one to the character returned from chr()?

Oops. Should be "chr 1 + ord $_", of course. Thanks for pointing it
out.

Anno
 
A

Anno Siegel

Chris Mattern said:
You might want to actually, y'know, try it before you state it doesn't
work. Or you could read the documentation for foreach.

Well, in fact it doesn't work as intended, as John W. Krahn pointed out
("1 + chr ord $_" should be "chr 1 + ord $_"). But that's a different
problem than the non-existent one Nikos anticipated.

Anno
 
N

Nikos

Anno said:
Oops. Should be "chr 1 + ord $_", of course. Thanks for pointing it
out.

Yes. Guys i noticed that you dont use parenthessis in functions. why?
isnt it better to see and read a function containing parenthesis?
 
A

Anno Siegel

Nikos said:
Yes. Guys i noticed that you dont use parenthessis in functions. why?
isnt it better to see and read a function containing parenthesis?

Shrug. It's a matter of style. Most people seem to prefer putting
parentheses only where they are needed. Sometimes a pair of not-strictly-
necessary parens can make things clearer. Always writing all parens
clutters the code.

Anno
 
J

John W. Krahn

Nikos said:
Yes. Guys i noticed that you dont use parenthessis in functions. why?
isnt it better to see and read a function containing parenthesis?

That is because we are using operators which don't require parentheses instead
of functions which do. (The operators 'chr', '+' and 'ord' to be specific.)

perldoc perlop


John
 
S

Sherm Pendley

John said:
That is because we are using operators which don't require parentheses
instead of functions which do. (The operators 'chr', '+' and 'ord' to be
specific.)

Um... Did I miss something? chr() and ord() are both listed in perlfunc.
And functions in general often don't require parentheses.

sherm--
 
J

Joe Smith

Nikos said:
$i=0;
foreach (@letters) {
$letters[$i++] = chr( ord($_) + 1 );
}

Since you are using $letters[$i] in the loop, you shouldn't be
using foreach(@letters){}.

When working with indexes into an array (as opposed to the elements
in the array), start at 0 and end at the highest array index.

foreach my $i (0 .. $#letters) {
$letters[$i] = chr(ord($letters[$i])+1);
}

-Joe
 
J

Joe Smith

Nikos said:
Until now i thoughs that if we ommited the doubles quotes in a print it
would be tha same as haveing it, meaning that the $variable or @array
would still be interpolated.

But why is that difference?

It was deliberately designed that way.
The perl special variable $" was created for that difference.

perl -le '@a=qw(a b c); $" = q{_*_}; print @a; print "@a"'
abc
a_*_b_*_c

-Joe
 
J

Joe Smith

Nikos said:
a-z transletes to b-z
A-Z msut to B-Z why you say A-Za ?

If you use tr/A-Z/B-Z/ then Y translates to Z, but the character
that Z translates to is not specified. So either
tr/A-Z/B-ZA/
or
tr/A-Z/B-Z[/
is appropriate.

Your program should explicitly document what it does to
characters that are outside of the A-Z range.
-Joe
 
T

Tassilo v. Parseval

Also sprach Sherm Pendley:
Um... Did I miss something? chr() and ord() are both listed in perlfunc.
And functions in general often don't require parentheses.

Perl is one of the few languages which blurs the distinction between
operators and functions. In most languages, a function is syntactically
different from an operator in that it requires parentheses. Therefore,
in C++ 'new' and 'delete' are operators. Also, operators very often are
in infix notation whereas a function is prefix with respect to its
arguments.

In Perl, the parentheses-rule-of-thumb does not exist.

Tassilo
 
J

John W. Krahn

Sherm said:
Um... Did I miss something? chr() and ord() are both listed in perlfunc.
And functions in general often don't require parentheses.

Functions without parentheses ARE operators.

perldoc perlfunc
[snip]
Any function in the list below may be used either with or without
parentheses around its arguments. (The syntax descriptions omit the
parentheses.) If you use the parentheses, the simple (but occasionally
surprising) rule is this: It looks like a function, therefore it is a
function, and precedence doesn't matter. Otherwise it's a list
operator or unary operator, and precedence does matter.


perldoc perlop
[snip]
Terms and List Operators (Leftward)

A TERM has the highest precedence in Perl. They include variables,
quote and quote-like operators, any expression in parentheses, and any
function whose arguments are parenthesized. Actually, there aren't
really functions in this sense, just list operators and unary operators
behaving as functions because you put parentheses around the arguments.
These are all documented in perlfunc.

If any list operator (print(), etc.) or any unary operator (chdir(),
etc.) is followed by a left parenthesis as the next token, the
operator and arguments within parentheses are taken to be of highest
precedence, just like a normal function call.

In the absence of parentheses, the precedence of list operators such as
"print", "sort", or "chmod" is either very high or very low depending
on whether you are looking at the left side or the right side of the
operator.


cat Changes5.000
[snip]
All functions have been turned into list operators or unary operators,
meaning the parens are optional. Even subroutines may be called as
list operators if they've already been declared.


You are probably thinking of Perl4 where functions actually were functions.

:)

John
 
N

Nikos

Joe said:
perl -le '@a=qw(a b c); $" = q{_*_}; print @a; print "@a"'
abc
a_*_b_*_c

-Joe

I see. But how is this helpfull?
I cant think of a reason other that seperate the values of the @array
when it comes to print "@array";
 
N

Nikos

Joe said:
Nikos said:
a-z transletes to b-z
A-Z msut to B-Z why you say A-Za ?


If you use tr/A-Z/B-Z/ then Y translates to Z, but the character
that Z translates to is not specified. So either
tr/A-Z/B-ZA/
or
tr/A-Z/B-Z[/
is appropriate.
Your program should explicitly document what it does to
characters that are outside of the A-Z range.

Yes, now i think i understand. So the 'A' in tr/A-Z/B-ZA/ means that
every other character outside the A-Z range should be changes to A

and to the 2nd example with [ ?
 
A

Anno Siegel

Joe Smith said:
If you use tr/A-Z/B-Z/ then Y translates to Z, but the character
that Z translates to is not specified. So either

Ah but it is, just not explicitly:

Otherwise, if the REPLACEMENTLIST is shorter than
the SEARCHLIST, the final character is replicated
till it is long enough. If the REPLACEMENTLIST is

So "Z" is translated to "Z".

Anno
 
A

A. Sinan Unur

Nikos said:
I see. But how is this helpfull?

If you need to ask that, then you do not see.

#! /usr/bin/perl

use strict;
use warnings;

my $data = [ [ Chicago => 1000 ], [ Tulsa => 2000 ] ];
output_sep_values($data, sepchar => "\t");
output_sep_values($data);


sub output_sep_values {
my $data_ref = shift;
my %args = (
sepchar => ',',
@_,
);
{
local $" = $args{sepchar};
for my $row (@{ $data }) {
print "@{ $row }\n";
}
}
}


__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top