[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: handling lists in perl



On Tue, Dec 18, 2018 at 01:34:53PM +0000, mick crane wrote:
> I'm not very good at perl (or anything else ) and could maybe sort it but
> perhaps there is an extension that does it.
> I have an list of pairs
> (1=>8,2=>20,6=>100,15=>100....)
> and an array of unique numbers
> (1 21 100 8 15 22 6 12 15.... )
> I want to see what pairs can be satisfied from the array of numbers, send
> that list to a file and also to another file the left over numbers.

Start by converting the "array of unique numbers" to a hash (associative
array, dictionary).  That way, you can quickly look up which numbers are
in, or not in, the data set.

Next, set up a second hash to record which numbers have been matched (vs.
which have not) in the following step.  It starts out empty.

Now, iterate over your "pairs".  If both members of the "pair" are
in the first hash, then write the pair to the appropriate output bin,
and add both numbers to the second hash.

When that's finished, you want to find out which numbers from the first
hash are not present in the second hash.  So, iterate over the keys of
the first hash, and any key that is not also in the second hash, write
to the appropriate output bin.

This can be done in any language that has hashes (associative arrays,
dictionaries) and the ability to iterate over a list.  Perl certainly
qualifies, if that's your language of choice.  Python and Tcl can also
do it quite easily.  Even bash could do this, although it would be slow,
and would not be my first pick.

If you need help actually *writing* it in Perl (or whatever language),
you should ask for help in a language-specific mailing list.


Reply to: