|Just another Perl shrine|
Combining Duplicate entries in an Arrayby mmartin (Monk)
|on Feb 22, 2012 at 18:43 UTC||Need Help??|
mmartin has asked for the
wisdom of the Perl Monks concerning the following question:
Ok, so this is driving me NUTS!!! I have this array (below) which is a simple 2D-Array. Which contains at position [x] will contain a Identifier number like "220.127.116.11.72" and the element in the same spot but at [x] will contain a single integer value, i.e. any number 0,1,2...100...etc.
Here is what the array looks like after running the following:
Now if you notice in the printout, some of the ID elements are almost the exact same ID except that if there is a similar one, the first one will end in "0" and the second one will end in "1".
What I've been trying to do is loop through the array and if the next element is the same as the current one then add the integer values together from the 2nd column and make it one element in a new array.
So some pseudo code would be (might be easier to understand this way):
I'm sure you get the gist of what I'm trying to say, but one more time... Take you through the 'code':
-- remove last "." and digit "0" or "1"
-- if this element matches the next element then add their values and set to new array
-- else just set the current element to the new array element
I hope I'm being clear enough...
If you need me to explain more let me know...
Any suggestions would be great.
Thanks in Advance,