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

A piece of cake. 2

Status
Not open for further replies.

stormbind

Technical User
Mar 6, 2003
1,165
0
0
GB
First, thanks for reading! :)

I don't know PERL. The little I did know seems to have been forgotten.

The following script was translated from a JScript which has similar syntax. I would appreciate it if someone could look over it and fix errors.

Thanks very much for your time.

Code:
#!perl

file open="data.txt";

// data.txt is a flat file. I only want to read it's contents.

var cY = split (//, file);

// cY is a new array. Each member contains one character from data.txt (including spaces)

var i=0;
var b;
var c;
while (i<cY.length+3) {
	b=i;
	c=b+2;
	i=i+3;
	while (b-1!=c) {
		if (cY[c]) {eY=eY+cY[c];} else {eY=eY+' ';}
		c--;
	}
}
cY = join (//, cY);

// cY is now one long string

print '<body onload=&quot;parent.decrypt($cY)&quot;></body>';
}

// print HTML and exit

P.S. Yes, I know the encryption is really pathetic but it's just to test a concept :)
 
I remember a little bit now. I think it should be more like this, but I'm getting the dreaded Error 500! :(

#!perl

print &quot;Content-type: text/html\n\n&quot;;
print &quot;<html>\n&quot;;

open FILE, &quot;<data.txt&quot;;

var cY = split (//, FILE);

close FILE;

var i=0;
var b;
var c;
while (i<cY.length+3) {
b=i;
c=b+2;
i=i+3;
while (b-1!=c) {
if (cY[c]) {eY=eY+cY[c];} else {eY=eY+' ';}
c--;
}
}
cY = join (//, cY);

print &quot;<body onload=\&quot;parent.decrypt('$cY')\&quot;></body>\n';
print &quot;</html>\n&quot;;
 
Hi,

I can see you have really forgotten Perl ;-)

Try this:

#!/usr/bin/perl

open(FILE,&quot;data.txt&quot;);

// data.txt is a flat file. I only want to read it's contents.
@content=<FILE>;
close(FILE);

@cY=();
foreach(@content)
{
@tmp = split (//, file);
push(@cY,@tmp);
}

// cY is a new array. Each member contains one character from data.txt (including spaces)

$i=0;
$len=@cY;
$eY=&quot;&quot;;
while (i<$len+3) {
$b=$i;
$c=$b+2;
$i=$i+3;
while (($b-1)!=$c) {
if ($cY[$c]) {$eY.=$cY[$c];} else {$eY.=' ';}
$c--;
}
}
$cY = join (&quot;&quot;, @cY);

// cY is now one long string

print &quot;Content-Type: text/html\n\n&quot;;
print &quot;<body onload=\&quot;parent.decrypt($cY)\&quot;></body>&quot;;
}

Hope that is what you wanted!

--
Smash your head on keyboard to continue...
 
Thanks, it looks good but I cannot get it to work. There's a glitch in it somewhere.

I have to admit, PERL looks really foreign! Swapping between languages can be really hard on the eyes :(

Anyway, I tried with &quot;Hello World&quot; in data.txt and nout happened except for another Error 500. The hard drive went mad for a couple of seconds - suggesting to me there's an endless loop in there but I don't see it.

I don't quite understand this snippet. If @content is an array, is the entire text in content[0]?

@cY=();
foreach(@content)
{
@tmp = split (//, file);
push(@cY,@tmp);
}

I accidentally discovered in JScript that if you split a regular variable it is automatically made into a new array (this doesn't seem to be documented in the language reference). Can't PERL do something similar?

@cY=split (//, file);

or

@cY=<FILE>
@cY=split (//, @cY);

Lastly, you put split(//) and join(&quot;&quot;).

I hope none of this sounds like whining, I really do appreciate the time people give to helping others on these forums :)
 
Incase anyone isn't sure. This is what it should do:

Open the file, paste the contents to HTML with each set of 3 characters flipped.

HELLO WORLD ==> &quot;LEH OLROW LD&quot;

Security isn't the issue at the moment, but I want to see it working. I have it working within the browser environment but the plan is to communicate with a server :)
 
stormbind,
@content is an array with n elements where n = number of lines in the file. So if your text file only has one line everything will be stored in $content[0].

One way to read all the data from the file and split into array of chars would be something like:
Code:
$/=undef;
open(FILE,&quot;data.txt&quot;);
@cY=split(//, <FILE>);
close(FILE);
As fo the // in split, but &quot;&quot; in join. split takes a patern to split on, where as join needs a string to join with. If you tried using // in the join you would get the last patern match not null or undef as you intended.

Hope some of this helps.
 
It's all helping :]

I still don't have it working though. The CPU is getting thrashed and I'm not recieving any response through the browser - had to manually shutdown perl.exe :(

This is it at the moment. Btw, I'm not sure what $/undef; does LOL :)

#!perl

$/=undef;
open(FILE,&quot;data.txt&quot;);
@cY=split(//, <FILE>);
close(FILE);

$i=0;
$len=@cY;
$eY=&quot;&quot;;
while (i<$len+3) {
$b=$i;
$c=$b+2;
$i=$i+3;
while (($b-1)!=$c) {
if ($cY[$c]) {$eY.=$cY[$c];} else {$eY.=' ';}
$c--;
}
}
$cY = join (&quot;&quot;, @cY);

print &quot;Content-Type: text/html\n\n&quot;;
print &quot;<body onload=\&quot;parent.decrypt('$cY')\&quot;></body>&quot;;
 
Ok. Some success! (i ==> $i)

It printed this:

<body onload=&quot;parent.decrypt('hello world')&quot;></body>

Sooo... it didn't work... but it printed something. Whoohoo! :D
 
Sorted fixed. Thanks all! Stars and icecream all round :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top