in reply to Using Positions from one array to find the value in the same position of another.

Building on the ideas of NetWallah and choroba:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my @dict = ('???', qw(a abbey zymophore zymophoric)); ;; my %encode = map { $dict[$_] => $_ } 0 .. $#dict; dd \%encode; ;; chomp(my $sentence = <STDIN>); print qq{sentence: '$sentence'}; my @words = split ' ', lc $sentence; dd \@words; ;; my @encoding = map { $encode{ exists $encode{$_} ? $_ : '???' } } @wo +rds; dd \@encoding; ;; my @decoding = @dict[ @encoding ]; dd \@decoding; " { "???" => 0, "a" => 1, "abbey" => 2, "zymophore" => 3, "zymophoric" = +> 4 } a crumbling zymophoric abbey sentence: 'a crumbling zymophoric abbey' ["a", "crumbling", "zymophoric", "abbey"] [1, 0, 4, 2] ["a", "???", "zymophoric", "abbey"]


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Using Positions from one array to find the value in the same position of another.
by RuZombieSlayer (Novice) on Apr 29, 2016 at 21:19 UTC
    Like i said i am a learner so although i dont complete follow everything there i think i see what your doing.
    Correct me if i am wrong but you are creating a dictionary of words for values and for keys an array foreach number $#dict though i am not sure what map does. Then i also think you have implemented an error code for unrecognised words not in @dict?
      ... i am not sure what map does.

      See map. (Update: See also Map: The Basics in the Tutorials section.)

      ... creating a dictionary of words for values and for keys an array foreach number $#dict ...

      @dict is an array of all the words known to the program.  %encode is a hash associating any (known) word to its index in the  @dict array, with error handling.

      ... i ... think you have implemented an error code for unrecognised words ...

      Yes.


      Give a man a fish:  <%-{-{-{-<