Ask a Question related to PERL Beginners, Design and Development.
-
Bjorn Van Blanckenberg #1
Reading tab delimited File & sort everything according item 5 of every line
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
Is there someone that can help me
--
Oooo.
oooO ( )
( ) ) /
\ ( (_/
\_)
thanks
Bjorn Van Blanckenberg
Bjorn Van Blanckenberg Guest
-
Reading Last Line in a Txt File
Hi, I'm trying to read the last line in a file using "ASFileRead" function. Im having trouble specifying the last parameter which counts the... -
Reading a Comma Delimited File
Hello all, So as the title says, I need to read a comma delimited file generated by MS Excel. I know that by doing GetListAt() or ListLen()... -
Parsing pipe delimited file
Hello everyone, Thanks to everyone who helped with my last problem last week. I've hit a snag in another problem this week. I need to parse... -
Reading a line at a time from a file
Hey, Just wondering what the best way to do this is? What i specifically want to do is check if a user exists on a unix machine. The best way... -
Reading a scalar line by line
Hi, does anyone know how I would do this? something like: while ($string) print _$ } Google searches did not turn up anything. -
Randy W. Sims #2
Re: Reading tab delimited File & sort everything according item 5 of every line
On 01/23/04 03:36, Bjorn Van Blanckenberg wrote:
A variation on the Swartzian transform: (read from bottom up)> 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
#!/usr/bin/perl
use strict;
use warnings;
print map { $_->[1] . "\n" }
sort {$a->[0] cmp $b->[0]}
map { chomp; [(split /\t/, $_)[4], $_] }
<DATA>;
__DATA__
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
Randy W. Sims Guest
-
Owen #3
Re: Reading tab delimited File & sort everything according item 5 of every line
On Fri, 23 Jan 2004 09:36:00 +0100
Bjorn Van Blanckenberg <bjornvb@ppc.be> 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
Owen Guest



Reply With Quote

