  Home  |  About Us  |  Link To Us  |  FAQ  |  Contact  # Array::Each::Tutorial 0.02

Date Added: January 13, 2010  |  Visits: 1.758 Report Broken Link Printer Friendly Version Product Homepage Download (112 downloads)

Array::Each::Tutorial - POD giving various examples how to use Array::Each. SYNOPSIS man Array::Each man Array::Each::Tutorial or perldoc Array::Each perldoc Array::Each::Tutorial Overview This tutorial contains only POD, so dont do this: use Array::Each::Tutorial; # dont do this Rather, simply read the POD (as you are doing). But first, please read the docs for Array::Each, because the whole scoop is there. This tutorial is intended to augment those docs with examples showing situations where you might want to use Array::Each instead of other techniques. EXAMPLES Parallel Arrays vs. Using a Hash First of all, use a hash. Its almost always the best solution if you want to associate a "key" with a "value". And there are modules available that will let you do wonderful things with hashes, like keeping the keys sorted or keeping them in the order they were added. So given a hash, you might at some point want to do this: my %h = ( a=>1, b=>2, c=>3, d=>4, e=>5 ); while( my( \$k, \$v ) = each %h ) { # ... do something with \$k and \$v ... } On the other hand, if parallel arrays better implement your algorithm, then you may find you want to do something like this: my @k = qw( a b c d e ); my @v = qw( 1 2 3 4 5 ); for my \$i ( 0 .. \$#k ) { my( \$k, \$v ) = ( \$k[\$i], \$v[\$i] ); # ... do something with \$k and \$v (and maybe \$i) ... } Using Array::Each, you could do the same thing this way: use Array::Each; my @k = qw( a b c d e ); my @v = qw( 1 2 3 4 5 ); my \$obj = Array::Each->new( @k, @v ); while( my( \$k, \$v, \$i ) = \$obj->each ) { # ... do something with \$k and \$v (and maybe \$i) ... } If you dont need \$i at all, you can leave it out, e.g., while( my( \$k, \$v ) = \$obj->each ) { # ... do something with \$k and \$v ... } If you have more than two parallel arrays, include them all in the call to new() and add as many "capture" variables as you need, e.g., my @k = qw( a b c d e ); my @v = qw( 1 2 3 4 5 ); my @p = qw( - + ~ = : ); my \$obj = Array::Each->new( @k, @v, @p ); while( my( \$k, \$v, \$p, \$i ) = \$obj->each ) { # ... do something with \$k, \$v, and \$p (and maybe \$i) ... }.

 Requirements: No special requirements Platforms: Linux Keyword: Arrayeachtutorial,  C D E,  Do Something,  Examples,  How To,  Pod,  Qw,  Something,  Want To Users rating: 0/10        