Array::Each::Tutorial

Array::Each::Tutorial - POD giving various examples how to use Array::Each.
Download

Array::Each::Tutorial Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Brad Baxter
  • Publisher web site:
  • http://search.cpan.org/~bbaxter/Array-Each-0.02/Each.pm

Array::Each::Tutorial Tags


Array::Each::Tutorial Description

Array::Each::Tutorial - POD giving various examples how to use Array::Each. Array::Each::Tutorial - POD giving various examples how to use Array::Each.SYNOPSIS man Array::Each man Array::Each::Tutorialor perldoc Array::Each perldoc Array::Each::TutorialOverviewThis tutorial contains only POD, so don't do this: use Array::Each::Tutorial; # don't do thisRather, 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.EXAMPLESParallel Arrays vs. Using a HashFirst of all, use a hash. It's 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, $v ); # ... 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 don't 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:· Perl Requirements: · Perl


Array::Each::Tutorial Related Software