in reply to sorting difficulty

use strict; use warnings; my $str = q{<tag refid="5,8,7,3,2,1">}; sub sort_and_reformat { my $s = shift; return join " ", sort split m/,/, $s; } $str =~ s/"([^"]*)"/'"' . sort_and_reformat($1) . '"'/eg; print $str, $/;

The regex isn't very selective about what it considers an attribute, so you might want to tweak that.

Update: You can use look-ahead and -behind to get rid of that ugly substitution body:

$str =~ s/(?<=")([^"]*)(?=")/sort_and_reformat($1)/eg;

If you want to be more specific about what to match, you can use something like this:

$str =~ s/(?<=<tag refid=")([^"]*)(?=")/sort_and_reformat($1)/eg;

Replies are listed 'Best First'.
Re^2: sorting difficulty
by texuser74 (Monk) on May 07, 2008 at 10:22 UTC
    hi, thanks for your reply. $str =~ s/(?<=<tag refid=")([^"]*)(?=")/sort_and_reformat($1)/eg; is not removing commas. also, 1 more doubt, some times i might need to sort alphabets too, e.g. <tag refid="c,b,a">
      is not removing commas.

      Did you even try it? It works for me. If it really doesn't work for you: What version of perl are you using, and what's the result on your perl?

      If you want to sort both numbers and letters correctly, you should use Sort::Naturally (and replace sort with nsort).

        Thanks a lot, its working perfectly fine now. Thanks for your time and help.