Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

isn't that the same in any expression-oriented language ?

E.g. in ruby this could be

    puts $1 if /(\d+)/ =~ 'test 123' # perl-like
    
    if m = /(\d+)/.match('test 123') then puts m[1] end # perl-less
Perl's regexes do more things perhaps, but this is a relatively common thing, I believe.


($hour, $min, $sec) = $someTimeString =~ /(\d\d):(\d\d):(\d\d)/;

    if ($hour >= 12) {
       if ($min == 42) {
          doSomething();
       }
    }

Whereas in Python, Ruby even though it's only a tiny extra step, but that symantic distance in one's head when parsing out and naming the parts of a regex on the same is a convenience that when you get used to it, you really miss it.

    m = /(\d\d):(\d\d):(\d\d)/.match( someTimeString )
    hour = m[0]
    min  = m[1]
    sec  = m[2]
    if hour >=  ...


but ruby, python etc.. can just extract the list too, it's just an extra method call on the same object (perhaps there could be destructuring too in modern ruby/python)

    hour, min, sec = /(\d\d):(\d\d):(\d\d)/.match('10:11:12').captures
or

    hour, min, sec = re.compile(r'(\d\d):(\d\d):(\d\d)').match('10:11:12').groups()
ruby & python will explode if there is no match rather than fail silently but I'm not convinced the difference is huge.


Ruby is a very concise language.

Matching & getting in most languages typically devolves into matching a regexp to a string, getting a MatchResult object, and then getting/iterating/checking/... on it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: