thanks for help owen and Randy,
Sorry it took so lang for responding and thanking you but I was on a long vacation


I just want to say that I have made a mistake in the question,
It doesn't have to sorted by item 5 but needs to look for everything that contains item 5
and than be sorted by item 2


--
                      Oooo.
         oooO    (     )
         (     )       )   /
          \   (       (_/
            \_)

Thanks

Bjorn Van Blanckenberg

On 23-jan-04, at 11:46, Owen wrote:

On Fri, 23 Jan 2004 09:36:00 +0100
Bjorn Van Blanckenberg <[EMAIL PROTECTED]> wrote:

let say that the file contains these items (every item is seperated
with a tab)

one  title   state   name   code1   number
two  title2   state2   name2   code2   number2
one  title3   state3   name3   code3   number3
four  title4   state4   name4   code4   number4
six  title5   state5   name5   code1   number5
dip  title6   state6   name6   code1   number6
fun  title7   state7   name7   code2   number7

the thing i'am looking for is that it is sorted by item 5 and writes
back to the file
with an extra line if item 5 is different

so I would come up with:

one  title   state   name   code1   number
six  title5   state5   name5   code1   number5
dip  title6   state6   name6   code1   number6

two  title2   state2   name2   code2   number2
fun  title7   state7   name7   code2   number7

one title3 state3 name3 code3 number3

four title4 state4 name4 code4 number4


Well you can try;
-------------------------------------------------------------



#!/usr/bin/perl -w

chomp(@fields = <DATA>); # slurp in the file
$lastbit=1;

 @sorted =
 map { $_->[0] }
 sort { $a->[5] cmp $b->[5] }
 map { [ $_ , (split /\t/) ] } @fields;#tab separated fields

foreach (@sorted){
@bits = split;
print "\n" if ($bits[4] ne $lastbit);
print "$_\n";
$lastbit=$bits[4];
}

__DATA__
one     title   state   name    code1   10
two     title2  state2  name2   code2   21
one     title3  state3  name3   code3   13
four    title4  state4  name4   code4   14
six     title5  state5  name5   code1   number5
dip     title6  state6  name6   code1   number6
fun     title7  state7  name7   code2   number7

----------------------------------------------
and it produces


21:42:56 [~/perltest]#perl sortdata1.pl


one     title   state   name    code1   10
six     title5  state5  name5   code1   number5
dip     title6  state6  name6   code1   number6

two     title2  state2  name2   code2   21
fun     title7  state7  name7   code2   number7

one title3 state3 name3 code3 13

four title4 state4 name4 code4 14



 --
Owen

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to