Sponsored Content
Full Discussion: Compare two arrays
Top Forums Shell Programming and Scripting Compare two arrays Post 302762893 by frodo61 on Wednesday 30th of January 2013 04:17:40 AM
Old 01-30-2013
Thanks. The nested for loop works but if I want to output files that arent found as well I get repeated messages due to not matching to any of the array elements, how could I only output the filenames that arent found just once? I guess I could put them into another variable and then use the uniq command maybe?

Code:
for orig in ${origfilelist}
do
        for fail in ${failfiles}
        do
                if [[ ${orig} == ${fail} ]]
                then
                print -- "File ${orig} found "
else
print -- "File ${orig} not found"
                fi
        done
 
done

Thanks

p.s I though all variables were arrays?

---------- Post updated 30-01-13 at 09:17 AM ---------- Previous update was 29-01-13 at 01:11 PM ----------

Although I still would like the answer to the previous question I also need to know how to output the array elements onto different lines in a text file?

IFS solutions dont seem to be working.

Code:
 
OLDIFS="$IFS"; IFS=$'\n'
echo "${failagain[*]}" >>"${reloadfiles}"/results.txt
IFS="$OLDIFS"

I have now been able to output to single lines by using another for loop.

Code:
 
for i in ${failagain[*]}
do
echo $i >>"${reloadfiles}"/results.txt
done


Last edited by frodo61; 01-30-2013 at 06:10 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two arrays in sh or compare two fields

I want a soultion to compare two arrays in sh with an easy way.I want a solution to synchrose users between different AIX servers where no NIS is available. All users are meant to be same on all 10 servers. So the approach is to consider first server as master user repository and whatever the users... (0 Replies)
Discussion started by: rijeshpp
0 Replies

2. Shell Programming and Scripting

Perl - Compare 2 Arrays

Hi all, I have the following script where the contents of file1 and file2 would be something like this: file1: 56790,0,0,100998765 89756,0,0,100567876 867645,1,3,678777654 file2: 56790,0,0,100998765 65776,0,0,4766457890 +5896,0,0,675489876 What I then want to do is check if... (4 Replies)
Discussion started by: Donkey25
4 Replies

3. Shell Programming and Scripting

compare/match arrays

Hi there all, I am having a question. Is it posible to compare elements of 2 different arrays? For example I got Array 1 | Array 2 123_abc | 123_bcd 123_bcd | 234_bcd 234_abc | 567_abc 234_bcd | 123_abc than the match is 123_abc & 234_bcd and non of the others. So... (3 Replies)
Discussion started by: draco
3 Replies

4. Shell Programming and Scripting

Compare arrays in perl

Hello, Let's say that we have the two following arrays @array1= @array2= Is there any easy way to compare these two arrays and print the values that exist in array1 and not in array2 and the values that exist in array2 and not in array1? Regards, Chriss_58 (3 Replies)
Discussion started by: chriss_58
3 Replies

5. Shell Programming and Scripting

compare 2 arrays in perl

Hi Im supposed to compare lines in a file : KB0005 1019 T IFVATVPVI 0.691 PKC YES KB0005 1036 T YFLQTSQQL 0.785 PKC YES KB0005 1037 S FLQTSQQLK 0.585 DNAPK YES KB0005 1045 S KQLESEGRS 0.669 PKC YES KB0005 1045 S KQLESEGRS 0.880 unsp YES KB204320 1019 T IFVATVPVI 0.699 PKC YES ... (7 Replies)
Discussion started by: karla
7 Replies

6. Shell Programming and Scripting

Compare arrays (perl)

Hi, my first post here! Description of my problem: I have one txt-file with six rows and each row contains seven numbers seperated with whitespaces. I want to: Compare one array with seven numbers with each row of numbers in the txt-file. I have managed to compare one array with... (6 Replies)
Discussion started by: mjoh
6 Replies

7. Shell Programming and Scripting

Perl Compare 2 Arrays

Hello, Consider the following 2 arrays: Array1 = qw(Fa0/0 Fa0/1 Fa0/2 Fa0/3); Array1 = qw(Fa0/1 Fa0/2 Fa0/3 Fa0/4); I want to compare the following 2 arrays as follows: Take specific action when elements of Array1 that doesn't exist in Array2 (in my example: Fa0/0). Take another... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

