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!

Uniquify a column based on values of another column 1

Status
Not open for further replies.

johnsbn

Technical User
Mar 23, 2010
12
0
0
US
Hello,

I'm a perl newbie and would appreciate your help here.

This is the file I have,

Arm -0.4 1.2 3.2
Bow 0.06 2.3 4.9
Arm 0.1 4.9 31.0
Pix -0.7 45.6 33.0
Gel 1.0 12.0 3.2
Arm 1.1 1.9 3.0

My columns of interest are 1 and 4. I want to uniquify column1 entries based on its largest column 4 value.

The new file should have the following,

Bow 0.06 2.3 4.9
Arm 0.1 4.9 31.0
Pix -0.7 45.6 33.0
Gel 1.0 12.0 3.2
 
This is what I've tried so far. Please help.


sub comp()
{
if ($field1 != 0)
{
while (<IN_FILE>)
{
chomp;
$next_line = $_;
($next_name,$next_field2,$next_field3,$next_field4) = split(/\s+/,$next_line);

if ($field1 eq $next_name)
{
if ($next_field4 > $field4)
{
$field4 = $next_field4;
$field2 = $next_field2;
$field3 = $next_field3;
}
}
print OUTFILE "$field1 $field2 $field3 $field4\n";
}

}

}

while (<IN_FILE>)
{
chomp;
@data = <IN_FILE>;
&comp;
($field1,$field2,$field3,$field4) = split(/\s+/,$data);

}
 
You have to open a file before you can read it.

You are reading the file in multiple places... remember that there is only one file handle, and the file is read sequentially, so you are going to get different parts of the file read in different sections of code. Just read the file once in one place, and store it in a hash.

Personally I would use the following algorithm:

Code:
while lines remain
    read a line
    split into fields
    if the last field is larger than those previously seen for this record type (or if it is the first record of this type)
        save the field value in a hash of the largest values indexed by record type
        save the entire line in a hash of the data indexed by record type
    end if
end while

print out the values of the data hash

Annihilannic.
 
I opened the file at the begining of the code. Sorry, I didn't post those lines here.

In your alogirthm,

if the last field is larger than those previously seen for this record type (or if it is the first record of this type)
save the field value in a hash of the largest values indexed by record type
save the entire line in a hash of the data indexed by record type


Can you give an example for this hash? I'm not proficient in using hashes, yet.

Thanks,
John
 
Code:
[gray]#!/usr/bin/perl -w[/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%largest[/blue][red];[/red]
[black][b]my[/b][/black] [blue]%data[/blue][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<>[red])[/red] [red]{[/red]
        [black][b]my[/b][/black] [blue]@fields[/blue]=[url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red];[/red]
        [olive][b]if[/b][/olive] [red]([/red]![url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url] [blue]$largest[/blue][red]{[/red][blue]$fields[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red] || [blue]$fields[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red] > [blue]$largest[/blue][red]{[/red][blue]$fields[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red][red])[/red] [red]{[/red]
                [blue]$largest[/blue][red]{[/red][blue]$fields[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red] = [blue]$fields[/blue][red][[/red][fuchsia]3[/fuchsia][red]][/red][red];[/red]
                [blue]$data[/blue][red]{[/red][blue]$fields[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red][red]}[/red] = [blue]$_[/blue][red];[/red]
        [red]}[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [url=http://perldoc.perl.org/functions/values.html][black][b]values[/b][/black][/url] [blue]%data[/blue][red];[/red]

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top