Hi guys !
I have a hash of arrays (the number of arrays is random and each array can hold different number of values), each array holds a list of numbers. The hash is built on runtime according to user's input from STDIN (each iteration builds one array), so far so good.
What I need is to verify that in each array the numbers are consecutive, all the arrays together should holds numbers from 0 to the higher number the user entered, and no number should appear more than once, for example:
Example 1:
(iteration 1): 0 1 2
(iteration 2): 4 6 5
### Wrong input - missing 3, and second array is not consecutive
Example 2:
(iteration 1): 4 5 6
(iteration 2): 0 1 2 3 4
### Wrong - 4 appears twice
Example 3:
(iteration 1): 4 5 6 7 8
(iteration 2): 0 1 2 3
### this one is OK
In general, I can join all the arrays to a big one, sort it, and check that each array value equals the array's index, but I'll fall in case the user enters:
first: 0 1 3
second: 2 4 5
The joined array will qualify but the little ones not.
anyone has a short idea (long solutions I have in plenty, but probably very ugly) ?
Thanks
Hotshot
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.