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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

about array matching 1

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US


Hi! I am trying to compare 2 arrays and when there is match in the second array, wants to print the element, the one before and the one after the match.

when using only one matching element, it works, see below.
my @num = (1..20);

foreach $num(@num){
$count++;
if ($num == 2) {




print FILEW1 "$num[$count-2]\t$num\t$num[$count]\n";


}

}


but when i try to loop through all elements of the array1 to find matching elements in array2, it does not,



foreach $arr(@arr){
foreach $tmpvar(@tmpva){
$count++;

if ($arr eq $tmpvar){

print FILEW1 $tmpvar[$count-2]\t$tmpvar\t$tmpvar[$count]\n";
}
}
}

Thanks in advance
 
use array indexing to loop through the array you want to get a before and after match from:

Code:
for my $i (1..$#array-1) {
   print "$array[$i-1],$array[$i],$array[$i+1]";
}

note the looping starts at 1 and ends at $#array-1 because there is nothing before index 0 and nothing after index $#. Adjust accordingly if you still want to search index 0 and index $#.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Code:
foreach $arr(@arr){
  if($arr eq$tmpvar[0]){
    print FILEW1"\t$tmpvar[0]\t$tmpvar[1]\n";
  }
  for(my$count=1;$count<$#tmpvar;$count++){
    if($arr eq$tmpvar[$count]){ 
      print FILEW1"$tmpvar[$count-1]\t$tmpvar[$count]\t$tmpvar[$count+1]\n";
    }
  }
  if($arr eq$tmpvar[$#tmpvar]){
    print FILEW1"$tmpvar[$#tmpvar-1]\t$tmpvar[$#tmpvar]\t\n";
  }
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 

Kevin, I am not clear here, how do i use your suggestion to work with 2 arrays. Could you please show some more..

Thanks


 
Hi! Prex1/Kevin, thanks for posting the code. here is the sample data and the full script. I used Prex1 core, but still code returns errors. Please see if there is obvious error in my script.

input.txt
SCA_1.546604
SCA_1.608602


exp-file2.txt
SCA_1.271105
SCA_1.512793
SCA_1.546604
SCA_1.572546
SCA_1.604502
SCA_1.608602
SCA_1.612601
SCA_1.764328
SCA_1.895367
SCA_1.913347

expected output
SCA_1.512793
SCA_1.546604
SCA_1.572546

SCA_1.604502
SCA_1.608602
SCA_1.612601

my code after using your suggestion

#!/usr/bin/perl -w


my (@arr,@tmpv, @tmpva);
my ($arr, $tmpvar1, $tmpvar2, $tmpvar, $count);
open INPUT, "input.txt";
while (<INPUT>) {
chomp;
push(@arr, $_);
}

close INPUT;

open(FILEW1, ">out-file1.txt") || die "couldn't create the file\n";
open (LIST1, "exp-file2.txt") || die "File not found\n";
while (<LIST1>) {
chomp;
$tmpvar= $_;
push(@tmpva, $tmpvar);

}
close(LIST1);


foreach $arr(@arr){
if($arr eq$tmpvar[0]){
print FILEW1"\t$tmpvar[0]\t$tmpvar[1]\n";
}
for(my$count=1;$count<$#tmpvar;$count++){
if($arr eq$tmpvar[$count]){
print FILEW1"$tmpvar[$count-1]\t$tmpvar[$count]\t$tmpvar[$count+1]\n";
}
}
if($arr eq$tmpvar[$#tmpvar]){
print FILEW1"$tmpvar[$#tmpvar-1]\t$tmpvar[$#tmpvar]\t\n";
}
}

Thanks
 
What kind of error?
In your code above you call an array [tt]@tmpva[/tt], after it is referred to as [tt]$tmpvar[][/tt]: just decide which name to use.
And please next time put your code between [ignore]
Code:
...
[/ignore] tags: much easier to read and to check.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Prex1 my apologies and thanks for the advice. I modified the line as

push(@tmpvar, $tmpvar);

rest of the code us same as above. now there are no error messages but it prints only the below to the out file.

SCA_1.913347

 
Can't see why it shouldn't work.
To narrow down the problem you should add some debugging features to the print statements.
I would start by determining which one of the three print statements is printing that single value: just replace the tab characters with a star in the first one, a percent in the second and a plus in the third one (or any other recognizable char).
Also make sure, before going on, that your files do not contain spaces at the end of the lines.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 

prex1 thanks for the advice.

print FILEW1"%$tmpvar[$count-1]\t$tmpvar[$count]\t$tmpvar[$count+1]\n";

is the line that is printing that single value. I also checked , there are no spaces at the end of the line.

 
Still doesn't make sense to me. There must be some spelling mistake in your actual code.
Please check faq219-2889, put
[tt]use strict;[/tt]
at script top, add these two statements
[tt]print join('*',@arr),"\n";
print join('*',@tmpvar),"\n";[/tt]
before the [tt]foreach[/tt] and see if everything is as expected.
That single value should be printed together with two tab characters, that you don't see: you should replace them with printable chars as suggested above.
Come back with the exact printouts and put them with your code between [ignore]
Code:
[/ignore] tags (don't retype, just copy and paste).

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 

Prex1, i indeed had a typo in the code. Thanks a lot for straightening the error. For future reference, how do i use code tags. This is a plain text file and not a HTML code and i am using windows DOS.










 
See the link 'Process TGML' at bottom of the 'Your reply' window.
You simply type any text (specifically code, but not necessarily only that) between an opening [ignore]
Code:
[/ignore] tag (written as you read it) and a closing [ignore]
[/ignore] tag, and you'll see it formatted in equispaced characters in Preview Post and after submitting.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top