在Perl

时间:2015-05-14 12:52:10

标签: arrays perl math type-conversion

我不理解qw在Perl中的作用,我在调试此thread中的代码时理解这一点。 其手册非常有限hereqw/string/

我试图理解以下代码中出现的问题我正在对数据进行简单的操作(乘以一(1.0))。

原始代码,是现实案例中情况的简化版本

use v5.16;
use Math::Geometry::Planar qw(SegmentLineIntersection);

sub x_intercepts {
    my ($points) = @_;

    die 'Must pass at least 2 points' unless @$points >= 2;

    my @intercepts;
    my @x_axis = ( [0, 2000], [1, 2000] );

    foreach my $i (0 .. $#$points - 1) {
      my $intersect = SegmentLineIntersection([@$points[$i,$i+1], @x_axis]);
      push @intercepts, $intersect if $intersect;
    }

    return \@intercepts;
}

my @xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 4.619254 4.526920 4.418416 4.321419 4.219890 4.123336 4.009777 3.912648 3.804183 3.705847 3.597756 3.512301 3.393413 3.301963 3.196725 3.098560 3.007482 2.899825 2.801002 2.688680 2.598862 2.496139 2.393526 2.282183 2.190449 2.084530 1.987778 1.877562 1.788788 1.678473 1.578123 1.467071 1.373372 1.283629 1.176670 1.071805 0.975422 0.877622 0.767820 0.667409 0.562480 0.469068 0.354589 0.264291 0.152522 0.063765 -0.045323 -0.136783 -0.248559 -0.343694 -0.459178 -0.551917 -0.640803 -0.755502 -0.845535 -0.955227 -1.045879 -1.155600 -1.254556 -1.365163 -1.461669 -1.571370 -1.658043 -1.772672 -1.865942 -1.981037 -2.073702 -2.176205 -2.276184 -2.367371 -2.476278 -2.567385 -2.686326 -2.777433 -2.884357 -2.980067 -3.087754 -3.183607 -3.291003 -3.386942 -3.495822 -3.586759 -3.702955 -3.793550 -3.900680 -3.999672 -4.093094 -4.200047 -4.301026 -4.399742 -4.493190 -4.602013 -4.705124 -4.812383 -4.907510 -5.022904 -5.109829 -5.214304 -5.317662 -7.703172 -10.350131 -12.921361 -15.431203 -18.188013 -20.544248 -22.822808 -25.679854 -22.999092 -20.540434 -17.964916 -15.398857 -12.990375 -10.402209 -7.888263 -5.504909 -5.217892 -5.109841 -5.014187 -4.908558 -4.811807 -4.704282 -4.605555 -4.504613 -4.406546 -4.292540 -4.204043 -4.088770 -3.995524 -3.905669 -3.796544 -3.707958 -3.596449 -3.490966 -3.382869 -3.293054 -3.185821 -3.088417 -2.971214 -2.880314 -2.772518 -2.677986 -2.569794 -2.473668 -2.365096 -2.276422 -2.179232 -2.068195 -1.973110 -1.859565 -1.771873 -1.669422 -1.569953 -1.462626 -1.364911 -1.258100 -1.159318 -1.050486 -0.959979 -0.849149 -0.749421 -0.640950 -0.547122 -0.451754 -0.344463 -0.252269 -0.134625 -0.051640 0.052970 0.154112 0.266505 0.353926 0.468739 0.561666 0.673810 0.759169 0.881697 0.973207 1.082409 1.170424 1.282163 1.378096 1.472728 1.586454 1.678473 1.785688 1.873862 1.984090 2.086021 2.196799 2.292400 2.386097 2.493190 2.601726 2.694346 2.803450 2.901878 3.011959 3.103050 3.196979 3.294507 3.397563 3.504076 3.600163 3.712539 3.809573 3.919799 4.012314 4.120694 4.216406 4.322895 4.416466 4.522871 4.623917 4.735925 4.826929 7.361253 9.647208 12.337984 14.870260 17.439730 19.921717 22.524080 25.125903/;
# -1 1.0 3 4 5 6 7 7 8 9 10/;
my @ys = qw/3699.316162 3676.939697 3659.950195 3641.476318 3605.965576 3580.152588 3555.092529 3528.118408 3509.595703 3504.416504 3508.355957 3510.452881 3510.304443 3499.548340 3499.943848 3493.196533 3499.488770 3494.984375 3494.390137 3493.938965 3492.606689 3493.434814 3488.926514 3488.890869 3484.599854 3488.077393 3484.177979 3477.801758 3478.098877 3472.040039 3477.642090 3477.802002 3472.908447 3478.532715 3469.805420 3472.759766 3464.269043 3463.950684 3465.888184 3458.441650 3459.006104 3455.686035 3455.677490 3454.548828 3454.241211 3455.250000 3449.803711 3447.423340 3457.498779 3448.445557 3453.106689 3447.701172 3444.543945 3448.558350 3450.073730 3449.884033 3444.751953 3444.056152 3444.825195 3449.671143 2593.656494 788.985779 776.407776 776.385925 767.522522 774.794250 770.596008 775.218384 770.962769 766.214294 766.735962 759.847351 760.073486 760.026489 753.721741 755.601929 753.942566 758.356506 747.932617 746.332214 746.464844 747.055115 750.173706 737.463379 739.810486 742.011475 744.332581 743.022461 737.302490 737.396606 734.325256 737.305359 740.642395 734.709717 735.754089 737.961182 740.697510 727.310913 730.918640 728.325012 721.845459 728.389893 727.765625 729.961243 725.608459 723.581909 730.736084 720.707764 720.398193 691.499390 657.534546 628.854431 615.219727 572.711365 561.505127 539.865173 517.139709 545.076416 580.880005 602.851135 628.006104 657.119263 683.746033 692.044373 716.640320 715.451294 715.415405 718.968018 723.698669 729.758606 728.564514 734.523376 731.454468 736.899780 731.257263 729.390686 732.837463 730.479431 733.497803 735.362732 742.581543 742.998108 735.918579 738.190002 738.108337 738.154297 740.425537 739.496033 743.105835 737.412537 740.537354 747.498108 747.424194 748.992920 752.244263 755.603455 756.611755 761.916504 762.920715 752.261658 758.293823 755.664062 753.728882 756.746338 754.825684 755.080444 761.192383 761.955505 763.209351 760.402771 764.342224 775.400940 767.148621 775.184998 777.084595 778.357117 776.217163 1086.248291 3444.476807 3438.105957 3440.625000 3438.325195 3438.430420 3449.251709 3453.216309 3452.126465 3458.547119 3451.694336 3456.417725 3457.336426 3457.777832 3455.553955 3457.256348 3458.823486 3459.088623 3459.492187 3463.538818 3466.455078 3456.521240 3459.809082 3457.505127 3462.721191 3466.518066 3467.562744 3469.211182 3469.120361 3464.043945 3466.291992 3472.698486 3476.146729 3471.635254 3472.539551 3475.163574 3473.687744 3479.102051 3488.351807 3482.367432 3481.961914 3484.844238 3481.511719 3482.469238 3488.947021 3488.882080 3491.247314 3499.116699 3511.889893 3539.602783 3565.981445 3598.203613 3628.028076 3657.928955 3685.231689/;
# my @ys = 1.0 * @ys;

