If u have the text in $text, try:
$counter='a';
$text=~s/(MyNAME)/$1 . $counter/ge;
Main Topics
Browse All TopicsHi,
I need to generate a sequence of alphabets (lower case a to z) in Perl 5.8
How to do this ?
ie. if one particular string (say MyNAME), exist more than once, then I need to append lower case "a" to the 1st string, then "b" to the 2nd string
it would be like -->
MyNAMEa
MyNAMEb
and so on
:
:
I need to generate the sequence of characters based on the nbr of count of existence of MyNAME
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
My problem is ---
If MyNAME occurs more than once, then I need to make it unique by appending a to z at the end of it
Say, if MyNAME occurs thrice, then
MyNAMEa
MyNAMEb
MyNAMEc
somehow, I could mage to get this as follows --
my $act="MyNAME";
my $count=3;
my $ii;
my @var=('a','b','c','d'); #### ------> need to make this dynamic or auto generated
for $ii (0..$count-1)
{
print "cnt=$act"."$var[$ii]\n";
}
so, now my problem is how to generate the array from a to z without doing it as above
like, assigning a,b,c,.....
then I will use that array
Here's some code that does what you need. I'll explain the regex in my next message:
use strict;
my $str = "hello world hello nice to see you in EE";
# get all the words
my @res = split('\s+',$str);
my %h;
# store some information about them
foreach my $word (@res)
{
# test that we didn't get some puctuation
if ($word =~ /\w+/)
{
# how many of them?
$h{$word}{count}++;
# if there are more than one then we will add the salt
$h{$word}{salt} = ($h{$word}{count} > 1 ? 'a' : '');
}
}
# here's the tricky bit:
$str =~ s/(\b\w+\b)/$h{$1}{count} < 2 ? $1 : $1.$h{$1}{salt}++/ge;
print $str, "\n";
__END__
prints:
helloa world hellob nice to see you in EE
Here's the line that does the work:
$str =~ s/(\b\w+\b)/$h{$1}{count} < 2 ? $1 : $1.$h{$1}{salt}++/ge;
The first part should be fairly easy to understand:
$str =~ s/(\b\w+\b)/
Match characters on word boundaries
This part:
/$h{$1}{count} < 2 ? $1 : $1.$h{$1}{salt}++/ge;
might need some explanation.
$1 will hold the word that has been matched
$h{$1}{count} will be the count of that words occurances within the text.
Then that is tested to see if it is less than 2. I like to use that instead of '== 1' just in case...
If the count of that words occurances is less than two the the first part after the ? is what is substituted. In this case it is just $1 which means that words that occur only once will be substituted for themself. If the count is two or more then the result of the ? test will be the part following the : which is:
$1.$h{$1}{salt}++
In this case the substitution is the word followed by the salt value for that word, $h{$1}{salt}. Then the salt value is incremented so that a becomes b and b becomes c, etc.
The last bit, /ge, says to apply the expression globally to the string and the e says to treat the second part of the substitution as an expression that must be evaluated.
And a footnote: It really wasn't necessary to test the count of the words in the first loop before creating the salt value. Since the test is made when the words are being processed by the regex, had a word with a count of one had a salt value it simply would have been ignored.
So this line:
$h{$word}{salt} = ($h{$word}{count} > 1 ? 'a' : '');
can actually be:
$h{$word}{salt} = 'a';
and the code will work just as well.
Business Accounts
Answer for Membership
by: suhasbharadwajPosted on 2006-09-20 at 05:27:48ID: 17560052
Hi prasen,
why you want to keep count of MyNAME using "a to z",
instead you can use the counter for that.
suppose,
$str = "hello world hello nice to see you in EE";
@res = split('\s+',$str);
$count = 0;
$srch = "hello";
foreach $i (@res)
{
if($i =~ /$srch/)
{
$count++;
}
}
print $count; #gives the number of occurence of word "hello" in given string
Cheers!
Suhas