【问题标题】:Perl output successive string from arrayPerl从数组中输出连续的字符串
【发布时间】:2019-09-07 22:57:42
【问题描述】:

我有一个可以打印为“abcd”的数组,但是我试图将其打印为“a>ab>abc>abcd”。我无法在我拥有的 foreach 循环中找出我需要的嵌套循环。我需要在其中使用什么循环才能以这种方式打印它?

my $str = "a>b>c>d";
my @words = split />/, $str;

foreach my $i (0 .. $#words) {
print $words[$i], "\n";
}

谢谢。

【问题讨论】:

  • 为什么使用split 或数组? substr 在循环中是你的朋友。
  • 或者尝试使用累积变量 my $a ='' ;,它是 1) 由 $words[$i] 扩充(连接)然后 2) 在每个循环迭代中打印
  • 您的真正意见是什么?字符串还是数组?这里有$str@words,目前还不清楚@words 是来自您解决问题的尝试,还是实际上是您的输入。
  • 我的输入将从 CSV 文件中读取行,其中包含“a>b>d>e”格式的字符串。我正在尝试输出“a>ab>abc>abcd”的值。

标签: arrays perl foreach


【解决方案1】:

您的想法是正确的,但您不想打印位置 i 处的单词,而是想要打印位置 0 和 i(包括)之间的所有单词。此外,您的输入可以包含多个字符串,因此请遍历它们。

use warnings;

while (my $str = <>) {              # read lines from stdin or named files
  chomp($str);                      # remove any trailing line separator

  my @words = split />/, $str;      # break string into array of words
  foreach my $i (0 .. $#words) {
    print join '', @words[0 .. $i]; # build the term from the first n words
    print '>' if $i < $#words;      # print separator between terms (but not at end)
  }

  print "\n";
}

还有许多其他的编写方式,但希望这种方式可以帮助您了解正在发生的事情以及原因。祝你好运!

【讨论】:

    【解决方案2】:

    一个班轮:

    perl -e '@a=qw(a b c d); for(@a) {$s.=($h.=$_).">"} $s=substr($s,0,-1);print $s'
    

    【讨论】:

    • 谢谢,这会在末尾附加一个“>”,有什么办法可以删除它吗?
    【解决方案3】:

    我会这样做:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $str = "a>b>c>d>e>f>g";
    my @words = split />/, $str;
    
    $" = '';
    my @new_words;
    
    push @new_words, "@words[0 .. $_]" for 0 .. $#words;
    
    print join '>', @new_words;
    

    需要解释一些事情。

    Perl 将在双引号字符串中展开数组变量。所以是这样的:

    @array = ('x', 'y', 'z');
    print "@array";
    

    将打印x y z。注意元素之间有空格。插入元素之间的字符串由$" 变量控制。因此,通过将该变量设置为空字符串,我们可以删除空格,所以:

    $" = '';
    @array = ('x', 'y', 'z');
    print "@array";
    

    将打印xyz

    最复杂的一行是:

    push @new_words, "@words[0 .. $_]" for 0 .. $#words;
    

    这只是一种简洁的书写方式:

    for (0 .. $#words) {
      my $new_word = "@words[0 .. $_]";
      push @new_words, $new_word;
    }
    

    我们遍历整数从零到@words 中的最后一个索引。每次循环时,我们使用数组切片从数组中获取元素列表,将其转换为字符串(通过将其放在双引号中),然后将该字符串推送到 @new_words

    【讨论】:

      【解决方案4】:

      这就是我最终得到的结果,这是我能够理解并获得我正在寻找的输出的唯一方法。

      use strict;
      use warnings;
      
      my $str = "a>b>c>d>e>f>g";
      my @words = split />/, $str;
      
      my $j = $#words;
      my $i = 0;
      my @newtax; 
      while($i <= $#words){
              foreach my $i (0 .. $#words - $j){
                  push (@new, $words[$i]);
          }
      
          if($i < $#words){
              push(@new, ">");
          }
          $j--;
          $i++;
      }
      
      print @new;
      

      这个输出“a>ab>abc>abcd>abcde>abcdef>abcdefg”

      【讨论】:

      • 如果这对您来说最清楚,那么我建议您努力理解@mwp 的解决方案,因为最相似但更简单。 (注意,都应该是@new@newtax
      猜你喜欢
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多