Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Combining Duplicate entries in an Array

by BrowserUk (Patriarch)
on Feb 22, 2012 at 19:04 UTC ( [id://955602]=note: print w/replies, xml ) Need Help??


in reply to Combining Duplicate entries in an Array

If you step through the array backward, you can delete (splice) out the redundant elements safely.

This will do it, but the code doesn't format nicely:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my @a = map[ split ], <DATA>; substr( $a[$_-1][0],0,-1 ) eq substr( $a[$_][0],0,-1 ) and do{ $a[$_-1][1] += $a[$_][1]; splice @a, $_, 1; } for reverse 1 .. $#a; pp \@a; __DATA__ 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

Output:

C:\test>junk [ ["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.96.0", 0], ["38.153.162.95.64.0", 0], ["58.152.64.24.192.0", 1], ["58.152.64.46.48.0", 3], ["58.152.94.71.0.0", 1], ]

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Combining Duplicate entries in an Array
by mmartin (Monk) on Feb 22, 2012 at 20:18 UTC
    Ok, so I modified you example a little bit. Didn't change anything except the way it's structured. The way you did it makes sense but is a little hard for me to read, and possibly maintain later if I ever have to change something.

    Here is what I modified to:
    for (my $x = $#temp; $x >= 0; $x--) { if ( substr($temp[$x-1][0], 0, -1) eq substr($temp[$x][0], 0, -1)) { $temp[$x-1][1] += $temp[$x][1]; splice @temp, $x, 1; } }
    Does the same thing just a little more long winded and simpler for a noob..


    Thanks Again for Your Help,
    Matt

Re^2: Combining Duplicate entries in an Array
by mmartin (Monk) on Feb 22, 2012 at 19:39 UTC
    Hey BrowserUk, thanks for the reply.

    Cool I'll give that a try, thanks! As I was writing my original post I began thinking about possibly being a better idea to loop in reverse. So thanks for that...


    Thanks Again,
    Matt


Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://955602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-19 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found