CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Matrix for character substitution

 



jwhit61
Novice

Jan 29, 2009, 9:59 AM

Post #1 of 5 (4209 views)
Matrix for character substitution Can't Post

Not sure if this is the right forum. But I need to put together a script that I can give someone that has no perl knowledge. The script will be used to do character substitutions. While s/something/else/g is doable, I'm curious if it is possible to create a table or matrix of a hash? Where @ = at or = n. As we find additional characters that need defined, we can modify the hash without adding additional lines of code to handle the substitution.

For example,

%hash_example = (
=> 'n',
=> 'N',
=> 'u',
=> 'o',
=> '',
=> '-',
);


And then in the coding section, somehow s/%hash-key/%hash-value/g.

This doesn't seem to make sense to me (that it would be possible). Hopefully, I'm making sense enough here that someone can point me in the right direction.

Thanks in advance.


KevinR
Veteran


Jan 29, 2009, 11:02 AM

Post #2 of 5 (4206 views)
Re: [jwhit61] Matrix for character substitution [In reply to] Can't Post

If I understand your situation, it sounds possible. The "do" function might be what you are looking for, eval is another option but "do" should be better. You want to read the "do EXPR" section of the "do" manpage:

http://perldoc.perl.org/functions/do.html

How you actually code in the "do" call and make the subsitutions is entirely up to you and depends on your particular requirements.

Edit:

Disregard the above. I completely misunderstood your question. See below.
-------------------------------------------------


(This post was edited by KevinR on Jan 29, 2009, 1:19 PM)


shawnhcorey
Enthusiast


Jan 29, 2009, 1:04 PM

Post #3 of 5 (4204 views)
Re: [jwhit61] Matrix for character substitution [In reply to] Can't Post


Code
s/$_/$hash_example{$_}/g for keys %hash_example;


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


KevinR
Veteran


Jan 29, 2009, 1:18 PM

Post #4 of 5 (4203 views)
Re: [shawnhcorey] Matrix for character substitution [In reply to] Can't Post


In Reply To

Code
s/$_/$hash_example{$_}/g for keys %hash_example;



oops, I think I totally misunderstood his question.
-------------------------------------------------


1arryb
User

Feb 26, 2009, 2:32 PM

Post #5 of 5 (4119 views)
Re: [shawnhcorey] Matrix for character substitution [In reply to] Can't Post

Shawn,

I think you're onto something. But maybe:


Code
my %hash_example = ( 
=> 'n',
=> 'N',
=> 'u',
=> 'o',
=> '',
=> '-'
);
my $xtr = "<some non-english string>";
my $estr = '';

# Translate each character in the xstr, one at a time. Substitute '?' for untranslatable characters.
map { $estr .= $hash_example{$_} ? $hash_example{$_} : '?' } split('', $xstr);
print "xstr=$xstr, estr=$estr\n";

UPDATE:
Perhaps it's better to leave untranslatable (or English) characters unchanged:

Code
map { $estr .= $hash_example{$_} ? $hash_example{$_} : $_ } split('', $xstr);


Cheers,

Larry


(This post was edited by 1arryb on Feb 26, 2009, 2:42 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives