Arrays 将字符串数组转换为字符串数组
我的目标是转换这个Arrays 将字符串数组转换为字符串数组,arrays,perl,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
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";
}
}
我想知道你为什么要这么做,但这是
@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
请克服您的偏见。如果您将值推送到一个新数组,您只需评估
$item
是否为数组引用:
#! 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;
从转储程序输出:
$VAR1 = [
[
'red'
],
[
'blue'
],
[
'green'
],
[
'purple',
'orange'
]
];
关于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 ];
}
}
虽然在Borodin的回答中使用map会更自然。在学习了一天比我想象/想要学习的更多的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'
]
}
}
};
当你想要
map
时,为什么要使用for
?因为我的答案与他的例子相符。他显然来自python,对perl是新手。我发现,如果你使用他们已经熟悉的代码结构,人们就更容易适应一种新的语言。你能通过在线修改而不是创建一个新的数组来回答吗?这是公平的,但OP似乎已经使用Perl和for
编写了一个解决方案。这不管用吗?他们没有说任何一种方式,但我认为这是工作代码,应该像他们的Python原始代码一样简短。我现在不能运行任何代码,因为我在上下班路上。希望这会很快停止。@Borodin:我可以看出你是如何从问题的措辞中得到这种印象的。我认为,多年来从事客户支持和编辑工作使我在这方面有了优势。:)你的答案肯定是我在编写生产代码时会采用的答案。令人不安的是,有人的部落主义如此强烈。显然,海报不熟悉Perl,否则就不会问这么简单的问题。提到Python显然是为了在一种他们知道如何完成任务的语言之间建立某种可信度和平等性。@nmz787:“我之前在别处聊天时帮助OP”那么,当你知道背后还有很多东西时,你指望别人回答关于面值的问题是不合理的。当你的建议导致一个不好的问题时,把它当作个人问题也是不合理的。@nmz787:我不明白。您的解决方案的行为方式与OP的代码不同。我通常不会覆盖原始数据,但我是这样写的,因为这是OP要求的。您必须写一条注释来解释如何更改解决方案,以便修改原始数组。不管怎样,我不认为你或我的答案都值得投反对票。整个交流都是关于愤怒和伤害感情,而不是好的编程;这意味着在Python中有一种三层的方式来做他们想要做的事情,但并不是说在Perl中也没有这种方式,只是他们不知道。您正在阅读关于Perl的评论,但实际上并没有。