質問

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