EMBPERL Getting Values Out of Hashes Using Loops

2 ways to get values out of a hash using loops.

Make the hash

[- %customers = (
"AT&T Corp","1.2.11.1",
"BellSouth Corp","1.2.16.1",
"British Telecommunications","1.2.19.1",
"BroadWing Inc","1.2.155",
"China Unicom","1.2.3"
) -]

Output using "foreach"

    [$ foreach $name (sort keys %customers) $]
  • [+ $name +]
  • [$ endforeach $]

Output using "do"

[- $total = scalar(keys %customers); -]
[- $i = 0 -]
    [$ do $] [- @k = keys %customers; @v = values %customers; -]
  • [+ $k[$i] +]
  • [- $i++ -] [$ until $i == $total $]