in reply to Re: use array for hash-keys without loop
in thread use array for hash-keys without loop

Impossible. You're asking to do something that's the very definition of looping without using a loop. What do you actually want?

At university I learned that loops can be replaced by recursivity. So the simple code below fulfills the requirements of the OP (one line, no loop) ... but maybe isn't what he wants. ;-)

However this statement stems from a course of theoretical computer science. Don't use the code below in a productive environment. It is slower and takes much more resources than the other, straightforward solutions presented in this thread!

You have been warned! Rata

use strict; use warnings; my @x = qw ( a b c d e ff ggg hhhh iiii ); my %y; # Tata!!! Here comes the requested line of code: noLoop(); sub noLoop { $y{pop(@x)} = 1; noLoop() if (scalar(@x)); } # + btw.: using global vars in subs is usually bad style ;-) print keys(%y), "\n"; exit 0;

Replies are listed 'Best First'.
Re^3: use array for hash-keys without loop
by ikegami (Patriarch) on Mar 11, 2010 at 17:07 UTC
    Yes, there's a lot of different ways of writing loops.

    Note that your code can be simplified to

    my %y; $y{shift(@x)} = 1 while @x;
    or the non-destructive
    my %y = map { $_ => 1 } @x;