in reply to Re: Sorting Issue
in thread Sorting Issue

The only part missing is that the value of $status comes from a data base for specific record as P|F|C|D|I|A|N|R|S|X and after that I am using the reg. exp. to translate into a more specific message. I am just trying to display the values sorted.

Replies are listed 'Best First'.
Re^3: Sorting Issue
by polettix (Vicar) on Jul 26, 2005 at 14:20 UTC
    All code below is untested

    Update: suggestion about splitting is valid only if the result from database is "|"-separated, as cleverly pointed out here...

    You had to say it immediately:

    my @strings = split /\|/, $status;
    Note that, by default, putting @sorted_strings in double quotes will give you space-separated items. You can change the separator using the perlvar $":
    { local $" = "|"; print "<br><font color=red>@sorted_strings</font><br>"; }
    I put the curly brackets in order to restrict the modification of the $" variable, making this modification "visible" to the print statement only.

    Anyway, I'd probably use a more flexible, metadata-based approach:

    my %subst ( P => 'Posted', F => 'Posted', C => 'Posted', D => 'Posted', I => 'Posted', A => 'Accepted', N => 'New', R => 'Rejected', S => 'Save', X => 'Canceled', ); my @sorted_strings = sort map { $subst{$_} } split /\|/, $status; # The above line can be roughly expanded to the three lines below: # # my @status_letters = split /\|/, $status; # my @strings; # foreach (@status_letters) { # push @strings, $subst{$_} # } # my @sorted_strings = sort @strings;

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
      Nothing works but thanks...
        You're the welcome, but you could post some more code and a real-world example of what ends up inside $status. I think that many of us got fooled into thinking that $status might contain the whole value "P|F|C|D|I|A|N|R|S|X", pipe characters included. Please post an example of an actual value and how you get it from the database.

        Moreover, you're maybe doing some iteration over the results from the database. If so, please post the loop as well. As a rule of thumb, post anything that may help us understanding where you come from, how you munge your data and where you want to go. Cutting out all that's not necessary to the goal :)

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Don't fool yourself.
Re^3: Sorting Issue
by blazar (Canon) on Jul 26, 2005 at 14:01 UTC
    So far so fine. Care to read the ramining part of my post?

    Turning the glass sphere mode one I guess you may be after something like:

    my @strings; while ( whatever ) { # ... push @strings, $status; } my @sorted=sort @strings;
Re^3: Sorting Issue
by Iron (Scribe) on Jul 26, 2005 at 14:02 UTC
    $status=~s/P|F|C|D|I/Posted/g; $status=~s/A/Accepted/g; $status=~s/N/New/g; $status=~s/R/Rejected/g; $status=~s/S/Save/g; $status=~s/X/Canceled/g; my @strings = split /\|/, $status; my @sorted_strings = sort @strings; print "<br><font color=red>@sorted_strings</font><br>";

    Update: If there is for each record status is only one letter, then I misunderstood string 'P|F|C|D|I|A|N|R|S|X' and '|' stands for 'or'. In such case see blazar's post.
      Update: If there is for each record status is only one letter, then I misunderstood string 'P|F|C|D|I|A|N|R|S|X' and '|' stands for 'or'. In such case see blazar's post.
      And if so the example code I provided there could be further simplified: no substituion at all should be necessary. Just hash lookup.
Re^3: Sorting Issue
by Anonymous Monk on Jul 26, 2005 at 14:00 UTC
    while ($pointer = $sth->fetchrow_hashref) { ....code.... $status=~s/P|F|C|D|I/Posted/g; $status=~s/A/Accepted/g; $status=~s/N/New/g; $status=~s/R/Rejected/g; $status=~s/S/Save/g; $status=~s/X/Canceled/g; my @strings = $status; my @sorted_strings = sort @strings; print "<br><font color=red> @sorted_strings</font><br>"; }