English 中文(简体)
Math =>PHP to Perl [los]
原标题:Math => PHP to Perl [closed]
  • 时间:2011-09-11 21:18:10
  •  标签:
  • php
  • perl
  • math

我是PHP dev,我很想知道,有人是否能够帮助我把这一法典变成一个Perl文字。

I wrote this PHP code today:

<?php
for ($i = 1; $i > $i++; $i++) {
    $cut = strlen($i)/2;

    $a = substr($i,0,$cut);
    $b = strrev($a);

    $som = $a + $b;

    if((strrev($a) == $b) && (strrev($som) == $som) && ($a > 10000)) {
        echo $a . " + " . $b . " = " . $som . "
";
    }       
}
?>

它做的是:

如果你铭记这一公式:美元+美元b = 美元,基本不变。

  • Cuts $i in half, reverses the 2st part and stores into $b, and stores the first part unreversed into $a.
  • Adds $a and $b into $c, and checks if $c equals $c in reverse.
  • 产出:

    1248 + 8421 = 9669

如果任何人能够帮助我:

先进。

最佳回答

考虑到Zaids的建议和很少的想法,一些小的变化:

use strict;

for(my $i = 1;$i > 0;$i++)
{
    my $input = $i;
    my $firstHalf = substr($input,0,length($i)/2,  );
    my $sum = $firstHalf + $input;
    if(($firstHalf == reverse($input)) && (reverse($sum) == $sum) && ($firstHalf > 1000)) {
    print $firstHalf . " + " . $input . " = " . $sum . "
";
    }
}

在使用<代码>时使用警告 我收到增加的警告,必须对此进行调查。

问题回答
  • Use length to get the length of the input
  • substr to cut the input
  • reverse to check if it s a palindrome
  • Define the functionality in a subroutine
  • Avoid use of $a and $b as variable names
  • It s a good idea to ensure that the input is numeric
  • use strict; use warnings; when coding

use strict;
use warnings;
use Scalar::Util  looks_like_number ;

sub is_modified_palindrome {

    my $input = shift;

    # Test input numericness
    warn("Input  $input  non-numeric"), return unless looks_like_number $input;

    my $cutIndex  = length($input)/2;                  # Don t worry about int-ing it
    my $firstHalf = substr $input, 0, $cutIndex,   ;   # $input contains second half

    my $result    = $firstHalf + $input;

    return $result = reverse $result;                  # Returns true or false
}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签