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";
}

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>


Reply via email to