문제

my @array = qw( one two three four five six seven );
my $arr = \@array;
# $arr=~ tr/,(a-z)/\t(A-Z)/;
print uc(join(' ', @array)), "\n";

output

ONE TWO THREE FOUR FIVE SIX SEVEN

But I want the same output using tr///.

도움이 되었습니까?

해결책

Replace (a-z) with a-z and perform tr/// for each element of @array

my @array = qw(one two three four five six seven);

tr/a-z/A-Z/ for @array;
print join(' ',@array), "\n";

다른 팁

Use map function

@array = qw(one two three four five six seven);
@for=map{uc$_}@array;
print "@for \n";

It's far from clear what you want. Since you have a solution, why do you want to use tr instead? Is this a homework assignment?

Because tr returns the number of characters translated, you have to join the elements of the array into a separate variable and then translate that to upper case.

This program shows a working example.

By the way, please always use strict and use warnings at the start of every program you write, especially if you are asking for help with your code.

use strict;
use warnings;

my @array = qw( one two three four five six seven );

my $arr = join ' ', @array;
$arr =~ tr/a-z/A-Z/;
print $arr, "\n";

output

ONE TWO THREE FOUR FIVE SIX SEVEN
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top