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

Hi Monks,
I have this code I am working on and just need someone to look at and tell me if there is a easy way to write this piece of code, cause I have to include the code into a table and the way it is is getting difficult.
I just need this to be in a standard easy way of writing a foreach loop like,
foreach $array_element(@Array) { print "$array_element "; }

Thanks and here is the code:
print $_ . ': ' . $erg{$_} . '<br />' . "\n" foreach (keys %erg);

Replies are listed 'Best First'.
Re: Foreach Question
by dragonchild (Archbishop) on May 10, 2004 at 13:29 UTC
    Another option would be:
    while (my ($k, $v) = each %erg) { print "$k: $v<br />\n"; }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Foreach Question
by PodMaster (Abbot) on May 10, 2004 at 12:36 UTC
    Is this what you want ?
    C:\>more f print $_ . ': ' . $erg{$_} . '<br />' . "\n" foreach (keys %erg); C:\>perl -MO=Deparse,-p f foreach $_ (keys(%erg)) { print((((($_ . ': ') . $erg{$_}) . '<br />') . "\n")); } f syntax OK

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.