Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unique elements of an big array...?? Help(+) 1

Status
Not open for further replies.

AlexmMorozo

Technical User
Feb 16, 2003
10
GB
Hi!

I have some q.!
See the example pls.

file "RES":
@T=();
x u y_inv 1 1 0 14 ===> i need
==>@T=("x","u","y_inv");

x s_inv t 1 1 0 14
v r_inv u 1 1 0 14==>
==>@T=("x","u","y_inv","v",&quo t;t_inv","w");

v t_inv w 1 1 0 14
v t u_inv 1 1 0 14
x u_inv z_inv 1 1 0 14
v s_inv y 1 1 0 14
v u y_inv 1 1 0 14
v r u_inv 1 1 0 14
x r s_inv 1 1 0 14
v t_inv u 1 1 0 14
v s_inv z_inv 1 1 0 14
x u z 1 1 0 14
x w z 1 1 0 14
x s_inv z_inv 1 1 0 14
v r_inv s 1 1 0 14
x u_inv y 1 1 0 14
x r w_inv 1 1 0 14
...
p s z 1 2 0 13 ==>
==>@T=("x","u","y_inv","v","t_inv","w","p","s","z");

v u_inv w_inv 1 15 0 0
p s_inv t_inv 1 15 0 0
v r_inv t 1 0 15




my wrong script:
@T=();
open (FH4,&quot;<RES&quot;);
while(<FH4>)
{
chomp();
s/\s+/ /g;
split(' ');
$num++;

$out1=$_[0];
$out2=$_[1];
$out3=$_[2];


my $st1=$out1=~/(.+?)_/?$1:$out1;
my $st2=$out2=~/(.+?)_/?$1:$out2;
my $st3=$out3=~/(.+?)_/?$1:$out3;

if(($st1 ne $st2) && ($st1 ne $st3) && ($st2 ne $st3))
{


print &quot; $num> $out1 $out2 $out3 \n&quot;; last if(++$n>3);

push @TROYIKI,&quot;$out1 $out2 $out3&quot;;
if ($n==9){ last}
}

}

close FH4;

############################
STDOUT:
1> x u y_inv ----- ok!
2> x s_inv t ----- wrong! must => v r_inv w
3> v r_inv u ----- wrong! must => p s z

@T: x u y_inv x s_inv t v r_inv u --wrong!
#############################

Dream:)!
@T= (x u y_inv x s_inv t v r_inv u);
0 1 2 3 4 5 6 7 8


with best regards and than's!,
Al.
 
I've tried reading your question a few times, but I cannot make sense of exactly what it is you're trying to do. Can you describe what you're trying to accomplish and give some examples of desired input and output?
 
Goog day,
Excuse me! and large thank for the response!!!!


By the reason to that my intolerable English... :) :(

So,
I have file (<RES>)of the following structure:

INPUT:
======
x s_inv u 1 1 0 99
x r t_inv 1 1 0 99
v y z 1 1 0 99
v y_inv z_inv 1 1 0 99
x r_inv t 1 1 0 99
v s u_inv 1 1 0 99
x s u_inv 1 1 0 99
v s_inv w 1 2 0 98
x u_inv w 1 2 0 98
v r t_inv 1 2 0 98
x u w_inv 1 2 0 98
v r_inv t 1 2 0 98
x s_inv w 1 3 0 97
x s w_inv 1 3 0 97
v u_inv w 1 3 0 97
v u w_inv 1 3 0 97
v s w_inv 1 3 0 97
x t_inv w 1 4 0 96
x t w_inv 1 4 0 96
x r_inv w 1 5 0 95
x r w_inv 1 5 0 95
...
...
...
p r_inv u 1 22 0 78
p r_inv s 1 22 0 78
p s_inv w 1 22 0 78
p t_inv u 1 23 0 77
p u w_inv 1 23 0 77
p s t_inv 1 23 0 77
=============================================

I should find unique elements(3)(or better tell-LINEs, with unique, not repeating names).
For the submitted example:

OUTPUT: (unique elements/lines)
=======
x s_inv u
v y z
p r_inv s
===========================

thus it's important that:
s!=s_inv, r!=r_inv ...and so on


A task:
The found elements to bring in a array, for ex. @T.

@T=(x s_inv u v y z p r_inv s).

Number of unique lines ($n), for this example we shall accept =3.
:) sorry, English..
$n=3.

Thank you!
Al.
 
I'm sorry, I don't see any definition of uniqueness that will give you this output
Code:
OUTPUT: (unique elements/lines)
=======
             x s_inv u
             v y     z
             p r_inv s
