in reply to Re: Text Manipulation Quickie
in thread Text Manipulation Quickie

Sorry about the abruptness of the main question. I seemed to have forgotten my manners.
What I am trying to do is sort results from a database into a predefined format. The results always come out sorted from the database and I want to rearrange them.
And a little add on to that question
What do I need to do if I have two (or more) columns seperated by semi-colons but I only want to sort by the first one?
These maybe simple problems but this is my first attempt at using perl to manipulate text! And I have a looming deadline. So be gentle.
2;3425; 2;4534; 2;2155; 3;1324; 3;1253; 3;3455; 4;3454; 4;3464; 4;3454;

Required Result

2;3425; 3;1324; 4;3454; 2;4534; 3;1253; 4;3464; 2;2155; 3;3455; 4;3454;

Thanks for all your help and please excuse those of us brought up with no manners :-P

j o h n i r l .

Sum day soon I'Il lern how 2 spelI (nad tYpe)

Replies are listed 'Best First'.
Re: Re: Re: Text Manipulation Quickie
by tommyw (Hermit) on Aug 15, 2002 at 13:21 UTC

    But you're still not defining the question in any manner that we can accurately extrapolate what you're trying to do. Especially when you use the word sort. We define sort to put all occurances of 2 before occurances of 3, before 4. So what you're asking us to do is unsort the data. There are an infinite number of ways of doing so, which result in arbitrary orderings (although the are only n! such orderings).

    Most of the solutions suggested simply counted the number of 2s, 3s and 4s (etc), and then repeatedly printed one of each (in ascending order) as long as necessary: they were actually discarding the original data.

    What it seems like you're after is

    foreach (<DATA>) { ($key, $val)=split ';', $_, 2; push @{$store{$key}}, $val; } while (keys %store) { foreach $key (sort keys %store) { print join ';', $key, shift @{$store{$key}}; delete $store{$key} unless scalar @{$store{$key}}; } } __DATA__ 2;3425; 2;4534; 2;2155; 3;1324; 3;1253; 3;3455; 4;3454; 4;3464; 4;3454;
    which is, essentially, just a variation of that theme.

    The answer to your 'little add' is to use split to extract the individual values

    --
    Tommy
    Too stupid to live.
    Too stubborn to die.

Re: Text Manipulation Quickie
by Abigail-II (Bishop) on Aug 15, 2002 at 13:15 UTC
    my @sorted = do {no warnings 'numeric'; sort {$a <=> $b} @array};
    Abigail