Alternate for making such a thing work in perl : `for(10…0)`

后端 未结 3 1250
青春惊慌失措
青春惊慌失措 2021-01-18 02:20

This doesn\'t work in perl : for(10...0) It essentially doesn\'t loop even once, because it checks that 10>0 initially.

Any alternate s

3条回答
  •  心在旅途
    2021-01-18 03:12

    I'm not sure brevity is a great criteria for doing this, but you don't need the map in the inverting solution, nor reverse in the reversing one:

    # By Inverting without map, one of:
    for(-10..0){$_=-$_;say}
    for(-10..0){$_*=-1;say}
    # Compare to similar length with map:
    for(map-$_,-10..0){say}
    
    # Can just use -$_ where $_ is used, if $_ is used < 6 times; that's shorter.
    for(-10..0){say-$_}
    
    # By Reversing without reverse (in a sub; in main use @ARGV or @l=...=pop@l)
    @_=0..10;while($_=pop){say}
    
    # More Pop Alternatives
    for(@_=0..10;$_=pop;say){}
    @_=0..10;for(;$_=pop;){say}
    @_=0..10;do{say$_=pop}while$_
    ($_,@_)=(10,0..9);do{say}while($_=pop)
    # Though, yeah, it's shorter with reverse
    for(reverse 0..10){say}
    

提交回复
热议问题