Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert str -> str 2

Status
Not open for further replies.

JohnLucania

Programmer
Oct 10, 2005
96
US
chomp(@SeqChar = <STDIN>) will have hundreds of input strings of qw/abce/

How do you convert all of the inputs like below?

a -> u
b -> v
c -> x
e -> y
 
sub Trans {
return scalar tr/abce/uvxy/ for @SeqChar;
}

print Trans ();

why is this not printing?

jl
 
It's not printing anything because there is nothing to print. What's it supposed to print?
 
I need to have the translated strings printed on the screen.

jl
 
actually, it might print the number of times the tr/// operator transformed a character, but thats not what you want (I think). Using your method I maybe would do it like this:

Code:
sub Trans {
    map {tr/abce/uvxy/, print "$_\n"} @SeqChar;
}
Trans();

but unless you intend to call the Trans() sub a few times in the script there is no need to use a sub routine.
 
Kevin - perfectly good solution. Personally, I'd prefer to use a `for' loop rather than `map' in a void context. (See here for reasons why), which would change that to:
Code:
sub Trans {
    for ( @SeqChar ) {
       tr/abce/uvxy/;
       print "$_\n";
    }
}
Trans();
or
Code:
# note I'm not adding the newline - if this is the only 
# thing happening to @SeqChar, there's no need to chomp
# it in the first place!
sub Trans {
    map tr/abce/uvxy/, @SeqChar;
}
print Trans();
 
I agree with you ishind, but in the this case the print command seems like it makes it obvious what the map function is being used for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top