Operating System - Linux
1819792 Members
3087 Online
109607 Solutions
New Discussion юеВ

Convert variable to uppercase in perl

 
SOLVED
Go to solution
Ratzie
Super Advisor

Convert variable to uppercase in perl

I have a script that prompts a user for input.


I need to make this into uppercase.
Bere in mind that I also cut the first 4 letters off the use as a variable for a file name.

I need what ever the use types in both prompts to be in uppercase.

print "Enter BLD: ";
chomp (my $bld = );
my $bld4=substr $bld,0,4; #Pull first 4 char out of BLD for naming of file

print "Enter in room:";
chomp (my $room = );

open my $fc, ">$bld4.cust_has" or die "$bld4.cust_has: $!";


I have tried when I print to do the \U but I get an error...

print $f join "," => $acode.$a[0],\U$bld, $room, $a[1], $a[2], "\n";
4 REPLIES 4
Stuart Browne
Honored Contributor
Solution

Re: Convert variable to uppercase in perl

Why not just use a simple:

y/[a-z]/[A-Z]/;

? like this:

print "Enter BLD: ";
chomp($_ = );
y/[a-z]/[A-Z]/;
my $bld4 = substr( $_, 0, 4 );
One long-haired git at your service...
Bryan Dees
Occasional Contributor

Re: Convert variable to uppercase in perl

Forgive me if someone replied to this already ask i'm trying to catch up on email after vacation. There is an easier way to do this by using the 'uc' function such as:

print $f join "," => $acode.$a[0],uc $bld, $room, $a[1], $a[2], "\n";

Or you can use ucfirst $_ to set the first character to upper case such as:
ucfirst lc $_

Perl rules...
Ross Minkov
Esteemed Contributor

Re: Convert variable to uppercase in perl

Another way to do it is to call the tr command from the perl script:

tr '[:lower:]' '[:upper:]'

Regards,
Ross
Alexander Chuzhoy
Honored Contributor

Re: Convert variable to uppercase in perl

First do all the necessary actions,then print the result.
For example:
print "Enter some long word\n";
chomp ($var=);
$var=~s/^....(.*)/$1/;
$var=~s/$var/\U$var/;
print "$var\n";
Regards.