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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple substitutions 3

Status
Not open for further replies.

JohnLucania

Programmer
Oct 10, 2005
96
US
chomp(@SeqChar = <STDIN>)

sub trans {
map {tr/abce/uvxy/, print "$_\n"} @SeqChar;
}

How do you get the strings translated from 'sub trans' and print the corresponding strings on the screen?

i.e.

if the strings translated are 'uvx' then print 'asn' on the screen
if the strings translated are 'uxx' then print 'cya' on the screen
if the strings translated are 'yyu' then print 'tis' on the screen
if the strings translated are 'xvu' then print 'lis' on the screen
etc
etc

 
What you want to do sounds perfect for a hash table.:

Code:
my %translations = (
   uvx => 'asn',
   uxx => 'cya',
   yyu => 'tis',
   xvu => 'lis'
);

now say your translated string is 'yyu'. You would most likely have that in a variable I'll just call $yyu:

print $translations{$yyu};

will print: tis

if you have a list of these translated strings:

print "$translations{$_}\n" for @list;
 
John - can you explain more what exactly it is that you're trying to do with this code. In your input/output examples, you finish with "etc, etc" but I can't see any particular pattern to the translations that are occurring. What rules are you using to decide what should translate to? In your previous post, you implied that each letter has another letter it maps to, which isn't the case here. Here, you're implying that each 3-letter ``word'' has its own unique translation that's unrelated to anything else, which is the problem Kevin's trying to solve. Is that correct?
 
sub trans {
map {tr/abce/uvxy/, print "$_\n"} @SeqChar;
}

my %translate = (
uvx => 'asn',
uxx => 'cya',
yyu => 'tis',
xvu => 'lis'
);

I am getting errors below. All of the strings (i.e. uvx, uxx, yyu, xvu, etc) should be from the sub trans and then need to be translated into i.e. asn, cya, tis, lis, etc etc
In this translation, it is not letter-by-letter translation, but word-by-word (three letters to three letters) translation. There are ~ 50 predefined strings (without clear pattern) to be translated.
i.e
uvx must be translated into 'asn',
uxx must be translated into 'cya',
yyu must be translated into 'tis',
xvu must be translated into 'lis'
etc
etc

The point is that the strings before the translations (uvx, uxx, yyu, xvu, etc) should be from sub trans {}

Can't modify non-lvalue subroutine call in scalar assignment line 16, near ");"
Global symbol "%translate" requires explicit package name at line 17.
Bareword "uvx" not allowed while "strict subs" in use at line 13.
Bareword "uxx" not allowed while "strict subs" in use at line 13.
etc
etc

 
Are you sure this is the ony code.
Because I executed it and it works.

Corwin
 
This is what I have:


#! /usr/bin/perl
use strict;
use warnings;

my @SeqChar;
print "Enter sequences and 'enter' and then Ctrl + D: ";
chomp(@SeqChar = <STDIN>);

sub trans {
map {tr/abce/uvxy/, print "$_\n"} @SeqChar;
}

my %translate = (
uvx => 'asn',
uxx => 'cya',
yyu => 'tis',
xvu => 'lis'
);

print "$translate{$_}\n" for @trans;
 
Code:
#!/usr/bin/perl
use strict;
use warnings;

my @SeqChar;
print "Enter sequences and 'enter' and then Ctrl + D: ";
chomp(@SeqChar = <STDIN>);

sub trans {
       map {tr/abce/uvxy/, print "$_\n"} @SeqChar;
       return \@SeqChar;
}

my %translate = (
   uvx => 'asn',
   uxx => 'cya',
   yyu => 'tis',
   xvu => 'lis'
   );

my $translated = &trans;
for (@$translated) {
   if (defined $translate{$_}) {
      print "$translate{$_}\n";
   }
}

should return ref to an array from the func and then derefference.

May exists better way so wait for >exp. :)

Corwin
 
Enter sequences and 'enter' and then Ctrl + D: abcabcabc
uvxuvxuvx
uvxuvxuvx
Can't use string ("2") as an ARRAY ref while "strict refs" in use at ./tchar.pl line 23, <STDIN> line 1.

I am getting this.

jl
 
Enter sequences and 'enter' and then Ctrl + D: abcabcabc
uvxuvxuvx
uvxuvxuvx
uvxuvxuvx
uvxuvxuvx
uvxuvxuvx

This is my output.

Corwin
 
Amm

Did u forget:
Code:
return \@SeqChar;

At the end of sub trans?

Btw that replaces only if matches the whole string.
uvx will be replaced, but not uvxuvx

Corwin
 
right, I forgot return \@SeqChar;

The final output shoud be 'asnasnasn' not 'uvxuvxuvx'.
How do you do that?

jl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top