Comparing arrays in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing arrays in perl
# 1  
Old 12-12-2008
Comparing arrays in perl

Hi all,

I am trying to compare two arrays in perl using the following code.
foreach $item (@arrayA){
push(@arrayC, $item) unless grep(/$item/, @arrayB);
}

The above code stores in arrayC all the elements of arrayA that do not belong in arrayB. The problem is the following.

If for example @arrayA[3]=1121 and @arrayB[13]=11213456
The above code will return that 1121 of arrayA exists in arrayB. This is not correct. How can i overcome this?

Best regards,
Christos
# 2  
Old 12-12-2008
I will suggest using

List::Compare module from CPAN

List::Compare - Compare elements of two or more lists - search.cpan.org

this is very handy and useful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

2. Shell Programming and Scripting

Comparing 2 arrays but ignoring certain patterns

I'm trying to compare 2 array and print the difference at a 3rd file. However how am i going to compare this 2 arrays by ignoring certain patterns: For example: 1st array contains: ctahci cptcsa0 ctsata:25:seed cptcsa1:50:seed ctsata_1:25:seed 2nd array contains: cptcsa0 ctsata... (0 Replies)
Discussion started by: hongping
0 Replies

3. Shell Programming and Scripting

Arrays in perl

Hi all, I have a log file which has logs. I am reading logs line by line into perl arrays. I want to print all the arrays elements starting from 8(word) to end of the line. print array......array to a new file. and I have to do it in perl as res of the program in perl. Please help me on... (9 Replies)
Discussion started by: firestar
9 Replies

4. UNIX for Dummies Questions & Answers

Comparing lists.. Arrays maybe?

Problem Part 1. Gather data from linux server and output to a file named data_DDMMYY Output file must contain the file name and size Part 2. Compare todays data_DDMMYY to yesterdays data_DDMMYY and output results to a file named difference_DDMMYY Output file must show the difference in... (3 Replies)
Discussion started by: Aussiemick
3 Replies

5. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

6. Shell Programming and Scripting

perl arrays

Hi I need some help using arrays in perl. I have an array say var and a variable var1. I want to check if the var1 is present in the array. How do I check that ? my @var = 1...10; my $var1 =5; if ( $var1 in @var ) { ....... } else { ....... } Something like above. Can some... (2 Replies)
Discussion started by: ammu
2 Replies

7. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies

8. Shell Programming and Scripting

perl functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
1 Replies

9. Shell Programming and Scripting

Comparing Arrays?

Is there anyway that I can compare two Arrays to see if any new strings have been added in them? eg: Array 1: Joe Bob Jane Array 2: Joe Bob Jane Greg It would then output a new array with the changes: Array 3: Greg I'm not very good at shell scripting, and my google and forum searches... (4 Replies)
Discussion started by: blckleprd
4 Replies

10. Programming

perl arrays

hello ppl, i'm coding a perl script and i have the following situation: @array1 = ("test1", "test2", "test3"); @array2 = ("something1", "something2", "something1"); $var1 = "with_one_of_the_array1_values"; $var2 = "with_one_of_the_array2_values"; what i want to do is to compare $var1... (2 Replies)
Discussion started by: crashnburn
2 Replies
Login or Register to Ask a Question