in reply to Re: Regexp for alphabetical order match within the string
in thread Regexp for alphabetical order match within the string

You can avoid the sort for an O(N) solution:

sub is_alphabetical { my @c = split //, shift; ord($c[$_]) >= ord($c[$_ - 1]) or return 0 for 1 .. $#c; return 1; }

Update: Yes, my use of ord() is unnecessary. Thanks tlhf++.

Edit: Added a missing my.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Re: Regexp for alphabetical order match within the string
by tlhf (Scribe) on Oct 30, 2003 at 19:45 UTC
    Elegent solution, tho the ords are needless.
    sub is_alphabetical { my @c = split //, shift; $c[$_] ge $c[$_-1] or return 0 for 1..$#c; return 1; }

    tlhf
    (Everyone forgets about ge and le ^_^)

Re: Re: Re: Regexp for alphabetical order match within the string
by pg (Canon) on Oct 30, 2003 at 20:37 UTC

    ++, but the original post obviously allows non-alphabetical chars in the string, and their orders are not considered. A small improvement to meet the original requirement:

    $str = "aBcD12ef7812g"; print is_alphabetical($str); sub is_alphabetical { (my $str = lc(shift)) =~ s/([^a-z])//g; my @c = split //, $str; $c[$_] ge $c[$_ - 1] or return 0 for 1 .. $#c; return 1; }
      Re: s/([^a-z])//g:
      Please, tr/a-z//dc is feeling unloved.
      but the original post obviously allows non-alphabetical chars in the string, and their orders are not considered.

      Actually, the regular expression in the original post did not match any non-alphabetic or even uppercase characters. (Okay, as the original wasn't anchored, I guess it did permit such characters in the string, but then it didn't deal with more than a single group of characters. And beyond that it matched the empty string.)

      I intentionally neglected those questions as I didn't feel they were really specified.

      -sauoq
      "My two cents aren't worth a dime.";