Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Javascript to Perl = fail

by Anonymous Monk
on May 04, 2012 at 21:41 UTC ( #968991=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i have attempted to convert javascript to perl with no luck, my perl knowlegde isnt very good to say the least! I'll show you the javascript code, and then the perl code.
var text = 'encoded text'; var prime = 0; var comp = 0; var text; var line = ""; for (i = 0; i < text.length; i++) { var c = text.charAt(i); var comparison =parseInt(c); if (comparison) { var n = Number(c); if ((n != 1) && (n != 0)) { if (isprime(n)) { prime += n; } if (!isprime(n)) { comp += n; } } } else if ((line.length < 25) && (comparison != 1)) { c = c.charCodeAt(0); c++; c = c.toString(); temp = c; c = String.fromCharCode(temp); line += c; } } var sum = 1 * prime * comp; line += sum; WScript.Echo(line); function isprime(n) { var primer = true; var ValidChars = "2357"; var str = n.toString(); if (ValidChars.indexOf(str.charCodeAt(0)) == -1) { primer = false; } return primer; }
now perl
sub isprime($) { my $num = $_[1]; $primer = 1; $ValidChars = "2357"; $str = $num; if (index($ValidChars, ord(substr $str, 0, 1)) == 1) { $primer = 0; } return $primer; } my ($text, $prime, $comp, $line); $text = "the encoded text"; for ($i = 0; $i < $text.length; $i++) { $c = substr($text, $i, 1); ($comparison) = $c =~ s/[^0-9]//g; if ($comparison) { $n = $c; if (($n != 1) && ($n != 0)) { if (isprime($n)) { $prime += $n; } if (!isprime($n)) { $comp += $n; } } $a=$line.length; } elsif (($a < 25) && ($comparison != 1)) { $c = ord(0); $c++; $temp = $c; $c = chr($temp); $line += $c; } } $sum = 1 * $prime * $comp; $line += $sum; print $line; <>;
thanks for any help.

Comment on Javascript to Perl = fail
Select or Download Code
Re: Javascript to Perl = fail
by choroba (Abbot) on May 04, 2012 at 21:51 UTC
    Just the first thing I noticed: The first argument of a subroutine is $_[0], not $_[1].
      thanks i changed it now.. more problems to solve now.. :/
Re: Javascript to Perl = fail
by JavaFan (Canon) on May 04, 2012 at 22:36 UTC
    sub isprime($) { my $num = $_[1]; $primer = 1; $ValidChars = "2357"; $str = $num; if (index($ValidChars, ord(substr $str, 0, 1)) == 1) { $primer = 0; } return $primer; }
    That returns 1 unless the first character of the first argument (assuming you fix the already noticed error regarding $_[1]) equals 3. I think you are calling isprime only with single digit numbers, and want to return if the digit is a prime number. I'd write that as:
    sub isprime {$_[0] =~ /^[2357]$/}
    although I would fix the name. Or just inline the test -- that's a lot clearer than any name I can think of.
    if (($n != 1) && ($n != 0)) { if (isprime($n)) { $prime += $n; } if (!isprime($n)) { $comp += $n; } }
    Too many ifs, and too many calls to isprime. I'd write that as:
    if ($n =~ /^[2357]$/) { $prime += $n; } elsif ($n !~ /^[01]$/) { $comp += $n; }
    Or, when I'm in the mood for something funky, as a ternary returning an lvalue.
    $text.length
    That should be spelled length($text).
Re: Javascript to Perl = fail
by eyepopslikeamosquito (Canon) on May 05, 2012 at 00:07 UTC

    Let's start by examining just one snippet of your Javascript code:

    c = c.charCodeAt(0); c++; c = c.toString(); temp = c; c = String.fromCharCode(temp); line += c;
    I hadn't written any Javascript until just now, but it seems to me (and correct me if I'm wrong) that the above code could be equivalently expressed in one line as:
    line += String.fromCharCode(c.charCodeAt(0)+1);
    which is equivalent to the Perl code:
    $line .= chr(ord($c)+1);
    assuming that $c contains just one character, as I believe it does from reading your code. Note that Perl uses a dot rather than a plus sign to concatenate strings. The Perl ord function returns the ord value (0-255) of a character while the Perl chr function does the reverse.

    Given your lack of Perl experience, rather than attempting to convert the whole script all at once, I suggest you start by writing a number of very short Perl test programs, liberally sprinkled with print statements, so you can see what is going on. That should slowly build your confidence without overwhelming you. For example, you could start by running this little test program:

    use strict; use warnings; my $line = "hello"; my $c = "A"; print "1. line='$line' c='$c'\n"; $line .= chr(ord($c)+1); print "2. line='$line' c='$c'\n";
    which should print:
    1. line='hello' c='A' 2. line='helloB' c='A'
    Always start your Perl scripts with "use strict" and "use warnings" as shown above. Doing that will catch a lot of errors for you.

    As for learning Perl, take a look at learn.perl.org and Perl Tutorial Hub. Also, be sure to refer to the Perl online documentation. Good luck!

Re: Javascript to Perl = fail
by Not_a_Number (Parson) on May 05, 2012 at 09:22 UTC

    I couldn't follow the logic of your JS code, so I ran it as is on my machine. Here's the output:

    D:\progs\javascript>j test.js fodpefe!ufyu0

    It seems that the aim is to increase the numeric (ascii+) value of each character in the string by 1, then add a zero on the end. Testing with a number of other random strings appears to confirm this analysis.

    So here's how I'd translate the code into Perl:

    my $str = 'encoded text'; say join '', ( map { chr ord( $_ ) + 1 } split //, $str ), 0;

    Update: Further testing reveals that your code also, for some reason, strips all digits except zero from the string. Easy peasy:

    say join '', ( map { chr ord( $_ ) + 1 } grep /[^1-9]/, split //, $str ), 0;
      hi, this would be the encoded text http://pastebin.com/mHaZ3NBf

        Please explain why you pointed me to that site.

        Is it some encoded text that your JS script is supposed to decode? If so, you should have entitled your OP Javascript = fail, since all I get when I run the text through your code is garbage:

        fqAv@nbj@Aqsetfdxx@Am%@gh80676

        Or am I missing something? Maybe my confusion has something to do with the fact that the OP was anonymous, but the latest question was posed by sweepy838?

        Please elucidate.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://968991]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-09-16 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (158 votes), past polls