perl - How to use an array reference to pairs of elements -
i considering answer uses single array reference of points, point reference two-element array. original code of question (function extract-crossing) uses 2 separate arrays $x , $y here call this:
my @x = @{ $_[0] }; @y = @{ $_[1] }; ... return extract_crossing(\@x, \@y); the new code below based on answer takes (x, y) , returns single datatype, here x intercept points:
use strict; use warnings; use math::geometry::planar qw(segmentlineintersection); use test::exception; sub x_intercepts { ($points) = @_; die 'must pass @ least 2 points' unless @$points >= 2; @intercepts; @x_axis = ( [0, 0], [1, 0] ); foreach $i (0 .. $#$points - 1) { $intersect = segmentlineintersection([@$points[$i,$i+1],@x_axis]); push @intercepts, $intersect if $intersect; } return \@intercepts; } which try call this:
my @x = @{ $_[0] }; @y = @{ $_[1] }; ... $masi = x_intercepts(\@x); return $masi; however, code not make sense. confused passing "double array" x_intercepts() function.
how can make example code clearer original setting?
if understanding problem here, @thissuitisblacknot++ has written function (x_intercepts available in thread: function extract intersections of crossing lines on axes) expects argument reference list of array references. x_intercepts subroutine in turn uses function math::geometry::planar expects points of line segment passed series of array references/anonymous arrays contain x,y values each point.
again - not entirely clear - seems your data in 2 different arrays: 1 containing x values , 1 corresponding y values. case? if not correct please leave comment , remove answer.
if source of problem can "munge" or transform data before pass x_intercepts or - @thissuitisblacknot suggests - can rewrite function. here example of munging existing data "@input_list" pass x_intercepts.
my @xs = qw/-1 1 3/; @ys = qw/-1 1 -1 /; @input_list ; foreach $i ( 0..$#ys ) { push @input_list, [ $xs[$i], $ys[$i] ] ; } $intercept_list = x_intercepts(\@input_list) ; join ",", @$_ @$intercept_list ; adding lines above script produces:
output:
0,0 2,0 you have careful doing kind of thing , using tests make sure passing correctly transformed data in expected way idea.
i think more general difficulty until familiar perl tricky see sorts of values subroutine expecting, end after passed in, , how access them.
a solid grasp of perl data structures can - example think calling "double array" or "double element" here "array of arrays". there ways make easier see default arguments passed subroutine (in @_) going (notice how @thissuitisblacknot has passed them nicely named array reference: "($points)"). copious re-reading of perldocperbsub can things seem more obvious.
references key understanding perl subroutines since pass array or hash subrouting need references. if argument passed x_intercepts list of 2 lists of anonymous arrays when assigned ($points), @$points->[0] @$points->[1] arrays contain lists.
i hope helps , not basic (or incorrect). if @thissuitisblacknot finds time provide answer should accept it: useful examples have been provided.
Comments
Post a Comment