pglenski has asked for the wisdom of the Perl Monks concerning the following question:

I have this code (undef,undef,undef,undef,undef,$hic) = split(/\*/). It works fine. I was wondering if you could use the "x" operator and remove some of the undef? More like this: ((undef x 5), $hic) = split(/\*/); I know the above does not work but anyway it can be done?

Replies are listed 'Best First'.
Re: undef and "x" operator
by tilly (Archbishop) on Aug 09, 2006 at 21:20 UTC
    The usual approach is $hic = ( split(/\*/) )[5];
      Or $hic = ( split(/\*/,$_,7) )[5]; to split not more than necessary.
      Update: Thanks to lidden for showing my mistake (had a 6 instead of 7).

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        s/6/7/

        or the sixth field will contain too much.
Re: undef and "x" operator
by duckyd (Hermit) on Aug 09, 2006 at 23:15 UTC
    If you just want to last item, you could use
    my $hic = (split(/\*/))[-1];
Re: undef and "x" operator
by ysth (Canon) on Aug 10, 2006 at 01:30 UTC
    If it did work, it would be (((undef) x 5), $hic) =, since ()x is a different operator than x, and the former is what you would want. But it doesn't in fact work:
    $ perl -we'(((undef) x 5), $foo) = 1..6; print $foo' Can't modify repeat (x) in list assignment at -e line 1, near "6;"
      #!/usr/bin/perl -w use strict; ( @![(0)x5], my $x )= split /-/, "a-b-c-d-e-f-g-h-i-j"; # (undef)x5, # @![1..5], # @{[]}[(0)x5], print "\$x=($x)\n"; __END__ $x=(f)

      Note that no extra keystrokes are required. (: You can golf off one keystroke at the expense of more memory consumed. Update: For two or three extra keystrokes you can avoid damaging the important global @! array.

      Yes, if I got code from someone that used this, I would indeed shoot them.

      - tye        

Re: undef and "x" operator
by Skeeve (Parson) on Aug 09, 2006 at 23:54 UTC
    Just for the fun of it:
    $_="0*1*2*3*4*5*6*7*8"; eval '(' . 'undef,' x 5 . '$hic)=split/\*/'; print $hic;

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e