Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^5: enumerating values for slopes

by Laurent_R (Canon)
on Sep 23, 2014 at 17:36 UTC ( [id://1101689]=note: print w/replies, xml ) Need Help??


in reply to Re^4: enumerating values for slopes
in thread enumerating values for slopes

The error occurs in the module, but the coding error is actually located in your calling program. When you do this:
my @vector = (4.3125, 0.4375); push @AoA, @vector; @vector = (4.375, 0.375); push @AoA, @vector;
you are not creating an array of arrays (AoA), but a simple array whose 4 elements are:
(4.3125, 0.4375, 4.375, 0.375)
You need to push on @AoA a reference to @vector:
my @vector = (4.3125, 0.4375); push @AoA, \@vector; @vector = (4.375, 0.375); push @AoA, \@vector;
or build
directly an array reference:
push @AoA, [4.3125, 0.4375]; push @AoA, [4.375, 0.375];
I guess this should solve your issue.

As I said earlier, you should avoid using the $a and $b variables, they are special variables used among other things for sorting, they behave differently from other variables, and using as you do them might lead to difficult-to-track bugs. You may use $c or $d, if you like, this will not be a problem, but I would personally put a more meaningful name helping comprehension of what they represent.

Update: As pointed below by farang the first method above for building an AoA is actually buggy. Use the second one. Thanks to farang for picking that.

Replies are listed 'Best First'.
Re^6: enumerating values for slopes
by farang (Chaplain) on Sep 24, 2014 at 02:07 UTC

    You need to push on @AoA a reference to @vector:
    my @vector = (4.3125, 0.4375); push @AoA, \@vector; @vector = (4.375, 0.375); push @AoA, \@vector;
    That way is buggy, though, as the redefinition of @vector will clobber the first pushed array element. This is explained under COMMON MISTAKES in perldsc, "taking a reference to the same memory location repeatedly".

    So both lines above should instead use a square bracket reference.

    push @AoA, [ @vector ];

      You are right, it's a mistake to push references to the same array without declaring it anew. I have been bitten by that a couple of times, I should know better. That's why I am actually almost always using the second method using the [] anonymous array reference constructor.
Re^6: enumerating values for slopes
by Aldebaran (Curate) on Sep 24, 2014 at 06:30 UTC
    $ perl sill2.pl elt 0 0 is 0.4375 elt 0 1 is 4.3125 elt 0 2 is bath-left elt 1 0 is 0.375 elt 1 1 is 4.375 elt 1 2 is bath-middle elt 2 0 is 0.375 elt 2 1 is 4.4375 elt 2 2 is bath-right bath-left has slope 5.82263230478133 bath-middle has slope 4.91710033552881 bath-right has slope 4.84767847916148 $ cat sill2.pl #!/usr/bin/perl -w use strict; use 5.010; use Math::Trig; BEGIN { push @INC, ".."; } use utils1; my @AoA; my $aoa_ref = \@AoA; my $vector_ref = [0.4375, 4.3125, "bath-left"]; push @AoA, $vector_ref; $vector_ref = [0.375, 4.375, "bath-middle"]; push @AoA, $vector_ref; $vector_ref = [0.375,4.4375, "bath-right"]; push @AoA, $vector_ref; print_aoa($aoa_ref); for my $i ( 0 .. $#AoA ) { my $ratio = $AoA[$i][0]/$AoA[$i][1]; my $rad = asin($ratio); my $degrees = rad2deg($rad); my $name = $AoA[$i][2]; say "$name has slope $degrees"; } __END__ $

    This might be the most robust version of this material.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1101689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 07:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found