Technology Get the latest on technology, electronics and software…

Any Perl programmer/scripter in the house?

Thread Tools
 
Old Mar 22, 2001 | 04:49 PM
  #1  
Thread Starter
Three Wheelin'
 
Joined: Aug 2000
Posts: 1,382
Likes: 0
Post Any Perl programmer/scripter in the house?

I am writing a script and one part is holding me up.

I want to compare 2 arrays and return the difference between them. EX:

@X = 2,5
@Y = 6,8,5,2

and ultimately return 6 and 8.

I wrote a nested foreach loop to compare each element from each array but it returns too much (6,8,5,6,8,2).

Any help would be appreciated.

------------------
'00 White Diamond Perl w/Nav
Wings West Spoiler
Comptech Exaust
Rear Comptech Sway bar
H&R OE Sport Springs
AEM CAI
Reply
Old Mar 22, 2001 | 07:17 PM
  #2  
Red Hot TL's Avatar
Drifting
 
Joined: Jul 2000
Posts: 2,480
Likes: 0
Post

the difference of what? the greatest number in each arrays? I don't know Perl, but sounds like general ideas. more details could help

------------------
Moderator on Acura-TL.com Forums

2000 Red Hot TL w/o nav
Burlwood Trim and Shift Knob
Wood Trim Set
Gutter Guard Grill
Nebraska Legal Darkest Tint
Goodyear Eagle HP Ultra Plus 225/55/16
Photos Of My RED HOT TL
Reply
Old Mar 22, 2001 | 08:31 PM
  #3  
TLracr's Avatar
Instructor
 
Joined: May 2000
Posts: 196
Likes: 0
Post

So you want to compare two strings and return anything thats not an exact match?



------------------
Dark Emerald Pearl 2000 3.2 TL with Nav, Sebring Mufflers, Sebring Quad Sport Exhaust System, Factory Spoiler, HP Quantum Mirror Tint, 6 Disc Alpine CD changer, 16" 139C Pacer wheels, Yokohama 215/55/16s, PIAA 1700x
------------

Click to visit my updated page with pictures of my TL!
http://www.stanford.edu/~sevan9
Reply
Old Mar 22, 2001 | 08:46 PM
  #4  
TLracr's Avatar
Instructor
 
Joined: May 2000
Posts: 196
Likes: 0
Post

hehe...watching the terps beating up on the hoyas right now. You guys are pretty good. I'm impressed with that Dixon guy.

------------------
Dark Emerald Pearl 2000 3.2 TL with Nav, Sebring Mufflers, Sebring Quad Sport Exhaust System, Factory Spoiler, HP Quantum Mirror Tint, 6 Disc Alpine CD changer, 16" 139C Pacer wheels, Yokohama 215/55/16s, PIAA 1700x
------------

Click to visit my updated page with pictures of my TL!
http://www.stanford.edu/~sevan9
Reply
Old Mar 22, 2001 | 09:38 PM
  #5  
Thread Starter
Three Wheelin'
 
Joined: Aug 2000
Posts: 1,382
Likes: 0
Post

OK, hope I can clarify more.

say I have 2 arrays of undefined length I want to return the elements that are unique to the other array. These arrays contains strings in my case IP addresses. Say:

@Array_IP1 = (207.226.255.102, 123.123.123.123, 255.255.255.254)

@Array_IP2 = (123.123.123.123, 255.255.255.254)

I would like a function to read those two arrays and return the unique one. Where in this case would be 207.226.255.102

Thanks for your help.


PS TERPS BABY!!!!!!!!!!!!!!!!!! Dixon is a mad man, gets mad steals! not a great day but Baxter was a monster tonite!!!

------------------
'00 White Diamond Perl w/Nav
Wings West Spoiler
Comptech Exaust
Rear Comptech Sway bar
H&R OE Sport Springs
AEM CAI
Reply
Old Mar 23, 2001 | 12:05 PM
  #6  
Red Hot TL's Avatar
Drifting
 
Joined: Jul 2000
Posts: 2,480
Likes: 0
Post

damn, Perl... I was gonna put it in C++ and hope that you can trasfer it. I can't read that code, well, not really. I would use a search function for the character '.', and when it finds it, it stores the first number into array in the "0" location, and so on. and after that, I'd use a for loop to compare the same position, like "0" position in array one and two, and then one and three til it hits the end. and switch to the "1" array and compare all of them. I'd use integers since you're comparing IP. section by section.

------------------
Moderator on Acura-TL.com Forums

2000 Red Hot TL w/o nav
Burlwood Trim and Shift Knob
Wood Trim Set
Gutter Guard Grill
Nebraska Legal Darkest Tint
Goodyear Eagle HP Ultra Plus 225/55/16
Photos Of My RED HOT TL
Reply
Old Mar 23, 2001 | 12:20 PM
  #7  
avengerjr's Avatar
Banned
 
Joined: Oct 2000
Posts: 1,147
Likes: 0
From: Little Elm, TX
Smile

Originally posted by Red Hot TL:
damn, Perl... I was gonna put it in C++ and hope that you can trasfer it. I can't read that code, well, not really. I would use a search function for the character '.', and when it finds it, it stores the first number into array in the "0" location, and so on. and after that, I'd use a for loop to compare the same position, like "0" position in array one and two, and then one and three til it hits the end. and switch to the "1" array and compare all of them. I'd use integers since you're comparing IP. section by section.