my @input_list ;
foreach my $i ( 0..$#ys ) {
   push @input_list,  [ $xs[$i], $ys[$i] ]  ;
}

my $intercept_list = x_intercepts(\@input_list)  ;
say join ",", @$_ for @$intercept_list ;

我只是通过注释哪个

来激活此行# my @ys = 1.0 * @ys;
  

必须在test2.pl第7行传递至少2分。

这表明1.0 * qw_stuff正在做的事情超出了我的预期。

我跑

my @ys = 1 * scalar(@ys); my @ys = qw/@ys/;

但同样的错误仍然存​​在。

如何在Perl中对整数和qw之间的数据进行简单操作?

1 个答案:

答案 0 :(得分:7)

引用词语:

my @xs = qw/22.595451 20.089094 17.380813 15.091260 12.477935 10.054821 7.270003 4.804673 4.728526 4.619254 4.526920 4.418416 4.321419 4.219890 4.123336 4.009777 3.912648 3.804183 3.705847 3.597756 3.512301 3.393413 3.301963 3.196725 3.098560 3.007482 2.899825 2.801002 2.688680 2.598862 2.496139 2.393526 2.282183 2.190449 2.084530 1.987778 1.877562 1.788788 1.678473 1.578123 1.467071 1.373372 1.283629 1.176670 1.071805 0.975422 0.877622 0.767820 0.667409 0.562480 0.469068 0.354589 0.264291 0.152522 0.063765 -0.045323 -0.136783 -0.248559 -0.343694 -0.459178 -0.551917 -0.640803 -0.755502 -0.845535 -0.955227 -1.045879 -1.155600 -1.254556 -1.365163 -1.461669 -1.571370 -1.658043 -1.772672 -1.865942 -1.981037 -2.073702 -2.176205 -2.276184 -2.367371 -2.476278 -2.567385 -2.686326 -2.777433 -2.884357 -2.980067 -3.087754 -3.183607 -3.291003 -3.386942 -3.495822 -3.586759 -3.702955 -3.793550 -3.900680 -3.999672 -4.093094 -4.200047 -4.301026 -4.399742 -4.493190 -4.602013 -4.705124 -4.812383 -4.907510 -5.022904 -5.109829 -5.214304 -5.317662 -7.703172 -10.350131 -12.921361 -15.431203 -18.188013 -20.544248 -22.822808 -25.679854 -22.999092 -20.540434 -17.964916 -15.398857 -12.990375 -10.402209 -7.888263 -5.504909 -5.217892 -5.109841 -5.014187 -4.908558 -4.811807 -4.704282 -4.605555 -4.504613 -4.406546 -4.292540 -4.204043 -4.088770 -3.995524 -3.905669 -3.796544 -3.707958 -3.596449 -3.490966 -3.382869 -3.293054 -3.185821 -3.088417 -2.971214 -2.880314 -2.772518 -2.677986 -2.569794 -2.473668 -2.365096 -2.276422 -2.179232 -2.068195 -1.973110 -1.859565 -1.771873 -1.669422 -1.569953 -1.462626 -1.364911 -1.258100 -1.159318 -1.050486 -0.959979 -0.849149 -0.749421 -0.640950 -0.547122 -0.451754 -0.344463 -0.252269 -0.134625 -0.051640 0.052970 0.154112 0.266505 0.353926 0.468739 0.561666 0.673810 0.759169 0.881697 0.973207 1.082409 1.170424 1.282163 1.378096 1.472728 1.586454 1.678473 1.785688 1.873862 1.984090 2.086021 2.196799 2.292400 2.386097 2.493190 2.601726 2.694346 2.803450 2.901878 3.011959 3.103050 3.196979 3.294507 3.397563 3.504076 3.600163 3.712539 3.809573 3.919799 4.012314 4.120694 4.216406 4.322895 4.416466 4.522871 4.623917 4.735925 4.826929 7.361253 9.647208 12.337984 14.870260 17.439730 19.921717 22.524080 25.125903/;

相同
my @xs = ('22.595451', '20.089094', '17.380813', '15.091260', 'etc...');

但是,如您所见,可以保存输入的

所以既然你有很多字符串,你可以将它们用作数字,perl会自动为你做转换(一般推荐),或者如果他们的字符串冒犯了你,你可以明确地做一个过早的手册转换。

如果要转换数组或列表,请考虑mapforeach

@ys = map { 1 * $_ } @ys;

foreach my $element (@ys) {
    $element *= 1;    # or   += 0  both work.
}

......但是说真的,那实际上是在买什么?如果您允许Perl在闲暇时进行转换,则值将变为双值,如果您将它们用作字符串,则无需重新转换。使用手动转换时,您最终可能会遇到一个仅需要数值的值,这可能需要重新进行字符串化,因此可能会在舍入错误apple上进行第二次咬合。

真正想要进行手动转换的唯一时间是您需要转换为BigIntBigFloat或类似对象(即您关注的是任何可能的精度损失自动转换为浮点数都可以);或者,如果您通过JSON或YAML将输出值准备到关注“数字的文本表示”(49)和“引用的数字字符串”("49")之间差异的系统虽然被警告说这算作重新串联。