Re: How to get Incremental charecter groups in a string
by liverpole (Monsignor) on Jun 19, 2006 at 11:09 UTC
|
Hi Dora,
First of all, it's going to depend on exactly what you mean by incremental character string. The word "incremental" along with your example suggests that each string starts with the next letter of the alphabet (a.. b.. c..), but then the example doesn't follow the rule. Or do you just mean a string containing "this is <pattern> and <pattern> and <pattern> ..."?
Since the answer we give you will depend on the problem, I'd suggest you be more specific with EXACTLY what it is you're trying to match. The answer will most likely be some kind of regular expression match, but it will depend on having a more clearly-defined question.
Also, have you tried anything yourself? If so, show us the code you've started with ...
Update: Looking at your question some more, think I understand what you mean by "incremental" characters. Do you mean you want groups of characters in which each letter is further in the alphabet than the previous one? Or do you mean (more specifically) strings where each letter is the next letter in the alphabet from the previous one? (If it's the latter, then your example is still misleading, as bcf doesn't match. If it's the former, then you should realize that even words like is are going to match).
Here's a possible code fragment to get you started (assuming it's the former case, where subsequent letters only need to be further in the alphabet than previous letters):
#!/usr/bin/perl -w
+
use strict;
use warnings;
+
sub is_incremental {
my ($word) = @_;
my @letters = split //, $word;
my $prev = ord(shift @letters);
foreach my $letter (@letters) {
my $next = ord(lc $letter);
if ($next <= $prev) {
return 0;
}
$prev = $next;
}
return 1;
}
+
my $s = "This is abc and bcf and xyz and ijklmn";
+
foreach my $word (split /\s+/, $s) {
if (is_incremental($word)) {
print "Word [$word] is 'incremental'\n";
}
}
To modify this for the latter case, you could change the line "if ($next <= $prev) {" to "if ($next - $prev != 1) {". Maybe that will help you get started.
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
| [reply] [d/l] |
Re: How to get Incremental charecter groups in a string
by japhy (Canon) on Jun 19, 2006 at 11:31 UTC
|
That's pretty tricky to do with regexes, but I'll show you anyway:
while ($str =~ m{
( # capture to $1
. # any character
(?: # this chunk:
$ # either the end of the
+ string
| (??{ chr(1 + ord substr($&, -1)) }) # or the next character
+ (ASCII-wise) after
# the one we just match
+ed
)+ # one or more times
) # end capture
}xgs) {
print "run: <$1>\n";
}
| [reply] [d/l] |
|
|
#!/usr/bin/perl
+
use strict;
use warnings;
+
my $str = "this is abc and bcf and xyz and ijklmn";
+
while ($str =~ m{
( # capture to $1
. # any character
(?: # this chunk:
$ # either the end of the
+ string
| (??{ chr(1 + ord substr($&, -1)) }) # or the next character
+ (ASCII-wise) after
# the one we just match
+ed
)+ # one or more times
) # end capture
}xgs) {
print "run: <$1>\n";
}
Produces:
(?: # this chunk:
$ # either the end of the
+ string
| (??{ chr(1 + ord substr($&, -1)) }) # or the next character
+ (ASCII-wise) after
# the one we just match
+ed
)+ # one or more times
matches null string many times in regex; marked by <-- HERE in m/
( # capture to $1
. # any character
(?: # this chunk:
$ # either the end of the
+ string
| (??{ chr(1 + ord substr($&, -1)) }) # or the next character
+ (ASCII-wise) after
# the one we just match
+ed
)+ # one or more times
<-- HERE ) # end capture
/ at ./x line 17.
run: <hi>
run: <abc>
run: <bc>
run: <xyz>
run: <ijklmn>
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
| [reply] [d/l] [select] |
|
|
Yeah, I know, I got it too. A pox on it.
| [reply] |
Re: How to get Incremental charecter groups in a string
by GrandFather (Saint) on Jun 19, 2006 at 11:24 UTC
|
use strict;
use warnings;
my $str="this is abc and bcf and xyz and ijklmn";
my @words = split ' ', $str;
my @incWords = grep {$_ eq join '', sort split ''} @words;
print "@incWords";
Prints:
is abc bcf xyz ijklmn
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
Re: How to get Incremental charecter groups in a string
by Jasper (Chaplain) on Jun 19, 2006 at 16:24 UTC
|
$_ = "this abc xyz abbc donut ghijkl";
"abcdefghijklmnopqrstuvwxyz" =~ $_ and print for split;
This approaches it from a slightly different angle, and removes the false positive 'abbc' results that you get from the $_ eq join sort split method...
You can add a /i in there if you wanted to ignore case.. :S | [reply] [d/l] |
|
|
use strict;
use warnings;
my $str="this is abc and bcf and xyz and ijklmn but not abbc";
my @words = split ' ', $str;
my @incWords = grep {
my %uniq = map {$_ => 0} split ''; $_ eq join '', sort keys %uni
+q
} @words;
print "@incWords";
Prints:
is abc bcf xyz ijklmn not
DWIM is Perl's answer to Gödel
| [reply] [d/l] [select] |
|
|
Yes, well, yes, indeed, that does do what you say it does, but who knows what the OP wanted except the OP?
All I was showing was TMTOWTDI. Maybe. :)
| [reply] |
|
|
|
|
Nice, but it would be faster to use index:
index('abcdefghijklmnopqrstuvwxyz', $_) >= 0 and print for split;
| [reply] [d/l] [select] |
Re: How to get Incremental charecter groups in a string
by prasadbabu (Prior) on Jun 19, 2006 at 11:05 UTC
|
Hi Dora,
Welcome to the Monastery. :) Please take a look at Welcome to the Monastery! Make yourself at home..
What do you mean by incremental characters?
You ll get answers only in assumption unless you give exact rules. Here is one way to do it, but in assumption.
$str="this is abc and bcf and xyz and ijklmn";
(@word) = split ' ', $str;
for $org (@word)
{
$inc = join '', sort split '', $org;
print "Group of incremental characters:\t$org\n" if ($inc eq $org)
+;
}
updated: changed coding
| [reply] [d/l] |