LiTinOveWeedle has asked for the wisdom of the Perl Monks concerning the following question:

I hope with Schwartzian Transform...

Hi brothers,
I need sort @array in which is each element line of csv file - so values are separated by delimiter.like value1;value2;value3;value4 etc. And I need sort @array by certain valueX. But before comparing with <=> I must convert values to be numeric only. I know (it's given) that in value2 are only (in whole @array) number in this format xx.xxx.xxx,xx so I must use regex to remove points and replace comma by point. (but globaly I have many types of data fields, so script must be able to determine which regex use depending of given data type) But after sorting I want get as results not values changed by regex but sorted original values.....

I tested sort with mapbut I fail to get this to work :-(

ply if you know solution get me know..... THX to all

Li Tin O've Weedle
mad Tsort's philosopher

Replies are listed 'Best First'.
Re: sorting with Schwartzian Transform
by no_slogan (Deacon) on May 08, 2001 at 03:08 UTC
    How about
    @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, (split /[,.]/, $_)[$idx] ] } @array;
    Where $idx is the column number you care about. If @array contains newlines, that might spew warnings... try
    map { chomp(my $key = (split /[,.]/, $_)[$idx]); [ $_, $key ] } @array;
Re: sorting with Schwartzian Transform
by LiTinOveWeedle (Scribe) on May 08, 2001 at 03:15 UTC
    To request I try to specify more data - as sample.
    each element in array is in fact another array so there should be sometnig like:
    name;password;phonenumber;price

    with real datas something like this:
    LTOW;mypass;+420-2-312456;1.125.369,25

    I know (it is somewhere specify) that in phonenumber are only phonenumber in format like this one. When I want sort element in @array by phone number I must first convert number into number only format - remove + and - marks. Than I will be able to use <=> operator. But results should be @array with original phone numbers. Price field is the same problem - at first I must convert into numeric only and then I can sort.....

    Li Tin O've Weedle
    mad Tsort's philosopher

      Any particular reason why telephone numbers need to be compared as numbers and not as strings? If you need to make more transformations, just keep chewing on $key in the map statement.
      map { my $key = (split /,/, $_)[$idx]; chomp $key; $key =~ s/[-+]//g; # and so on... [ $_, $key ] } @array;
        Yeah no good example, prices is better.
        Many THX fot your answer....

        Li Tin O've Weedle
        mad Tsort's philosopher