Hello Monks,

Ok, so this is driving me NUTS!!! I have this array (below) which is a simple 2D-Array. Which contains at position [x][0] will contain a Identifier number like "94.22.10.141.72" and the element in the same spot but at [x][1] 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:
for (my $x = 0; $x <= $#tmp_numClients; $x++) { print "$AP_numClients[$x][0] \t $AP_numClients[$x][1]\n"; } __________ OUTPUT ____________ 19.25.55.11.144.0 5 19.25.55.14.16.0 12 19.25.59.200.208.0 8 19.25.59.204.160.0 7 19.25.60.5.176.0 4 19.25.60.15.48.0 0 19.25.60.17.240.0 3 19.25.60.18.96.0 5 19.25.115.138.224.0 30 19.25.115.141.32.0 4 26.109.108.64.144.0 1 38.153.162.89.0.0 1 38.153.162.89.0.1 0 38.153.162.89.96.0 0 38.153.162.89.96.1 0 38.153.162.95.64.0 0 38.153.162.95.64.1 0 58.152.64.24.192.0 1 58.152.64.24.192.1 0 58.152.64.46.48.0 3 58.152.64.46.48.1 0 58.152.94.71.0.0 1 58.152.94.71.0.1 0
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):

my @newArray; for (my $x = 0; $x <= $# AP_numClients; $x++)<br> { $AP_numClients[$x][0] =~ s/\.[0-1]$//; #--> remove the last di +git and "." if it ends in 0 or 1 # If current element matches the next element, then add their val +ues together... if ($AP_numClients[$x][0] =~ /$AP_numClients[$x+1][0]/) { $newArray[$x][0] = $AP_numClients[$x][0]; $newArray[$x][1] = $AP_numClients[$x][1] + $AP_numClients +[$x+1][1]; $x++; } else { $newArray[$x][0] = $AP_numClients[$x][0]; $newArray[$x][1] = $AP_numClients[$x][1]; } }

I'm sure you get the gist of what I'm trying to say, but one more time... Take you through the 'code':
-- Loop
-- 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
-- next


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,
Matt


In reply to Combining Duplicate entries in an Array by mmartin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.