It is easier in PERL to insert and compare the IP's as strings b/c you can split the array on the commas (,) between the IP's. Then comparing 2 strings is easy.

You have to loop through the first array and compare to the second, then loop through the second and compare to the first, b/c you mentioned they could have different lengths. This way you will get all IP's. This also prevents the third array from getting duplicates, and saves you code from having to test for duplicates. Much cleaner.

PERL and C++ are pretty good at just being able to convert code, but PERL to C is a lot better.

I love coding and can do it in PERL, C, C++, JAVA, Korn Shell scripting, HTML,... pretty much whatever I need to code in. That's what I went to school for and what I do everyday!

I will try to see if I can clean up my code that I posted, maybe put some tabs in there to make it easier to read.


------------------
avengerjr
2001 CL-S Satin Silver, spoiler, wheel locks, mud guards, fenderwell trim, Xephyr CAI

"hockey + music + cars = life"

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">[This message has been edited by avengerjr on March 23, 2001 @ ]</font>
Reply
Old Mar 23, 2001 | 12:37 PM
  #8  
avengerjr's Avatar
Banned
 
Joined: Oct 2000
Posts: 1,147
Likes: 0
From: Little Elm, TX
Post

footnotes to my code:

Version of PERL is: 5.005_03

Version of UNIX written on: AIX 4.3.3 PSSP 3.1.x

Just so you know, in case there are any problems, you know what I wrote mine on.


------------------
avengerjr
2001 CL-S Satin Silver, spoiler, wheel locks, mud guards, fenderwell trim, Xephyr CAI

"hockey + music + cars = life"
Reply
Old Mar 23, 2001 | 01:10 PM
  #9  
Thread Starter
Three Wheelin'
 
Joined: Aug 2000
Posts: 1,382
Likes: 0
Post

avengerjr and the rest, thanks for your suggestion, I'm sure that would work perfectly, I had similar code in my first attempt to do this but didnt do the pass the second time therefore I got duplicate results(stupid me).

this is what I ended up with. This utilizes hash in perl with I found to be very effective.

[HR]
@A = @StatIPList;
@B = @IPList;
%HashA = ();
%HashB = ();

foreach $a (@A){
$HashA{$a} = "";
}
foreach $b (@B) {
if (exists($HashA{$b})) {
delete ($HashA{$b});
} else{
$HashB{$b} = "";
}
@NetA = keys(%HashA);
@NewIP = keys(%HashB);
}
[HR]

Perl is definately very effective. I am still learning to use perl to its full capabilites but so far its a powerfull language.

------------------
'00 White Diamond Perl w/Nav
Wings West Spoiler
Comptech Exaust
Rear Comptech Sway bar
H&R OE Sport Springs
AEM CAI
Reply
Old Mar 23, 2001 | 03:33 PM
  #10  
avengerjr's Avatar
Banned
 
Joined: Oct 2000
Posts: 1,147
Likes: 0
From: Little Elm, TX
Thumbs up

yup...Hashes are nice, until you start gettin 3 hashes deep. That got me in trouble, when I couldn't keep up with all my hashes and keys...pretty much "I got lost"

I have gotten better and can deal with 2 hashes very easily now, can still work with 3 or more deep, but it gets confusing...

Be careful with those hashes!!!

That code looks good, I prolly would have thought of that, had I had more time and put more thought into it! Good job!
Reply
Old Mar 23, 2001 | 11:58 PM
  #11  
avengerjr's Avatar
Banned
 
Joined: Oct 2000
Posts: 1,147
Likes: 0
From: Little Elm, TX
Thumbs up

here ya go... (you have to treat the IP's as strings )

Code:
#!/bin/perl
@array1 = ('155.155.155.155', '200.200.200.200', '255.255.255.255');
@array2 = ('200.200.200.200', '255.255.255.255', '100.100.100.100', '300.300.300.300');
$found = 0;
for $item (@array1)
{
        for $item2 (@array2)
        {
                if ($item eq $item2)
                {
                        $found = 1;
                }
        }
        if ($found != 1)
        {
                push @array3, $item;
        }
        $found = 0;
}

$found = 0;
for $anitem (@array2)
{
        for $anitem2 (@array1)
        {
                if ($anitem eq $anitem2)
                {
                        $found = 1;
                }
        }
        if ($found != 1)
        {
                push @array3, $anitem;
        }
        $found = 0;
}

for $item3 (@array3)
{
        print ("\narray3 contains :$item3\n");
}
The result is this:

Code:
array3 contains :155.155.155.155

array3 contains :100.100.100.100

array3 contains :300.300.300.300
which contains the 3 IP's that are unique to the 2 arrays. I hope this is what you are looking for...good luck!
------------------
avengerjr
2001 CL-S Satin Silver, spoiler, wheel locks, mud guards, fenderwell trim, Xephyr CAI

"hockey + music + cars = life"

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">[This message has been edited by avengerjr on March 23, 2001 @ ]</font>

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">[This message has been edited by avengerjr on March 23, 2001 @ ]</font>

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">[This message has been edited by avengerjr on March 23, 2001 @ ]</font>
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
rp_guy
Member Cars for Sale
9
Jul 16, 2017 07:33 AM
CL-S progression 01
Car Parts for Sale
65
Jan 26, 2016 04:15 PM
steve
2G TL (1999-2003)
5
Sep 30, 2015 09:23 PM




All times are GMT -5. The time now is 12:17 AM.