anadem has asked for the wisdom of the Perl Monks concerning the following question:
I need to examine numerous collections of items (strings), and if specific items (the same 6 items throughout, only 3 shown below) are missing from a collection then they must be added (but added only if missing). Each collection has from five to thirty items in it.
Something like this ungainly semi-pseudocode:
My bones tell me that perl can do this much more elegantly ... please suggest how, kind brothersfor each collection { $have-apple = 0; $have-orange = 0; $have-pear = 0; #examine about 30 items: for each item { if( $item eq "apple" ) { $have-apple = 1; } if( $item eq "orange" ) { $have-orange = 1; } if( $item eq "pear" ) { $have-pear = 1; } } # then, when all items examined unless( $have-apple ) { add apple; } unless( $have-orange ) { add orange; } unless( $have-pear ) { add pear; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: are they all there?
by jhourcle (Prior) on Aug 22, 2006 at 16:44 UTC | |
|
Re: are they all there?
by ikegami (Patriarch) on Aug 22, 2006 at 17:01 UTC | |
by anadem (Scribe) on Aug 22, 2006 at 17:06 UTC | |
|
Re: are they all there?
by Not_a_Number (Prior) on Aug 22, 2006 at 18:16 UTC | |
by ikegami (Patriarch) on Aug 22, 2006 at 18:30 UTC | |
by ikegami (Patriarch) on Aug 23, 2006 at 19:24 UTC | |
|
Re: are they all there?
by jwkrahn (Abbot) on Aug 22, 2006 at 18:02 UTC | |
|
Re: are they all there?
by shmem (Chancellor) on Aug 22, 2006 at 20:57 UTC | |
|
Re: are they all there?
by artist (Parson) on Aug 22, 2006 at 17:28 UTC | |
by ikegami (Patriarch) on Aug 22, 2006 at 18:11 UTC |