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

时间:2017-10-27 17:41:44

标签: arrays perl

我的目标是转换此

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

进入这个

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

当前测试代码:

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";
   }
}

4 个答案:

答案 0 :(得分:2)

关于python的评论非常直接映射到perl。

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 ];
   }
}

虽然像鲍罗丁那样使用地图的答案会更自然。

答案 1 :(得分:1)

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

@array = map { ref ? $_ : [ $_ ] } @array

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


你的评论很荒谬

#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语言。

请克服你的偏见。

答案 2 :(得分:0)

如果将值推送到新数组,则除了评估$item是否为arrayref之外,您不需要做更多的事情:

#! 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输出:

$VAR1 = [
          [
            'red'
          ],
          [
            'blue'
          ],
          [
            'green'
          ],
          [
            'purple',
            'orange'
          ]
        ];

答案 3 :(得分:0)

经过漫长的一天学习Perl比我想象/想学习更多......这就是我认为可行的解决方案:

#! perl

use strict;
use warnings;

use Data::Dumper;


my %the_dict = (duts => 
                    {dut_a => {UDF => 'hamnet'},
                     dut_b => {UDF => [ '1', '2', '3', ]},
                     dut_c => {UDF => [ 'Z' ], }});

print Dumper \%the_dict;

foreach my $dut (keys %{$the_dict{duts}}) {
    # convert the dut's UDF value to an array if it wasn't already
    if ( 'ARRAY' ne ref $the_dict{duts}->{$dut}{UDF} ) { 
        $the_dict{duts}->{$dut}{UDF} = [ $the_dict{duts}->{$dut}{UDF} ]; 
    }
    # now append a new value to the array
    push(@{$the_dict{duts}{$dut}{UDF}}, 'works');
}

print Dumper \%the_dict;

运行时我们会看到这些打印输出:

$VAR1 = {
          'duts' => {
                      'dut_a' => {
                                   'UDF' => 'hamnet'
                                 },
                      'dut_c' => {
                                   'UDF' => [
                                              'Z'
                                            ]
                                 },
                      'dut_b' => {
                                   'UDF' => [
                                              '1',
                                              '2',
                                              '3'
                                            ]
                                 }
                    }
        };
$VAR1 = {
          'duts' => {
                      'dut_a' => {
                                   'UDF' => [
                                              'hamnet',
                                              'works'
                                            ]
                                 },
                      'dut_c' => {
                                   'UDF' => [
                                              'Z',
                                              'works'
                                            ]
                                 },
                      'dut_b' => {
                                   'UDF' => [
                                              '1',
                                              '2',
                                              '3',
                                              'works'
                                            ]
                                 }
                    }
        };