首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串数组转换为字符串数组

将字符串数组转换为字符串数组
EN

Stack Overflow用户
提问于 2017-10-28 01:41:44
回答 4查看 369关注 0票数 1

我的目标是将这个

代码语言:javascript
复制
my @array=("red", "blue", "green", ["purple", "orange"]);

进入到这个

代码语言:javascript
复制
my @array = ( ["red"], ["blue"], ["green"], ["purple", "orange"]);

当前测试代码:

代码语言:javascript
复制
my @array = ("red", "blue", "green", ["purple", "orange"] );

foreach $item ( @array ) {
   #if this was Python, it would be as simple as:
   #  if is not instance(item, array):
   #     # item wasn't a list
   #     item = [item]

   if(ref($item) ne 'ARRAY'){
      #It's an array reference...
      #you can read it with $item->[1]
      #or dereference it uisng @newarray = @{$item}
      #print "We've got an array!!\n";
      print $item, "\n";
      # keep a copy of our string
      $temp = $item;
      # re-use the variable but convert to an empty list
      @item = ();
      # add the temp-copy as first list item
      @item[0] = $temp;
      # print each list item (should be just one item)
      print "$_\n" for $item;
   }else{
      #not an array in any way...
       print "ALREADY an array!!\n";

   }
}

#  EXPECTED my @array=(["red"], ["blue"], ["green"], ["purple", "orange"]);
print @array , "\n";

foreach $item (@array){
    if(ref($item) ne 'ARRAY'){
    #
    #say for $item;
    print "didn't convert properly to array\n";
   }
}
EN

回答 4

Stack Overflow用户

发布于 2017-10-28 07:35:06

关于python的注释相当直接地映射到perl。

代码语言:javascript
复制
my @array = ("red", "blue", "green", ["purple", "orange"] );

foreach $item ( @array ) {
   #if this was Python, it would be as simple as:
   #  if is not instance(item, array):
   #     # item wasn't a list
   #     item = [item]
   if (ref $item ne 'ARRAY') {
       $item = [ $item ];
   }
}

不过,使用Borodin的答案中的map会更自然。

票数 2
EN

Stack Overflow用户

发布于 2017-10-28 01:52:32

我想知道你为什么要这么做,但这是

代码语言:javascript
复制
@array = map { ref ? $_ : [ $_ ] } @array

请不要调用数组@array;这就是@的作用。

你的评论太荒谬了

代码语言:javascript
复制
#if this was Python, it would be as simple as:
#  if is not instance(item, array):
#     # item wasn't a list
#     item = [item]

如果您熟悉Perl,那么就不需要问这个问题了。您必须知道,没有从Python到Perl的一对一转换。Python的表现力远不如Perl或C,但我无法想象您会要求简单地转换为C。

请克服你的偏执。

票数 1
EN

Stack Overflow用户

发布于 2017-10-28 01:56:28

如果将值推入新数组,只需计算$item是否为arrayref即可:

代码语言:javascript
复制
#! perl

use strict;
use warnings;

use Data::Dumper;

my @array=("red", "blue", "green", ["purple", "orange"]);

my @new_array;
foreach my $item (@array) {

    if ( ref($item) eq  'ARRAY' ) {
        push @new_array, $item;
    }
    else {
        push @new_array, [$item];
    }
}

print Dumper \@new_array;

Dumper的输出:

代码语言:javascript
复制
$VAR1 = [
          [
            'red'
          ],
          [
            'blue'
          ],
          [
            'green'
          ],
          [
            'purple',
            'orange'
          ]
        ];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46980709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档