in reply to Re^2: Sorting Issue
in thread Sorting Issue

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.

Replies are listed 'Best First'.
Re^4: Sorting Issue
by Anonymous Monk on Jul 26, 2005 at 16:49 UTC
    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.
        Hi, There isn't nothing more to it, the variable $status get a letter value that can be one of the letter on the code and the regular expression just do the substitution based on the value of the letter found: like if $status equal letter P|F|C|D|I it should print "Posted" and if $status equal letter A it should print (Display) "Accepted" and so on like on the code I submitted.