的Perl:阵列的打印名称传递给子程序 [英] Perl : Print Name of Array Passed to sub routine

查看:99
本文介绍了的Perl:阵列的打印名称传递给子程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AM传递一个阵列和一个标量,以一个函数来检查哪里该值是阵列的一部分或不在的情况下它不是分开然后推到阵列。为了便于参考一下,有显示是同时推动它来显示阵列的名称。这里是我的code

 使用v5.10.1;
使用严格的;
使用警告;我@ ARRAY1 = QW /这是备份阵列/;
我@ ARRAY2;
我的$值=version.xml;
子CheckPush($$)
{我的$ VAL =移(@_);
我的$ = ARRAY_REF移(@_);     除非($ VAL ~~ @ $ ARRAY_REF)
     {
        打印$ val中要推送到阵列\\ n;
        推(@ $ ARRAY_REF,$ VAL);
     }
回报(@ $ ARRAY_REF);
}@ ARRAY1 = CheckPush($价值,\\ @ ARRAY1);
打印出\\ n;
的foreach $ _(@ ARRAY1){
打印$ _ \\ n;
}@ ARRAY2 = CheckPush($价值,\\ @ ARRAY2);
打印OUT2 \\ N的;
的foreach $ _(@ ARRAY2){
打印$ _ \\ n;
}

OUT输出应该像下面

  $ VAL是要推阵列(@ ARRAY1)
$ VAL是要推阵列(@ ARRAY2)


解决方案

传递数组的名称与基准一起;它并不难。

人都不知道这样的事情与源过滤器或PadWalker :: VAR_NAME,但它不是一个好主意。

Am passing one array and one scalar to a function to check where that value is part of array or not in case it is not part then push to array. For reference purpose what It has to display is while pushing it has to display the name of array . Here is my code

use v5.10.1;
use strict;
use warnings;

my @ARRAY1 = qw/This is array of backup /;
my @ARRAY2;


my $value = "version.xml" ;


sub CheckPush($$)
{

my $val = shift (@_);
my $array_ref= shift (@_);

     unless ($val ~~ @$array_ref )
     {
        print "$val is going to push to array  \n";
        push(@$array_ref,$val);
     }   
return (@$array_ref);
} 

@ARRAY1 = CheckPush($value,\@ARRAY1);
print "out \n";
foreach $_ (@ARRAY1) {
print "$_ \n";
}

@ARRAY2 = CheckPush ($value,\@ARRAY2);
print "out2 \n";
foreach $_ (@ARRAY2) {
print "$_ \n";
}

Out put should be like below

$val is going to push Array (@ARRAY1)
$val is going to push Array (@ARRAY2)

解决方案

Pass the name of the array along with the reference; it isn't that hard.

People do do things like this with source filters or PadWalker::var_name, but it isn't a good idea.

这篇关于的Perl:阵列的打印名称传递给子程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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