Visit The New, Modern Unix Linux Community

loop through two files based on a variable

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop through two files based on a variable
# 8  
No to what?

I am still pretty confused, but is this what you are trying to do?

use strict;
use warnings;

my @set = (15, 150, 200, 250);
my @vals = (208, 258, 56, 123);

my @vals_sorted = sort {$a <=> $b} @vals;
foreach my $n (@set) {
   print "Closest to $n = ", shift @vals_sorted, "\n";

# 9  
Originally Posted by nogu0001
ya u r right but i need the way to claculate it
I don't understand what you mean. Are you prohibited from using sort?
# 10  
Also the columns already appear to be sorted so the next line is the closest one, no?
# 11  
hi kevin
the logic is perfect
but instead of writing the values in arrays i would like to assign the no.of values from column1 and column2 i.e defing @set as values from column 1 and @val as column 2.
input file;;
chr1 100 112
chr1 150 300
chr1 80 400

script needs correction

#!usr/bin/perl -w
use strict;
use warnings;
my $infile1 = 're5.txt';
open IN10, "< $infile1" or die "Can't open $infile1 : $!";
my %values;
while (<F1>) {
my ($chrom,$value1,$value2) = split /\t/;
my $rec = {value1 => $value1, value2 => $value2
push @{$vlaues{$chrom}}, $rec;
my @set = $value1;
my @vals = $value2;
my @vals_sorted = sort {$a <=> $b} @vals;
foreach my $n (@set) {
print "Closest to $n = ", shift @vals_sorted, "\n";

Thanx for the ideas
Funny laughs @other mails

Last edited by nogu0001; 02-24-2009 at 04:28 AM..
# 12  
Do u have time to check my original long script

if u donmind i will post to u
# 13  
$infile1 = 'file.txt';
$infile2 = 'cpg2.txt';
$outfile7 = 'out10.txt';
open IN10, "< $infile1" or die "Can't open $infile1 : $!";
open IN11, "< $infile2" or die "Can't open $infile2 : $!";
open OUT7, "> $outfile7" or die "Can't open $outfile7 : $!";

my %chromes;
my %chromes1;
while (<IN10>) {
my ($arrayid,$ncrnaid1,$ncrnaid2,$ncrnaid3,$ncrnaid4,$ncrnaid5,$ncrnaid6,$ncrnaid7,$ncrnaid8,$ncrnaid9, $ncrnaid10,$ncrnaid11,$ncrnaid12, $chrom,$start,$end,$cstrand,$en,$esi,$est) = split /\t/;
my $rec = {arrayid => $arrayid,
ncrnaid1 => $ncrnaid1,
ncrnaid2 => $ncrnaid2,
ncrnaid3 => $ncrnaid3,
ncrnaid4 => $ncrnaid4,
ncrnaid5 => $ncrnaid5,
ncrnaid6 => $ncrnaid6,
ncrnaid7 => $ncrnaid7,
ncrnaid8 => $ncrnaid8,
ncrnaid9 => $ncrnaid9,
ncrnaid10 => $ncrnaid10,
ncrnaid11 => $ncrnaid11,
ncrnaid12 => $ncrnaid12,
start => $start,
end => $end,
cstrand => $cstrand,
en => $en,
esi => $esi,
est => $est};
push @{$chromes{$chrom}}, $rec;
my @arrayids;
sub input {
my @attrs =qw(chrom start);
while (<IN10>) {
my %rec;
@rec{@attrs} = split /\t/;
push @arrayids,\%rec;
foreach my $chrom (sort keys %chromes){
my $count = scalar @{$chromes{$chrom}};
print OUT7 "$chrom\t$count\t\n\n";
print OUT7 map {"\t\t$_->{start}\t\n"} @{$chromes{$chrom}};


while (<IN11>) {
my ($cchrom,$middle) = split /\t/;
my $cpg = {middle => $middle};
push @{$chromes1{$cchrom}}, $cpg;
my @cpgids;
sub input {
my @cpgs =qw(cchrom middle);
while (<IN11>) {
my %cpg;
@cpg{@cpgs} = split /\t/;
push @cpgids,\%cpg;
foreach my $cchrom (sort keys %chromes1){
my $count = scalar @{$chromes1{$cchrom}};
print OUT7 "$cchrom\t$count\t\n\n";
print OUT7 map {"\t\t$_->{middle}\t\n"} @{$chromes1{$cchrom}};
#my @set = $start; #callin $start from $start (file1)
#my @vals = $middle; #calling $middle from $middle (file2)
#my @vals_sorted = sort {$a <=> $b} @vals;
#foreach my $n (@set) {
# print "Closest to $n = ", shift @vals_sorted, "\n";

close IN10;
close IN11;
close OUT7;

Last edited by nogu0001; 02-24-2009 at 02:23 AM..
# 14  
sorry for posting early

I caught with some group meeting crap.
well here I'm trying to recall the column1 ($start) from file1, IN10 and column2, ($middle) from file2, IN11. I think I screwed up some where.Smilie

note :I'm giving sample outputs of script so far except calculation of the closest point

Last edited by nogu0001; 02-24-2009 at 02:13 AM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #507
Difficulty: Easy
In the following code: x = 3y + 7; the variables x and y represent numbers that can take on a number of different values.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print based on common variable value.

Hi All, i have below two files. FILE: NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="" MOUNTPOINT="" NAME="/dev/sda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" PKNAME="/dev/sda" MOUNTPOINT="/boot" NAME="/dev/sda2"... (3 Replies)
Discussion started by: balu1234
3 Replies

2. Shell Programming and Scripting

Naming output files based on variable parameters and input filenames

Hello, I have a series of files in sub-directories that I want to loop through, process and name according to the input filename and the various parameters I'm using to process the files. I have a number of each, for example file names like AG005574, AG004788, AG003854 and parameter values like... (2 Replies)
Discussion started by: bdeads
2 Replies

3. Shell Programming and Scripting

Calling a Variable based on a Variable

Hi all, I have a source config file with variables like so: eth1_ip= eth2_ip= eth3_ip= I am trying to run a script which loops based on the number of eth interfaces on a machine and therefore modifies the variable it calls in the environment based on the... (5 Replies)
Discussion started by: landossa
5 Replies

4. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

5. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

6. Shell Programming and Scripting

printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out. For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do here i want to output the value of... (4 Replies)
Discussion started by: oly_r
4 Replies

7. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

8. Shell Programming and Scripting

Using awk to create files based on a variable name

Hey all, I am parsing a file which have records containing one of a number of files names: ".psd", ".cr2", ".crw" , ".cr", ".xi", ".jpg", ".xif" etc Somewhere on each line there is a value "Namex.psd" "Namex.crw" etc. The position of this name is highly variable I need to output all the ".psd"... (4 Replies)
Discussion started by: C0ppert0p
4 Replies

9. Shell Programming and Scripting

create variable name based on another variable's value

Hello, I am needing to create a variable and assign it a value based on the value of a previosly defined variable... I am using KSH.. Example: VAR1=COMPUTER1 I need another variable like ${VAR1}_FLAG="Y", so it would actually be COMPUTER1_FLAG="Y". I will be looping through many values in... (2 Replies)
Discussion started by: benefactr
2 Replies

Featured Tech Videos