Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Regexp for alphabetical order match within the string

by hardburn (Abbot)
on Oct 30, 2003 at 18:56 UTC ( [id://303357]=note: print w/replies, xml ) Need Help??


in reply to Regexp for alphabetical order match within the string

Do you really need it to be a regex?

# $str defined elsewhere print "Is alphabetical\n" if( $str eq join '', sort split //, $str );

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Regexp for alphabetical order match within the string
by sauoq (Abbot) on Oct 30, 2003 at 19:09 UTC

    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.";
    
      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 ^_^)

      ++, 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.";
        
Re: Re: Regexp for alphabetical order match within the string
by asarih (Hermit) on Oct 30, 2003 at 19:37 UTC
    But this won't take case into consideration, would it? "Ba" is not really alphabetical in my book.
      For case-insensitivity just throw an lc before the shift.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://303357]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-25 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found