in reply to Sorting numbers

Oops! Misread the problem. Here's something that works correctly

$, = $/; $a[$_] = ++$line while <>; print map $a[$_] || (), $[ .. $#a;

I goofed and wrote down the right answer to the wrong problem. Oops! This works if all of your numbers are positive.

$, = $/; undef @a[<>]; print grep exists $a[$_], $[ .. $#a;

Replies are listed 'Best First'.
Re^2: Sorting numbers (!exists)
by tye (Sage) on Jun 02, 2003 at 20:32 UTC

    Please don't use exists on array elements. It doesn't work on old versions of Perl and I hear that it will stop working again (thankfully) on some future version of Perl. You want defined instead. The difference between defined and exists makes sense for hash elements (tells you whether the key is present). The difference between defined and exists for array elements is an obscure memory allocation detail that you should never care about and can easily get wrong.

                    - tye