in reply to Sorting Issue

I'm having a hard time trying to make sense of what you may mean. Your code plainly won't work and IMHO it doesn't shed any light on what your real aim may be.

here you have a variable to which you apply a series of substitutions, (incidentally it is customary to write

for ($status) { s/P|F|C|D|I/Posted/g; s/A/Accepted/g; s/N/New/g; s/R/Rejected/g; s/S/Save/g; s/X/Canceled/g; }
when you have a long series of of substitutions - much cleaner IMHO).

Update: contrary to what I wrote above, in this particular case I'd probably do something along the lines of (untested):

my %subst={ A => 'Accepted', N => 'New', R => 'Rejected', S => 'Save', X => 'Canceled' }; $subst{$_}='Posted' for qw/P F C D I/; $status =~ s/[A-Z]/$subst{$&}||$&/ge;

Note to the update: I am aware of the gotchas about $& & C. My gut feeling is that people overestimate them. Personally I'm not a fanatic of its use, but happen to resort to it every now and again...

Then you create an array containing only (the value of) that variable and sort it, which is an immaterial operation, since it has only one entry in any case.

Replies are listed 'Best First'.
Re^2: Sorting Issue
by Anonymous Monk on Jul 26, 2005 at 13:57 UTC
    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.
      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...
      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;
      $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.
      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>"; }