如何将数组中选定的元素用双引号括起来

3

这里,我试图以双引号打印数组的第二、第三和第四个元素,但我只能用单引号做到。

my @fruits = ("apples", "oranges", "guavas", "passionfruits", "grapes");
my $a = 1;
while ($a<4){
    print " '$fruits[$a]' \n";
    $a += 1;
}

但是我不能在单引号中这样做。当我将单引号更改为双引号,反之亦然,它会打印"$fruits[$a]"\n三次。 当我将所有引号都更改为双引号时,它会出现错误,我理解为什么会出现错误。 请帮帮我,我真的需要帮助。
如果我能找到一种方法来打印所有三个元素而不必使用循环就好了。谢谢!
2个回答

9
在使用由双引号包围的字符串时,若要在字符串中使用双引号,请进行转义处理。
"foo: \"$bar\"\n"

您还可以切换分隔符(请记住,"..."qq"..."的简写)。

qq{foo: "$bar"\n}

2
  1. Always use

    use strict;
    use warnings;
    

    even in the shortest Perl scripts.

    In case of typos in the code, Perl will usually issue errors if you include them. Without, Perl will happily do weird, wrong and pointless things silently.


  1. Your example will not print the entire array. Instead you will get:

     'oranges' 
     'guavas' 
     'passionfruits' 
    

    The first index of an array is 0 and therefore 'apples' is skipped because $a is initialized with 1. The loop is also exited due to reaching the value 4 before printing out 'grapes'.

    In order to print the entire array you would do:

    • if you need to use the index value $i somewhere:

      for my $i (0 .. $#fruits) {
          print " $i: '$fruits[$i]' \n";
      }
      

      ($#fruits is the last index if @fruits, equal to the the size of the array minus 1. Since this array has 5 items, the index values range from 0 to 4)

    • otherwise:

      foreach my $current_fruit (@fruits) {
          print " '$current_fruit' \n";
      }
      

      where $current_fruit is set to each item in the array @fruits in its turn.


  1. Quotes in Perl function as operators, and depending on which ones you use, they may or may not do various things with the included string.

    • In your examples, the double quotes will do interpolation on the string, substituting the value of $fruits[$a] and replacing the escape sequence \n. Therefore:

      print " '$fruits[$a]' \n";
      

      (for $a == 1) becomes:

       'oranges' 
      
    • Single quotes, in contrast, will not do interpolation.

      Only single quotes themselves (and backslashes preceding single quotes) need to be escaped with a backslash. (Other backslashes can optionally be escaped.) All other character sequences will appear as they are, so the argument to print in:

      print ' "$fruits[$a]" \n';
      

      is considered entirely a literal string and thus

       "$fruits[$a]" \n "$fruits[$a]" \n "$fruits[$a]" \n
      

      is printed out.


    To get the desired output, there are multiple ways to go about it:

    1. The simplest way - but not easiest to read for complex strings - is to use double quotes and escape the included quotes:

      print " \"$fruits[$a]\" \n";
      
    2. You can use an generic notation for "...", which is qq{...} where {} are either a pair of braces (any of (), [], {} or <>) or the same other non-whitespace, non-word character, e.g.:

      print qq{ "$fruits[$a]" \n};
      

      or

      print qq! "$fruits[$a]" \n!;
      
    3. You can concatenate the string out of parts that you quote separately:

      print ' "' . $fruits[$a] . '"' . " \n";
      

    Which is easiest to read in code will depend on the complexity of the string and the variables contained: For really long strings with complex dereferences, indeces and hash keys you might want to use option 3, whereas for short ones 2 would be the best.

    My entire edited code:

    use strict;
    use warnings;
    
    my @fruits = ("apples", "oranges", "guavas", "passionfruits", "grapes");
    
    for my $i (0 .. $#fruits) {
        print " $i: \"$fruits[$i]\" \n";
        print qq< $i: "$fruits[$i]" \n>;
        print ' ' . $i . ': "' . $fruits[$i] . '"' . " \n";
    }
    
    foreach my $current_fruit (@fruits) {
        print " \"$current_fruit\" \n";
        print qq¤ "$current_fruit" \n¤;
        print ' "' . $current_fruit . '"' . " \n";
    }
    

    You can learn more about the different quotes in Perl from the perldoc (or man on UNIX-like systems) page perlop and its section titled "Quote and Quote-like Operators".


关于“您的示例不会打印整个数组”,他们也不想这样做。 - ikegami

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接