Perl计数器打印列表,带逗号(最后一项除外)

时间:2019-02-20 01:33:55

标签: perl

我在尝试执行带有2个数字参数的perl脚本时遇到问题,可以说$ ARGV [0]为2,$ ARGV [1]为4。我需要打印一个显示2的列表3,4,最后一项后面没有逗号。下面是我现在的脚本:

unless ((@ARGV)==2){
        print "error: incorrect number of arguments",
        "\n",
        "usage: inlist.pl a b (where a < b)",
        "\n";
                exit VALUE;
}

if ($ARGV[0] > $ARGV[1]){
        print "error: first argument must be less than second argument",
        "\n",
        "usage: intlist.pl a b (where a < b)",
        "\n";
                exit VALUE;
}
else {

        $COUNTER=$ARGV[0];

        while($COUNTER <= $ARGV[1]){
 print $COUNTER;
                $COUNTER += 1;
        if ($COUNTERELATIONAL < $ARGV[1]){
                print ", ";
        }
        else {
                print "\n";
        }
$COUNTERSYNTAX
        }
}
exit VALUE;

我尝试使用join,但一直无济于事,一直返回2,3,4,

我觉得我必须缺少一些简单的东西

2 个答案:

答案 0 :(得分:2)

重写代码以简化代码:

<linker>
    <assembly
        fullname="Prism.Forms">
        <type
            fullname="Prism.Common.ApplicationProvider"
            preserve="all" />
        <type
            fullname="Prism.Services.PageDialogService"
            preserve="all" />
        <type
            fullname="Prism.Services.DeviceService"
            preserve="all" />
        <type
            fullname="Prism.Ioc*"
            preserve="all" />
        <type
            fullname="Prism.Modularity*"
            preserve="all" />
        <type
            fullname="Prism.Navigation*"
            preserve="all" />
        <type
            fullname="Prism.Behaviors.PageBehaviorFactory"
            preserve="all">
            <method
                name=".ctor" />
        </type>
        <type
            fullname="Prism.Services.DependencyService"
            preserve="all">
            <method
                name=".ctor" />
        </type>
    </assembly>
    <assembly
        fullname="Prism">
        <type
            fullname="Prism.Navigation*"
            preserve="all" />
        <type
            fullname="Prism.Logging.EmptyLogger"
            preserve="all">
            <method
                name=".ctor" />
        </type>
    </assembly>
</linker>

如果我是为自己编写的,我会使其简短一些:

# Prefer 'if' over 'unless' in most circumstances.
if (@ARGV != 2) {
    # Consider using 'die' instead of 'print' and 'exit'.
    print "error: incorrect number of arguments\n",
          "usage: inlist.pl a b (where a < b)\n";

    # Not sure what VALUE is, but I assume you've
    # defined it somewhere.
    exit VALUE;
}

if ($ARGV[0] > $ARGV[1]) {
    # Consider using 'die' instead of 'print' and 'exit'.
    print "error: first argument must be less than second argument\n",
          "usage: intlist.pl a b (where a < b)\n";

    exit VALUE;
}

# Removed 'else' branch as it's unnecessary.
# Use 'join' instead of a complicated loop.
print join(',', $ARGV[0] .. $ARGV[1]), "\n";

# This looks like a successful execution to me, so
# that should probably be 'exit 0'.
exit VALUE;

答案 1 :(得分:0)

我知道了:

while($COUNTER <= $ARGV[1]){
        print $COUNTER;
        $COUNTER += 1;
if ($COUNTER <= $ARGV[1]){
        print ", ";
}
else {
        print "\n";
}

我需要将if更改为$ COUNTER和<=,并且可以正确打印。谢谢您的加入建议,如果我能更有效地设计脚本,那会有用