Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

reversed hash doesn't identify keys

by holly_71 (Initiate)
on Dec 11, 2012 at 21:25 UTC ( #1008410=perlquestion: print w/replies, xml ) Need Help??
holly_71 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to work with English and Dari - starting with a simple 1-1 translation.

It goes from English to Dari ok (except everything is printed backwards, L->R), but nothing is printed when Dari is input. Part of the problem is the backwards issue, w/Dari in STDIN (& STDOUT) it comes out L->R - I don't know how to address this so it will match the keys in %dari2english

(ex: خداوند بود خردمند should be translated as "God was wise" but nothing is printed; When going from E->D God was wise, you get the right words in the right order, but with the letters in the words going L->R (I can't replicate it here))

Thanks for any help.

#!/usr/bin/perl use utf8; binmode STDOUT, ":utf8"; %english2dari = ( "wise" => "&#1582;&#1585;&#1583;&#1605;&#1606;&#1583;", "was" => "&#1576;&#1608;&#1583;", "God" => "&#1582;&#1583;&#1575;&#1608;&#1606;&#1583;"); %dari2english = reverse %english2dari; do{ print "Give a sentence in Dari or English:\n"; chomp ($line = <>); if ($line =~ /\w/){ $line =~ s/^\s*//; $line =~ s/\s*$//; @words = split /\s+/, $line; @words = reverse @words; #word order R-> L but problem remains w/letters L->R foreach $word(@words){ if (exists ($english2dari{$word})){ $dariword = reverse $english2dari{$word} =~ m/\X/g; #should (dnw)reverse how Dari words print, so it's R->L print " $dariword " ; } elsif (exists ($dari2english{$word})) { print " $word "; #this doesn't seem to work at all } else { print " $word "; #unknown - doesn't work either } } } print "\n"; }

In my system, %english2dari displays the characters correctly:

"wise" => "خردمند", "was" => "بود", "God" => "خداوند",

Replies are listed 'Best First'.
Re: reversed hash doesn't identify keys
by roboticus (Chancellor) on Dec 11, 2012 at 22:33 UTC


    If you want to reverse the letter order in a word, you can do that by splitting the word into characters (via split), and then rejoin them in the correct order:

    # Take a word my $word = "Constantinople"; # Split into letters: my @letters = split '', $word; # Reverse them my @srettel = reverse @letters; # Join back into a single word: my $drow = join('', @srettel);

    Of course, you can combine it all into a single step:

    my $drow = join('', reverse split //, "Constantinople");

    Update: Or you could (as johngg pointed out) just use reverse, and it'll do the job without all the hoop-jumping-through-stuff.


    When your only tool is a hammer, all problems look like your thumb.

      reverse works on scalar strings as well!

      $ perl -Mstrict -Mwarnings -E ' > my $word = q{Constantinople}; > my $drow = reverse $word; > say $drow;' elponitnatsnoC $

      I hope this is of interest.




        I've been writing perl for a good while now, and I'm frequently surprised at what I don't know.


        When your only tool is a hammer, all problems look like your thumb.

        Thanks very much! I had one too many "reverse"s in there and it was reversing the reversed letters back...

Re: reversed hash doesn't identify keys
by Anonymous Monk on Dec 11, 2012 at 22:08 UTC
    you forgot to binmode STDIN to something as well :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008410]
Approved by davido
and the radiator hisses contentedly...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-06-20 08:20 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.