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!

sorting array elements 1

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US
Hi!
I have 4 columns of tab-delimited data. column 1 has two patterns, 'UA' and 'DWN'. I want to select all the rows where coulmn 4 has a value above zero. From these, print rows where column 1 is 'DWN' and print all the 'UA' rows when there are 3 or more UA's between 'DWN'.

I am stuck here and any help is appreciated. No, this is not class work please.

Thanks in advance

sample.txt

UA 16X 936154 4.313130105
DWN 16X 936199 3.584936089
DWN 16X 936259 -4.087834994
DWN 16X 936309 0.930493862
DWN 16X 936374 4.212535002
DWN 16X 936429 3.327646506
DWN 16X 936474 -2.916253542
UA 16X 936649 3.171373127
UA 16X 936754 2.445098511
UA 16X 936809 4.638831704
UA 16X 936864 3.212792628
UA 16X 936914 3.403498281
UA 16X 940084 -3.683286264
UA 16X 940134 3.949742106
DWN 16X 940244 5.031138006
DWN 16X 940309 3.814187618
UA 16X 940364 2.589227288
UA 16X 940409 3.427800184
DWN 16X 940909 3.582933058
DWN 16X 940959 3.493582633
UA 16X 941014 3.664841581
UA 16X 941124 3.321131006
UA 16X 941184 3.784367884

outfile.txt [what i expect]

DWN 16X 936199 3.584936089
DWN 16X 936309 0.930493862
DWN 16X 936374 4.212535002
DWN 16X 936429 3.327646506
UA 16X 936649 3.171373127
UA 16X 936754 2.445098511
UA 16X 936809 4.638831704
UA 16X 936864 3.212792628
UA 16X 936914 3.403498281
UA 16X 940134 3.949742106
DWN 16X 940244 5.031138006
DWN 16X 940309 3.814187618
DWN 16X 940909 3.582933058
DWN 16X 940959 3.493582633
UA 16X 941014 3.664841581
UA 16X 941124 3.321131006
UA 16X 941184 3.784367884

here is my code, which runs, but does not capture all the rows with 'UA' with a positive value.

#!/usr/bin/perl -w
use strict;
use diagnostics;

open(FILER1, "sample.txt") || die "couldn't open the file\n";
open(FILEW1, ">outfile.txt") || die "couldn't create the file\n";
my ($line, $all, $count, $full, $flag, $num);
my (@all);

while(<FILER1>){
$line=<FILER1>;

split /\t/, $line;

if($_[3] >= 0) {

if($line=~/DWN/) {

print FILEW1 "$line";
$flag =1;
}


else {

push(@all, $line);
$count= scalar @all;
$full = join '', @all;

if ($count >=3 && $flag ==1) {
$flag =0;
print FILEW1 "$full";
$count=0;
$full = '';
@all = '';
$all='';

}


}

}

}


 
This should do what you're looking for. You might want to test it with some more data just to be sure.
Code:
my @lines;
while (<FILER1>) {
	chomp(my @temp = split /\s+/, $_);
	push(@lines, \@temp) if $temp[3] >= 0;
}
my $ua_count;
select FILEW1;

for (my $i = 0; $i <= $#lines; $i++) {
	if ($lines[$i][0] eq 'DWN') {
		if ($ua_count >= 3) {
			foreach my $j ($i-$ua_count .. $i-1) {
				print join("\t", @{$lines[$j]}), "\n";
			}
		}
		print join("\t", @{$lines[$i]}), "\n";
		$ua_count = 0;
	} elsif ($lines[$i][0] eq 'UA') {	
		$ua_count++;
		if ($i == $#lines && $ua_count >= 3) {
			foreach my $j ($i-$ua_count+1 .. $i) {
				print join("\t", @{$lines[$j]}), "\n";
			}
		}
	}
}
select STDOUT;
 
rharsh, thanks for the solution. It does work as long as the end of the file has 'DWN'.

That will do for me.





 
Huh, that's surprising. When I used the sample input you provided (that ends with a UA record) it spits out everything you listed in your sample output.

Can you post some input data that doesn't generate the correct output?
 

My bad, there was an additional space in the actual file. Your script rocks. Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top