Creating loop for a script -Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating loop for a script -Perl
# 1  
Old 05-09-2009
Creating loop for a script -Perl

Hi Guyz
I designed a script that can compare 2 columns(values) of single file and gives the closest numbers to the first column by comparing the numbers in first column with second and it works in a single file.
Quote:

INPUT OUTPUT
Singlefile Singlefile
1stC 2ndC 1stC 2ndC
100 512 100 90
200 612 200 245
300 90 300 309
400 309 400 400
500 245 500 512
600 400 600 612
Now I'm trying to design a new script with 2 objectives for 2 files (not a single file like previous one). This new file contains a new column in both files containing A1-A22 and A-X and A-Y symbols randomly (the order of symbols in both files are may be different or may be not).

Objective1. Make a loop that can coonect the same symbols of 2 files and runs the script for closest numbers in those values. for Example
A-X in INPUT1 1st file has 100 - The script has to search every A-X in INPUT2 2nd File and take the closest of those values i.e A-X90

Quote:

INPUT1
1st File
1stC 2ndC
A1 100
A1 200
A-X 300
A-20 400
A-2 500
A-10 600


INPUT2
2nd File
1stC 2ndC
A-X 512
A11 612
A-X 90
A-20 309
A-21 245
A-7 400


OUTPUT
Singlefile
1stC 2ndC
A-X 90
A-20 309
Objective 2. Print the results to a single file OUTPUT

I'm here by including my script and examples of input and output files to make it easy for you to understand.
Thanx in advance and your valuable time.
Hope I'll get clue.

Last edited by repinementer; 05-09-2009 at 01:49 AM..
# 2  
Old 05-09-2009
Script for Closest numbers in a single file

Script may be useful for further step

Quote:
#!/usr/bin/perl
use strict;

open FH,"<file.txt" or die "Can not open file";

my (@a1,@a2,$gap,$key);
while(<FH>) {
chomp;
my @tmp=split("\t",$_);

if ($tmp[0]>=1) { # only if there is a number greater than 0 present in the first column
push @a1,$tmp[0];
}
if ($tmp[1]>=1) { # only if there is a number greater than 0 present in the second column
push @a2,$tmp[1];
}
}
close FH;

for(my $i=0;$i<=$#a1;$i++) {
$gap=1000000000000;
$key=0;
for(my $j=0;$j<=$#a2;$j++) {
my $tmp=abs($a2[$j]-$a1[$i]);
if ($tmp<=$gap) {
$gap=$tmp;
$key=$a2[$j];
}
}
print $a1[$i],"\t",$key,"\n";
}
# 3  
Old 05-09-2009
Quote:
Originally Posted by repinementer
...
Objective1. Make a loop that can coonect the same symbols of 2 files and runs the script for closest numbers in those values. for Example
A-X in INPUT1 1st file has 100 - The script has to search every A-X in INPUT2 2nd File and take the closest of those values i.e A-X90
...
Your perl program is way too convoluted. What you could do here is to sort both files on their keys and use the "join" built-in to retrieve just the rows that are common in both. Then loop through this data and use an associative array/hash/<whatever> to keep track of the minimum diff between 2nd and 3rd fields for the same key.

Code:
$ 
$ cat ip1
A1 100   
A1 200   
A-X 100  
A-20 400 
A-2 500  
A-10 600 
$        
$ cat ip2
A-X 512  
A11 612  
A-X 90   
A-20 309 
A-21 245 
A-7 400  
A-2 501  
A-2 490  
$        
$ sort -k1,1 ip1 > ip1_sorted; sort -k1,1 ip2 > ip2_sorted
$
$ cat ip1_sorted
A1 100
A1 200
A-10 600
A-2 500
A-20 400
A-X 100
$
$ cat ip2_sorted
A11 612
A-2 490
A-2 501
A-20 309
A-21 245
A-7 400
A-X 512
A-X 90
$
$ join ip1_sorted ip2_sorted |
> awk '{
>   x = $2>=$3 ? $2 - $3 : $3 - $2
>   if (mindiff[$1] == "") {mindiff[$1] = $1":"$2":"$3":"x}
>   else {split(mindiff[$1],arr,":"); diff = arr[4]
>         if (x < diff) {mindiff[$1] = $1":"$2":"$3":"x}
>   }}
> END {
>   for (i in mindiff){split(mindiff[i],arr,":"); print arr[1]" "arr[2]" "arr[3]}
> }'
A-X 100 90
A-20 400 309
A-2 500 501
$
$

Hope that helps,
tyler_durden

________________________________________________________________________
"With a gun barrel between your teeth, you speak only in vowels."
# 4  
Old 05-09-2009
Awesome Awk

Last edited by stateperl; 05-09-2009 at 12:16 PM..
# 5  
Old 05-09-2009
Hi

Hi
there is a problem
If the first column contain A1 more than 1 time??(There should be all the values in column 1)
Input1
A1tab 20
A1tab30
A1tab50
A2tab50
A2tab90
A3tab80

INPUT2
A1tab30
A2tab45
A4tab100

OUTPUT
A1tab30
A1tab30
A1tab30
A2tab45
A2tab45
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL script loop problem

I have written the below PERL script to reprocess messages from a failure queue. It basically browses all the messages in the failure queue to individual files in a directory and then scans those files to determine the originating queue. The script will then move each message in turn from the... (0 Replies)
Discussion started by: chris01010
0 Replies

2. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

4. Shell Programming and Scripting

Splitting a file and creating new files using Perl script

Hi All, I am new to Scripting language. I want to split a file and create several subfiles using Perl script. Example : File format : Sourcename ID Date Nbr SU IMYFDJ 9/17/2012 5552159976555 SU BWZMIG 9/14/2012 1952257857887 AR PEHQDF 11/26/2012 ... (13 Replies)
Discussion started by: Deepak9870
13 Replies

5. Shell Programming and Scripting

Need help with perl script with a while loop reading file

Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is... (1 Reply)
Discussion started by: brianjb
1 Replies

6. Shell Programming and Scripting

Creating variable by for loop

i have a file 'detail' which contains cat detail 111111 222222 333333 444444 but detail may be 4 line file.6 line file or 8 line file like cat detail 111111 222222 333333 444444 555555 666666 777777 888888 so i want a declare a loop which assign the value of first line in one... (11 Replies)
Discussion started by: rakeshtomar82
11 Replies

7. Shell Programming and Scripting

Creating a loop in csh

I have the following code and want to use a loop to output the results to the fparams file. if ($optparams == 1) then # Set the tdarwin parameters set txt01 = "Call to raytrac.csh" set txt02 = "" set txt03 = "./Scripts/raytrac.csh $*" set txt04 = "" set txt05 =... (0 Replies)
Discussion started by: kristinu
0 Replies

8. Shell Programming and Scripting

while loop in perl script

#! /usr/bin/perl $exp = "y"; while ($exp !="n") { system "clear"; # clear the window print "\nEnter the number of Widgets ordered: "; $widgetcount = <STDIN>; #vairable to save the number of total widgerts ordered chop $widgetcount; print "\nEnter the number of Gidgets ordered:... (2 Replies)
Discussion started by: navjot99cheema
2 Replies

9. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

10. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies
Login or Register to Ask a Question