===========================
based on your posted input. Are the following line equvalent or distinct? (i.e. does the ordering of the lines matter?)
Code:
v u_inv w 1 3     0 97    
v w u_inv 1 3     0 97
Could you provide a definition of what you mean by 'unique'?

jaa
 
....
Sorry, may be my explain so worse...

Concept 'unique' for me it:
first three elements do not repeat in the subsequent two (n) lines.
For an example, we can use my &quot;dream&quot;/ideal output:
=====================================================

x s_inv u
-> it's not a problem to take the first line of a file, therefore as all elements (INTR: ONLY first THREE in the line) her are UNIQUE.


v y z
-> the following line has UNIQUE Structure in relation to the FIRST LINE: v=!x,y=!s_inv,z=!u


p r_inv s-> the next line has UNIQUE Structure in relation to the FIRST,SECOND LINE:
p=!v=!x, r_inv=!y=!s_inv, s=!z=!u

==> Result:mad:T=(x s_inv u v y z p r_inv s).


Your point:

1.v u_inv w 1 3 !(->0 97<- we are don't consider)
2.v w u_inv 1 3 !(->0 97<- we are don't consider) ...
...

================
first three lements:
v u_inv w ===> all elements are unique! ok => @T=(v, u_inv, w)

second three elements:
v=v ===> WE DO NOT CONSIDER THIS LINE (v w u_inv)!

w=!u_inv, it's ok! but i consider therse three elements only in
aggregate, that is all three elements are DISTINCT from FIRST!
u_inv=!w, ----&quot;-----

next line from file <RES>:

...
...
...


==============
$n=3;
scalar(@T)=9 (3 elements from three line).

!!
and also important : r=!r_inv, a=!a_inv, v_inv=!v....
!!

with best regards! Thank you!
Al.
 
This produces the desired output from your given data.
Code:
while (<RES>) {

    my %cnt = ();
    my @tmp = /^(\S+)\s+(\S+)\s+(\S+)/;   # Grab first three elements
    $cnt{$_}++ foreach @tmp, @T;   # Test for common elements between @T and @tmp
    push @T, @tmp unless grep { $_ > 1 } values %cnt;   # Add @tmp to @T if all are unique
}
jaa
 
Big thank's justice41's!


.... i check this one now.....Very nice ...part...
thanks!!!!!

See you later.
 
!justice41's,


1. HUGE thank!
2. All is correct!
3. the words at me are not present
4. large gratitude to you!
5. yours faithfully.

PS: this question is closed
AL.
 
justice41's,
------------

i have admitted a mistake...my mistake!!!

your code:

#########################################################
@T=();
open (FH4,&quot;<RESULT_SORT&quot;);
while (<FH4>) {

my %cnt = ();
my @tmp = /^(\S+)\s+(\S+)\s+(\S+)/; # Grab first three elements
$cnt{$_}++ foreach @tmp, @T; # Test for common elements between @T and @tmp
push @T, @tmp unless grep { $_ > 1 } values %cnt; # Add @tmp to @T if all are unique


}

print &quot; \@T received : @T\n\n&quot;;
close FH4;
#########################################################


out:
@T received : p_inv q t v q_inv w x r u_inv

Excuse me! But, i have admitted a mistake in of a task!
The result of a code does not approach for the reason that:

p_inv: p is used. And further in consideration schould not be considered in any kind!

q: --&quot;---, on condition described does not approach above!!!
(q==q_inv and also it, necessary to search for other elements in the file. As this not unique!).

...

...


!!!r==r_inv, a==a_inv, v_inv==v...!!!!
if we have &quot;r&quot; don't use &quot;r_inv&quot;, because is not UNIQUE!!!


What it is possible to change for reception of correct result in your CODE?

Sorry, and thank you very m. for HELLP, justice41's!
Al.




 
One way to do it would be to change this line
Code:
$cnt{$_}++ foreach @tmp, @T;
to this
Code:
$cnt{$_}++ foreach map { /^([a-z])(?:_inv)?$/ } @tmp, @T;
# or simply
$cnt{$_}++ foreach map { /^(.)/ } @tmp, @T;
This can be inefficient, especially if @T becomes large because you are stripping off the '_inv' of the same elements many times. If this becomes a problem you could use a second array to hold the unique first letters (without the '_inv's). Then you would only be stripping new elements each time through the loop instead of all of them.

jaa
 
Big thank's,justice41 !

$cnt{$_}++ foreach map { /^(.)/ } @tmp, @T;
In a result this variant is put as basic. While on several examples of problems has not arisen. All works correctly. All best for you!
...and than's for comments,
best regards,
Al.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top