in reply to How to select specific lines from a file

I'd use a hash of arrays (see perldsc - Perl Data Structures Cookbook). The chain will be the key, there will be two values in each array: the Z-coordinate and the position. Then just iterate the input and test whether the new line should go to the hash or not:
my %hash; while (<>) { my ($chain, $pos, $zcoor) = (split)[4, 5, 8]; $hash{$chain} = [$pos, $zcoor] if not exists $hash{$chain} or $pos < $hash{$chain}[0]; }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: How to select specific lines from a file
by Anonymous Monk on Apr 29, 2014 at 17:41 UTC
    Great, it seems to do the trick... Is it correct that I get all positions in the HoA that you create?
    while(<>) %HoA=(); next unless /^ATOM\b/; @split_atom_line=split(/\s+/, $_); $chain=$split_atom_line[4]; $position=$split_atom_line[5]; $Zcoordinate=$split_atom_line[8]; if (not exists $HoA{$chain} or $position < $HoA{$chain}[0]) { $HoA{$chain} = [$position, $Zcoordinate]; } for $key ( keys %HoA ) { print "$key: $HoA{$key}[0]\n"; } }
      I mean, it keeps adding data although the position and the chain have already been inserted... Shouldn't the HoA contain only two lines in this example, i.e. chains A and B and 1 position for each (the smallest)?
        Damn! I had the HoA declaration inside the while loop... Works smoothly now, thanks a lot!