# need some helpo with Perl

Discussion in 'OT Technology' started by GotVtec, Jun 10, 2005.

1. ### GotVtec8th World Wonder

Joined:
Mar 14, 2000
Messages:
575
0
Location:
Omaha, NE
I have this snipet of code I need converted to PHP, anyone up to the task?

Code:
```# the first pair are absolute coords, where to put down the pen
# subsequent pairs are relative.
# so, a file like:
#    10,20
#    30,10
#    5,5
#
# becomes @coord = (10,20,20,-10,-25,-5,9999)
# 9999 is the end of stream marker.

my (\$max_x, \$max_y);
\$max_x = \$max_y = 0;
my (\$old_x, \$old_y);
my @coord;

while (<>) {
chomp;
s/\s//g;
my (\$x, \$y) = split ',';

\$max_x < \$x and \$max_x = \$x;
\$max_y < \$y and \$max_y = \$y;

my (\$diff_x, \$diff_y);

if (defined \$old_x) {
\$diff_x = \$x - \$old_x;
\$diff_y = \$y - \$old_y;
} else {
\$diff_x = \$x;
\$diff_y = \$y;
}

\$old_x = \$x;
\$old_y = \$y;

push @coord, \$diff_x, \$diff_y;
}

my \$width = \$max_x + 10;
my \$height = \$max_y + 10;

push @coord, END_OF_STREAM;

my \$path;

for my \$c (@coord) {

# coord can be positive or negative (as it is the diff of last point)
#
# coord  0  -1 1  -2 2  -3 3  ...
# f      0  1  2  3  4  5  6  ...
my \$f = (abs(\$c) << 1) - (\$c < 0);

# now, break \$f into groups of five bits. starting from lowest to highest.
# each of those groups gets a char.
do {
# get the last five bits of f.
my \$e = \$f & 31;

# shift \$f over 5 bits, for next time
# AND, if there's anything left of \$f, add a high bit to \$e. this
# tells the decoder to keep going.
(\$f >>= 5) && (\$e |= 32);

# put it into ascii
\$path .= chr(\$e+63);

} while (\$f != 0);

}
```

Last edited: Jun 10, 2005
2. ### AstroCode Monkey

Joined:
Mar 18, 2000
Messages:
2,047
0
Location:
Cleveland Ohio
PHP:
``` \$pi = 3.141593; \$circ = 2*\$pi; \$radius = 50; \$x_center = 50; \$y_center = 50; \$step = 4/\$radius; for (\$theta = 0; \$theta <= \$circ; \$theta += \$step) { \$x = int (\$radius * cos(\$theta)); \$y = int (\$radius * sin(\$theta)); print ((\$x_center+\$x) . ", " . (\$y_center+\$y) . "\n"); } ```
Untested, but should work. A little history: PHP grew up based off of Perl...

3. ### AstroCode Monkey

Joined:
Mar 18, 2000
Messages:
2,047
0
Location:
Cleveland Ohio
Hmm... I post and then there's an edit to the original Perl code. Disregard my first post. Currently reviewing new code to see if its possible...

Edit: Reviewed the code. Its definitely possible (at least at first glance) although would require a bit more time then I'm willing to spend translating it.

4. ### GotVtec8th World Wonder

Joined:
Mar 14, 2000
Messages:
575
0
Location:
Omaha, NE
Sorry, about that Astro, I had my first snippet of code in there on accident.

I am having a tough time trying to figure out how to convert that other stuff, my Perl skills are very poor.

5. ### AstroCode Monkey

Joined:
Mar 18, 2000
Messages:
2,047
0
Location:
Cleveland Ohio
I'm not very fluent in Perl either. What I find with Perl is it has a ton of shortcuts built in. It allows for some really cool tricks, but at the same time makes it a bit tough to figure out. I'd say take it one step at a time and decode each line/structure one at a time. I could probably do it, but I've got my own dragons to slay right now...

6. ### sammNext in Line

Joined:
Dec 22, 2000
Messages:
2,630
0
Location:
San Jose, CA
for a good laugh I like to read the submissions to the obfuscated perl coding contests. Some of the entries look like they could be line noise... Or maybe you could pipe /dev/random into the perl interpreter and come up with the solution to world peace?

Joined:
Mar 14, 2000
Messages:
575