Reputation: 141470
Minimum code
#!/usr/local/bin/perl
use strict;
use warnings;
use Math::Geometry::Planar qw(SegmentLineIntersection);
sub x_intercepts {
my $xa = @{ $_[0] };
my @xcross = ();
my @x_axis = ( [0, 2000], [1, 2000] );
foreach my $i (0 .. 1) {
my $xc = SegmentLineIntersection([ @$xa[$i, $i + 1], @x_axis ]);
push @xcross, $xc;
}
return \@xcross;
}
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/;
foreach my $i ( x_intercepts(@xs) ) {
print "$i \n";
}
which gives
Can't use string ("22.595451") as an ARRAY ref while "strict refs" in use at test5.pl line 8.
Something basic here wrong in declaring the variables.
Declaring my main::$xa = main::$xa;
in initialization is not the way to go.
Why is this error coming?
Upvotes: 0
Views: 148
Reputation: 118166
Your example boils down to:
#!/usr/bin/env perl
use strict;
use warnings;
run();
sub run {
print_x('x');
}
sub print_x {
my @x = @{ $_[0] };
print "@x\n";
}
You pass to print_x
a list of strings (in this case, a list with just one element), but in print_x
, you are treating the first argument as a reference to an array.
Note that if that were the case, you would be creating a second (shallow) copy of the array by copying all of its elements to the lexically scoped @x
. In most cases, this is not necessary or desired.
Reduce the problems you encounter to the shortest complete and runnable script that still exhibits the problem. That will help you learn.
Upvotes: 1
Reputation: 944538
You're reading the first argument you pass to the function (22.595451), which is a string, and are trying to treat it as an array (which it isn't).
Presumably you intended to pass an array reference as a single argument instead of passing each member of the array as a separate argument.
x_intercepts(\@xs)
Having converted the arrayref to a regular array, you then seem to try to treat it as a scalar:
my $xa = @{ $_[0] };
That doesn't make sense.
Based on what you do later, it looks like you want $ax
to be an arrayref, so don't dereference it:
my $xa = $_[0];
Upvotes: 5