On 7-mrt-04, at 00:00, R. Joseph Newton wrote:
Bjorn Van Blanckenberg wrote:
On 3-mrt-04, at 09:56, R. Joseph Newton wrote:
I understand how the code works
It reads the file end split every line according to the tabs and then
sorts everything.
For returning the info it looks at colomn 5 (1-based indexing) and if
colomn 5 of the
next line is the different print an extra newline. So it basically does
what I want
if colomn 5 is exact the same not if it start with string from colomn 5.
So It is basically what I need but without reordering (sorting) and looking at every line that starts with colomn 5 and then sorts that blok of tekst.
I hope I explaned it well enough.
Thanks
Well, that is good, but it leaves you having to do a lot of work over again,
mostly because you handled things in a limp early on.
That bit of code early on:
my @sorted = map { $_->[0] } sort { $a->[5] cmp $b->[5] } map { [ $_ , (split /\s+/) ] } @fields;
does a sort, then re-concatenates the lines as sorted. Then they have to be
picked apart again in order to look for places to insert a newline.
What you want to do will require design, not imitation.
First, think about the requirements per line:
While the fifth token in the line is the same [which none are, since they all
have different decimals slopped onto the end], you wish to collect them in a
group.
Then you want to print out the items in the group line by line.
If you want groups of lines, why are you throwing them together into a garbage
bag of an array?
my %fifth_item_groups;
while (my $line = <INFILE>) {
chomp $line;
next unless $line;
my @tokens = split /\s+/, $line;
my $fifth_item = $tokens[4];
$fifth_item =~ s/\d+$//;
$fifth_item_groups{$fifth_item} = [] unless $fifth_item_groups{$fifth_item};
push @{$fifth_item_groups{$fifth_item}}, [EMAIL PROTECTED]; }
foreach my $grouping_key (sort {$a cmp $b} keys %fifth_item_groups) {
print join("\t", @{$_}), "\n" foreach @{$fifth_item_groups{$grouping_key}};
print "\n";
}
It looks like I have some problems with hashes.
I changed that last block with
foreach my $grouping_key (sort {$fifth_item_groups{$a} cmp $fifth_item_groups{$b}} keys %fifth_item_groups) {
print join("\t", @{$_}), "\n" foreach @{$fifth_item_groups{$grouping_key}};
print "\n";
}
and It works like a charm.
Thanks again joseph
The code above should perform the task specified--with no subsort. That work is
for you to do.
Hint: I chse my data structures pretty carefully, so that you should not have
much trouble accessing any element by which you might wish to do a subsort.
When you have made use of this and added the subsort, please post again and show
us what you come up with.
Joseph
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
-- Oooo. oooO ( ) ( ) ) / \ ( (_/ \_)
Met vriendelijke groeten
Bjorn Van Blanckenberg Technical Support Rechtstreeks nummer: ++32 3 780.68.41 -- PPC nv | Oostjachtpark 5 | 9100 St Niklaas Tel: ++32 3 777 04 44 | Fax: ++32 3 777 15 23 http://www.ppc.be
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>