检测混合数组中的元素类型 [英] Detecting element types in a mixed array

查看:93
本文介绍了检测混合数组中的元素类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些带有子例程的代码,该子例程包括数组引用作为参数之一。此传入数组中的元素可以是小数组或字符串。

Im working with some code that has a subroutine which includes an array reference as one of the parameters. The elements in this incoming array can be either small arrays or strings.

我想确定每个元素是什么类型的,以便执行特定的操作(即,如果元素是数组,则通过索引深入研究它,如果元素是一个字符串,使用字符串)

I want to determine what type each element is in order to do something specific (i.e. if the element is an array, drill into it further via indexing, if the element is a string, use the string)

我尝试使用 ref 函数查询每个数组元素。它似乎适用于ARRAY元素,但是如果该元素是字符串,我期望 ref 返回SCALAR。但是 ref()似乎什么也不返回。我究竟做错了什么?我认为 ref()会返回一些内容。

I have tried using the ref function to interrogate each array element. It seems to work for elements that are ARRAYs, but if the element is a string, I was expecting the ref to return SCALAR. However ref() seems to return nothing. What am I doing wrong? I would think ref() would return something.

这里是一些示例代码:

my @array = ("string1", 
             ["ele1_arraystr1", "ele1_arraystr2"], 
             "string2", 
             ["ele4_arraystr1", "ele4_arraystr2"], 
             "etc");
my $tmp;
&foobar( 30, 20, \@array);

sub foobar {
    my($var1, $var2, $array_ref) = @_;
    foreach $element (@$array_ref) {
        my $tmp = ref($element);
        print "Array element type: $tmp\n";
        if ($tmp eq 'ARRAY') {
            print "  ARRAY: $element->[1]\n";

        } elsif ($tmp eq 'SCALAR') {
            print "  SCALAR: $element\n";
        } else {
            print "  Unexpected type: $tmp\n";
       }
    }
 }

输出看起来像这样:

ARRAY element test:
Array element type: 
  Unexpected type: 
Array element type: ARRAY
  ARRAY: ele1_arraystr2
Array element type: 
  Unexpected type: 
Array element type: ARRAY
  ARRAY: ele4_arraystr2
Array element type: 
  Unexpected type: 


推荐答案

ref 如果参数为',则返回空字符串。参考。文档说

The ref returns an empty string if its argument isn't a reference. Docs say


如果EXPR是引用,则返回非空字符串,否则返回空字符串。返回的值取决于引用所引用的事物的类型。

Returns a non-empty string if EXPR is a reference, the empty string otherwise. The value returned depends on the type of thing the reference is a reference to.

后面的列表包括 SCALAR reference 可以使用的类型。

The list that follows, which includes SCALAR, are the types that the reference can be to.

因此,当它有字符串时,返回一个空字符串,其结果为false。如果您实际上知道它是 ARRAY 或字符串,则可以这样做

So when it has a string it returns an empty string, which evaluates to false. If you were to know for fact that it's ARRAY or string, you could do

if (ref($element) eq 'ARRAY') {
    # process the arrayref
}
else { 
    # process the string
}

像您一样,更好地检查字符串(false),以便能够检测其他类型

Better check for the string (false) specifically, as you do, so to be able to detect any other types

my $ref_type = ref($element);
if ($ref_type eq 'ARRAY') {
    # process arrayref
}
elsif (not $ref_type) {  
    # process string
}
else { print "Got type $ref_type\n" }

这篇关于检测混合数组中的元素类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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