| | |
number of regex match
![]() |
But you have to remove the second $_, that is resetting the value.
Perl Syntax (Toggle Plain Text)
$_ = "fooFOObarBAR"; $count=(@var)=/[a-z]/g; print "$count @var\n";
An interesting construct to do the same thing. No need to actually use a named array:
But I would do it like this myself:
Perl Syntax (Toggle Plain Text)
$_ = "fooFOObarBAR"; $count=()=/[a-z]/g; print "$count\n";
But I would do it like this myself:
Perl Syntax (Toggle Plain Text)
$_ = "fooFOObarBAR"; $count = tr/a-z/a-z/; print "$count\n";
Well, I would normally have left the named array off and just done it with a simple scalar, but since the questioner is a newbie to perl, I wanted to show where the values end up when you use () and when you use just a scalar. I read a funny article in perl journal years ago (it was satire) in which the guy was supposedly using perl for military technology and forgot the () in the tolerances, so he was getting a "1" because it was the count of matches, rather than the value of matches.
Cool. Its sort of moot anyway in this example since tr/// is really the better way to do it since it involves no pattern matching, just simple character recognition.
I just thought the construct was interesting:
$count = () = /[a-z]/g;
I remember the first time I saw that years ago and it was a bit of a revelation in how perl works.
I just thought the construct was interesting:
$count = () = /[a-z]/g;
I remember the first time I saw that years ago and it was a bit of a revelation in how perl works.
Yeah, I find that interesting as well. Perl is amazing - thanks Larry!
I told a co-worker that I use perl everyday and he said "I hate that language" so I immediately lost all respect for him (haha).
You're right tr/// is better and more efficient. I sometimes use some old tricks because I have never had anyone actually teach me perl, just figured out by doing. I am now writing (most) everything object oriented in perl - which I hadn't before because I wrote most stuff very C-like.
I told a co-worker that I use perl everyday and he said "I hate that language" so I immediately lost all respect for him (haha).
You're right tr/// is better and more efficient. I sometimes use some old tricks because I have never had anyone actually teach me perl, just figured out by doing. I am now writing (most) everything object oriented in perl - which I hadn't before because I wrote most stuff very C-like.
![]() |
Similar Threads
- Basic Perl Web Upload Script (Perl)
- Regular Expressions with Decimal Points (Java)
- Need sample code for Text File Compression!!! (C++)
- Regex matches all but specific word? (Perl)
- rootmap help??urgent plz.. (PHP)
- Attribute error, Cookies not found! (Python)
- how to read csv file without using StringTokenizer (Java)
- Need help making simple random images (PHP)
- Trim Strings (JavaScript / DHTML / AJAX)
- Regular Expressions (C#)
Other Threads in the Perl Forum
- Previous Thread: Reading Substring from a string
- Next Thread: match data with another value in another file
Views: 1918 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Perl






