Hii I am a learner of Perl and I am asking a silly question now, I think!!
I have a text file containing:

ABCDEFGHIJKLmnOPQRSTUVWxYZ

Now I would like to translate those m n and x written in small letters into MN and X.

How could I do that? I have attached the text file aslo
Many thanks in advance..
cheers..

Please note that there are several lines in the text file like this and I need to do it for all of them.

$s="ABCDEFGHIJKLmnOPQRSTUVWxYZ";
$s="\U$s"; #\U translates a string to uppercase
print "$s\n";

Hii thanks..but I have got several lines actually in the same text like:
ABCDEFGHIJKLmnOPQRSTUVWxYZ
ABCDEFGHIJKLmnOPQRSTUVWxYZ
ABCDEFGHIJKLmnOPQRSTUVWxYZ
ABCDEFGHIJKLmnOPQRSTUVWxYZ

I was trying it in this way:

$letter="E:/letters.txt";

open (LETTER, $letter);
@let=<LETTER>;

$let=~ tr/mnx/MNX/;

print $let;
close LETTER;
exit;

But not getting anything..any help please??

if you want to do it that way, it will only translate those specific letters. You have to go through the file like this:

open(FILE,"<letter.txt");
while (<FILE>){
chomp;
print "\U$_\n";
}
close FILE;

That will print out each line in uppercase.

if you only want to match those letters:

open(FILE,"<letter.txt");
while(<FILE>){
chomp;
s/(m|n|x)/\U$1/g; #this matches m OR n or x - that was your problem
print "$_\n";
}

BTW, $let and @let are two different variables. Perl doesn't allow you to cross scalars and array references. You can have a $let, @let, %let and many more in Perl. They are pointers to the references in memory and you can see that by de-referencing them.

$let="a";
@let=("a","b","c");
%let=("1","a","2","b","3","c");
print \$let."\n";
print \@let."\n";
print \%let."\n";

Output:

SCALAR(0x182ad7c)
ARRAY(0x182ad9c)
HASH(0x182ae9c)

thanks but could not understand the last one...could you please elaborate it??
Thanks

What I was saying is that in your code you do this:

@let=<LETTER>;
$let=~ tr/mnx/MNX/;
print $let;

which is going to print nothing at all, because $let is not related to @let at all. They are two totally separate variables. Since $let is undefined, when you try to change the contents of it, nothing's going to come out.

You can fix your code by doing this:

open (LETTER, "<letter.txt");
@let=<LETTER>;
for (@let){
chomp;
s/(m|n|x)/\U$1/g; #this is the replacement code
print "$_\n";
}
close LETTER;

Output (with 3 lines of the original):

ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Let's look at this: s/(m|n|x)/\U$1/g; . What that does is that it replaces "m" or "n" or "x" with the uppercase (\U) of whatever it matched ($1). The parenthesis around the m|n|x tells Perl what to put in $1. The g at the end tells Perl to do the whole string, rather than stopping at the first instance of the pattern matched.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.