如何使用数组引用元素对 [英] How to use an array reference to pairs of elements

查看:165
本文介绍了如何使用数组引用元素对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑这个回答它使用点的单数组引用,其中一个点到两个参考-element数组。
这个问题我原来的code(函数提取渡)使用两个单独的数组 $ X $ Y 这里我称之为是这样的:

I am considering this answer which uses a single array reference of points, where a point is a reference to a two-element array. My original code of the question (function extract-crossing) uses two separate arrays $x and $y here which I call like this:

my @x = @{ $_[0] }; my @y = @{ $_[1] };
...
return extract_crossing(\@x, \@y);

以下,根据回答需要(X,Y) 并返回一个数据类型,这里X轴截距点:

The new code below based on the answer takes (x, y) and returns a single datatype, here x intercept points:

use strict; use warnings;    
use Math::Geometry::Planar qw(SegmentLineIntersection);
use Test::Exception;

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

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

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

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

   return \@intercepts;
}

我试着像这样调用:

which I try to call like this:

my @x = @{ $_[0] }; my @y = @{ $_[1] };  
...
my $masi = x_intercepts(\@x);
return $masi; 

不过,code没有意义。
我感到困惑的双阵列传递给 x_intercepts()功能。

如何才能让这个例子code更清晰的原始设置?

How can you make the example code clearer to the original setting?

推荐答案

如果我在这里认识问题,@ ThisSuitIsBlackNot ++写一个函数( x_intercepts 这是可以的螺纹:函数来提取对轴交叉线交点的)的预计,其参数是数组列表的引用引用。在 x_intercepts 子程序反过来使用的函数从数学几何:: ::平面这需要一个线段的点作为系列数组引用/匿名数组包含 X,Y 值为每个点的传递。

If I am understanding the problem here, @ThisSuitIsBlackNot++ has written a function (x_intercepts which is available in the thread: Function to extract intersections of crossing lines on axes) that expects its argument to be a reference to a list of array references. The x_intercepts subroutine in turn uses a function from Math::Geometry::Planar which expects the points of a line segment to be passed as series of array references/anonymous arrays that contain the x,y values for each point.

再次 - 它不是的完全的明确的 - 但似乎的您的数据的是在两个不同的数组:包含所有x值和一个与相应的y值之一。这样的话?如果这是不正确的,请发表评论,我会删除这个答案。

Again - it is not entirely clear - but it seems your data is in two different arrays: one containing all the x values and one with the corresponding y values. Is this the case? If this is not correct please leave a comment and I will remove this answer.

如果这是你的问题的根源,那么你可以在munge,或者你把它传递给 x_intercepts 前将您的数据或 - 作为@ThisSuitIsBlackNot建议 - 你可以重写的功能。这是改写(munging)现有的数据转化的例子是 @input_list 传递给 x_intercepts

If that is the source of your problem then you can "munge" or transform your data before you pass it to x_intercepts or - as @ThisSuitIsBlackNot suggests - you can rewrite the function. Here is an example of munging your existing data into an "@input_list" to pass to x_intercepts.

my @xs = qw/-1 1 3/;
my @ys = qw/-1 1 -1 /;

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 ;

添加上述行到你的脚本生成:

Adding the lines above to your script produces:

输出

0,0
2,0

您必须非常小心,在做这样的事情,并使用测试,以确保您传递正确转换后的数据按照预期的方式是一个好主意。

You have to be very careful doing this kind of thing and using tests to make sure you are passing the correctly transformed data in an expected way is a good idea.

我觉得一个更一般的困难是,直到你熟悉Perl有时棘手很容易看到一个子程序期待什么样的价值观,在那里他们最终它们传递后,以及如何访问它们。

I think a more general difficulty is that until you are familiar with perl it is sometimes tricky to easily see what sorts of values a subroutine is expecting, where they end up after they are passed in, and how to access them.

Perl数据结构扎实抓好可以与帮助 - 例如我的认为的你拨打什么是双阵列或双元在这里是一个的数组​​的数组的。有办法使它更容易看到默认的参数传递给一个子程序(在 @_ )会(注意@ThisSuitIsBlackNot是如何他们传递给一个很好的命名数组引用:($分))。 的perldoc 的丰富审读perbsub 可以帮助东西似乎更明显。

A solid grasp of perl data structures can help with that - for example I think what you are calling a "double array" or "double element" here is an "array of arrays". There are ways to make it easier to see where default arguments passed to a subroutine (in @_) are going (notice how @ThisSuitIsBlackNot has passed them to a nicely named array reference: "($points)"). Copious re-reading of perldocperbsub can help things seem more obvious.

参考的关键是因为理解的perl子程序传递一个数组或哈希到subrouting你需要通过引用这样做。如果参数传递 x_intercepts 是匿名数组然后当它被分配到($分)的两个列表名单, @ $点 - > [0] @ $点 - > [1] 将数组包含这些名单。

References are key to understanding perl subroutines since to pass an array or hash to a subrouting you need to do so by references. If the argument passed x_intercepts is a list of two lists of anonymous arrays then when it is assigned to ($points), @$points->[0] @$points->[1] will be the arrays contain those lists.

我希望这有助于,而不是太简单(或不正确)。如果@ThisSuitIsBlackNot发现的时候提供一个答案,你应该接受它。已提供了一些非常有用的例子

I hope this helps and is not too basic (or incorrect). If @ThisSuitIsBlackNot finds the time to provide an answer you should accept it: some very useful examples have been provided.

这篇关于如何使用数组引用元素对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