8. Shell Programming and Scripting

perl: compare two arrays

Hi friends, I want to compare two arrays and find matched one using perl? Also, I want to delete unmatched one. Plz suggest me solution (1 Reply)
Discussion started by: Renesh
1 Replies

9. Shell Programming and Scripting

Bash arrays that compare ip addresses.

I've been trying to have an array of ip addresses go through a loop one at a time. Then compare if the current element is in another array of ip addresses. I've traced my error with /bin/bash -x + for c in '"${ip}"' ./netk5: line 65: 50.17.231.23 23.64.146.110 23.64.159.139 107.14.36.129... (17 Replies)
Discussion started by: Azrael
17 Replies

10. Shell Programming and Scripting

Using Diff to compare 2 arrays

I have two arrays and they look like this: array=(`cat /local/mnt/*sys/*includes|grep -v NEW`) array2=(`cat /tmp/*sys.z |grep -v NEW`) I am trying to compare them but I need to use the diff -u command. I am not sure how to do this. I cannot just do diff -u ${array} ${array2} I cannot... (4 Replies)
Discussion started by: newbie2010
4 Replies
Array::Compare(3pm)					User Contributed Perl Documentation				       Array::Compare(3pm)

NAME
Array::Compare - Perl extension for comparing arrays. SYNOPSIS
use Array::Compare; my $comp1 = Array::Compare->new; $comp->Sep('|'); $comp->Skip({3 => 1, 4 => 1}); $comp->WhiteSpace(0); $comp->Case(1); my $comp2 = Array::Compare->new(Sep => '|', WhiteSpace => 0, Case => 1, Skip => {3 => 1, 4 => 1}); my @arr1 = 0 .. 10; my @arr2 = 0 .. 10; $comp1->compare(@arr1, @arr2); $comp2->compare(@arr1, @arr2); DESCRIPTION
If you have two arrays and you want to know if they are the same or different, then Array::Compare will be useful to you. All comparisons are carried out via a comparator object. In the simplest usage, you can create and use a comparator object like this: my @arr1 = 0 .. 10; my @arr2 = 0 .. 10; my $comp = Array::Compare->new; if ($comp->compare(@arr1, @arr2)) { print "Arrays are the same "; } else { print "Arrays are different "; } Notice that you pass references to the two arrays to the comparison method. Internally the comparator compares the two arrays by using "join" to turn both arrays into strings and comparing the strings using "eq". In the joined strings, the elements of the original arrays are separated with the "^G" character. This can cause problems if your array data contains "^G" characters as it is possible that two different arrays can be converted to the same string. To avoid this, it is possible to override the default separator character, either by passing and alternative to the "new" function my $comp = Array::Compare->new(Sep => '|'); or by changing the seperator for an existing comparator object $comp->Sep('|'); In general you should choose a separator character that won't appear in your data. You can also control whether or not whitespace within the elements of the arrays should be considered significant when making the comparison. The default is that all whitespace is significant. The alternative is for all consecutive white space characters to be converted to a single space for the pruposes of the comparison. Again, this can be turned on when creating a comparator object: my $comp = Array::Compare->new(WhiteSpace => 0); or by altering an existing object: $comp->WhiteSpace(0); You can also control whether or not the case of the data is significant in the comparison. The default is that the case of data is taken into account. This can be changed in the standard ways when creating a new comparator object: my $comp = Array::Compare->new(Case => 0); or by altering an existing object: $comp->Case(0); In addition to the simple comparison described above (which returns true if the arrays are the same and false if they're different) there is also a full comparison which returns a list containing the indexes of elements which differ between the two arrays. If the arrays are the same it returns an empty list. In scalar context the full comparison returns the length of this list (i.e. the number of elements that differ). You can access the full comparision in two ways. Firstly, there is a "DefFull" attribute. If this is "true" then a full comparison if carried out whenever the "compare" method is called. my $comp = Array::Compare->new(DefFull => 1); $comp->compare(@arr1, @arr2); # Full comparison $comp->DefFull(0); $comp->compare(@arr1, @arr2); # Simple comparison $comp->DefFull(1); $comp->compare(@arr1, @arr2); # Full comparison again Secondly, you can access the full comparison method directly $comp->full_compare(@arr1, @arr2); For symmetry, there is also a direct method to use to call the simple comparison. $comp->simple_compare(@arr1, @arr2); The final complication is the ability to skip elements in the comparison. If you know that two arrays will always differ in a particular element but want to compare the arrays ignoring this element, you can do it with Array::Compare without taking array slices. To do this, a comparator object has an optional attribute called "Skip" which is a reference to a hash. The keys in this hash are the indexes of the array elements and the values should be any true value for elements that should be skipped. For example, if you want to compare two arrays, ignoring the values in elements two and four, you can do something like this: my %skip = (2 => 1, 4 => 1); my @a = (0, 1, 2, 3, 4, 5); my @b = (0, 1, X, 3, X, 5); my $comp = Array::Compare->new(Skip => \%skip); $comp->compare(@a, @b); This should return true, as we are explicitly ignoring the columns which differ. Of course, having created a comparator object with no skip hash, it is possible to add one later: $comp->Skip({1 => 1, 2 => 1}); or: my %skip = (1 => 1, 2 => 2); $comp->Skip(\%skip); To reset the comparator so that no longer skips elements, set the skip hash to an empty hash. $comp->Skip({}); You can also check to see if one array is a permutation of another, i.e. they contain the same elements but in a different order. if ($comp->perm(@a, @b) { print "Arrays are perms "; else { print "Nope. Arrays are completely different "; } In this case the values of "WhiteSpace" and "Case" are still used, but "Skip" is ignored for, hopefully, obvious reasons. METHODS
new [ %OPTIONS ] Constructs a new comparison object. Takes an optional hash containing various options that control how comparisons are carried out. Any omitted options take useful defaults. Sep This is the value that is used to separate fields when the array is joined into a string. It should be a value which doesn't appear in your data. Default is '^G'. WhiteSpace Flag that indicates whether or not whitespace is significant in the comparison. If this value is false then all multiple whitespace characters are changed into a single space before the comparison takes place. Default is 1 (whitespace is significant). Case Flag that indicates whther or not the case of the data should be significant in the comparison. Default is 1 (case is significant). Skip a reference to a hash which contains the numbers of any columns that should be skipped in the comparison. Default is an empty hash (all columns are significant). DefFull Flag which indicates whether the default comparison is simple (just returns true if the arrays are the same or false if they're not) or full (returns an array containing the indexes of the columns that differ). Default is 0 (simple comparison). compare_len @ARR1, @ARR2 Very simple comparison. Just checks the lengths of the arrays are the same. compare @ARR1, @ARR2 Compare the values in two arrays and return a data indicating whether the arrays are the same. The exact return values differ depending on the comparison method used. See the descriptions of simple_compare and full_compare for details. Uses the value of DefFull to determine which comparison routine to use. simple_compare @ARR1, @ARR2 Compare the values in two arrays and return a flag indicating whether or not the arrays are the same. Returns true if the arrays are the same or false if they differ. Uses the values of 'Sep', 'WhiteSpace' and 'Skip' to influence the comparison. full_compare @ARR1, @ARR2 Do a full comparison between two arrays. Checks each individual column. In scalar context returns the number of columns that differ (zero if the arrays are the same). In list context returns an list containing the indexes of the columns that differ (an empty list if the arrays are the same). Uses the values of 'Sep' and 'WhiteSpace' to influence the comparison. Note: If the two arrays are of different lengths then this method just returns the indexes of the elements that appear in one array but not the other (i.e. the indexes from the longer array that are beyond the end of the shorter array). This might be a little counter-intuitive. perm @ARR1, @ARR2 Check to see if one array is a permutation of the other (i.e. contains the same set of elements, but in a different order). We do this by sorting the arrays and passing references to the assorted versions to simple_compare. There are also some small changes to simple_compare as it should ignore the Skip hash if we are called from perm. AUTHOR
Dave Cross <dave@mag-sol.com> SEE ALSO
perl(1). COPYRIGHT AND LICENSE
Copyright (C) 2000-2005, Magnum Solutions Ltd. All Rights Reserved. This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2011-12-18 Array::Compare(3pm)
All times are GMT -4. The time now is 11:49 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy