Sponsored Content
Full Discussion: Help with array comparison
Top Forums UNIX for Dummies Questions & Answers Help with array comparison Post 302258024 by radoulov on Thursday 13th of November 2008 04:30:47 PM
Old 11-13-2008
I'm not sure if this is an array or input.
Anyway, consider the following approach:

Code:
#! /usr/bin/perl

use warnings;
use strict;

my %Seen;

while (<DATA>) {
  print unless $Seen{(split /\|/)[7,8]}++;
  }


__DATA__
1|1|2|2|I|2|0|DUNN|LACY||||||
2|2|2|2|I|2|0|KOFE|ROGER||||||
3|3|2|2|A|2|0|KOFOED|ROBERT||||||
3|4|2|2|A|2|0|KOFOED|ROBERT||||||
3|5|2|2|A|2|0|KOFOED|ROBERT||||||
2|7|2|2|I|2|0|WILLIAMSON|JAMES||||||
3|8|2|2|I|2|0|MUNN|MARION||||||
4|9|2|2|A|2|0|BLOCK|ROY||||||

Which produces:

Code:
$ ./p  
1|1|2|2|I|2|0|DUNN|LACY||||||
2|2|2|2|I|2|0|KOFE|ROGER||||||
3|3|2|2|A|2|0|KOFOED|ROBERT||||||
2|7|2|2|I|2|0|WILLIAMSON|JAMES||||||
3|8|2|2|I|2|0|MUNN|MARION||||||
4|9|2|2|A|2|0|BLOCK|ROY||||||

You may try join '|', (split /\|/)[7,8] as a hash key for more precise processing.
 

10 More Discussions You Might Find Interesting

1. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

2. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

3. Shell Programming and Scripting

Awk multiple variable array: comparison

Foo.txt 20 40 57 50 22 51 66 26 17 15 63 18 80 46 78 99 87 2 14 14 51 47 49 100 58 Bar.txt 20 22 51 15 63 78 99 55 51 58 How to get output using awk 20 22 57 50 51 15 26 17 63 78 80 46 99 55 - - 51 58 49 100 (5 Replies)
Discussion started by: genehunter
5 Replies

4. Shell Programming and Scripting

perl, put one array into many array when field is equal to sth

Hi Everyone, #!/usr/bin/perl use strict; use warnings; my @test=("a;b;qqq;c;d","a;b;ggg;c;d","a;b;qqq;c;d"); would like to split the @test array into two array: @test1=(("a;b;qqq;c;d","a;b;qqq;c;d"); and @test2=("a;b;ggg;c;d"); means search for 3rd filed. Thanks find the... (0 Replies)
Discussion started by: jimmy_y
0 Replies

5. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

6. Homework & Coursework Questions

passing letters from an array into a string for string comparison

attempting the hangman program. This was an optional assignment from the professor. I have completed the logical coding, debugging now. ##I have an array $wordString that initializes to a string of dashes ##reflecting the number of letters in $theWord ##every time the user enters a (valid)... (5 Replies)
Discussion started by: lotsofideas
5 Replies

7. Shell Programming and Scripting

Array elements comparison using perl

Experts, I am looking to compare elements of 2 array using perl. Below is not the actual code but logic wise something like this. my $version = "MYSQlcl-5.2.4-264.x86_64"; <-- split this word into array as (5 2 4 264) ( which is to extract only the version number from the package name) my... (1 Reply)
Discussion started by: solaix14
1 Replies

8. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

9. Shell Programming and Scripting

Using associative array for comparison

Hello together, i make something wrong... I want an array that contains information to associate it for further processing. Here is something from my bash... You will know, what I'm trying to do. I have to point out in advance, that the variable $SYSOS is changing and not as static as in my... (2 Replies)
Discussion started by: Decstasy
2 Replies

10. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies
ARRAY_UDIFF_UASSOC(3)							 1						     ARRAY_UDIFF_UASSOC(3)

array_udiff_uassoc - Computes the difference of arrays with additional index check, compares data and indexes by a callback function

SYNOPSIS
array array_udiff_uassoc (array $array1, array $array2, [array $...], callable $value_compare_func, callable $key_compare_func) DESCRIPTION
Computes the difference of arrays with additional index check, compares data and indexes by a callback function. Note that the keys are used in the comparison unlike array_diff(3) and array_udiff(3). PARAMETERS
o $array1 - The first array. o $array2 - The second array. o $value_compare_func - The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. int callback (mixed $a, mixed $b) o $key_compare_func - The comparison of keys (indices) is done also by the callback function $key_compare_func. This behaviour is unlike what array_udiff_assoc(3) does, since the latter compares the indices by using an internal function. RETURN VALUES
Returns an array containing all the values from $array1 that are not present in any of the other arguments. EXAMPLES
Example #1 array_udiff_uassoc(3) example <?php class cr { private $priv_member; function cr($val) { $this->priv_member = $val; } static function comp_func_cr($a, $b) { if ($a->priv_member === $b->priv_member) return 0; return ($a->priv_member > $b->priv_member)? 1:-1; } static function comp_func_key($a, $b) { if ($a === $b) return 0; return ($a > $b)? 1:-1; } } $a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),); $b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),); $result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key")); print_r($result); ?> The above example will output: Array ( [0.1] => cr Object ( [priv_member:private] => 9 ) [0.5] => cr Object ( [priv_member:private] => 12 ) [0] => cr Object ( [priv_member:private] => 23 ) ) In our example above you see the "1" => new cr(4) pair is present in both arrays and thus it is not in the output from the function. Keep in mind that you have to supply 2 callback functions. NOTES
Note Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_udiff_uassoc($array1[0], $array2[0], "data_compare_func", "key_compare_func");. SEE ALSO
array_diff(3), array_diff_assoc(3), array_udiff(3), array_udiff_assoc(3), array_intersect(3), array_intersect_assoc(3), array_uinter- sect(3), array_uintersect_assoc(3), array_uintersect_uassoc(3). PHP Documentation Group ARRAY_UDIFF_UASSOC(3)
All times are GMT -4. The time now is 09:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy