RuZombieSlayer has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I want to use the values from one array to search for positions in another and return what was found at those positions, the code below may give you and idea of what i am trying to achieve. Basically input a sentance and have each word turned into a 6 digit number to show that words position in an array then find the value of something in another array at the same position. I am new to pearl have been learning for a few weeks. I am teaching my self and am hoping to learn through this project i have set myself. Sorry if the code is a bit sloppy or not as sleek as it could be.

use stict; use warnings; my @words = ("a", "abbey", .. "zymophore", "zymophoric"); #in reality, this is a list of all words in the english #language apro +ximatly 400,000 words long my $sentance = <>; my @sentanceSplit = split(' ', $lcsentance); #all words need to be lower case #for simplicity, say we type "a zymophoric". my @indexes; foreach my $place (@sentanceSplit) { push @indexes, sprintf '%06d', first_index { $_ eq $place } @words +; }; #because of our input, @indexes will print 000000 399999 #because 'a' is in the 0th position of @words therfore #000000 #and 'zymophoric' is in the 400 thousandth position #retuning 399999 my @numbers2 = ("250690", "004058", ..); #400,000 numbers but in a random order instead of #(0..399999) #my goal now is to take the values from @indexes and #compare them to +@numbers2 and say (values of @indexes are #0 399999) notice we remove + the leading zeros. Then look #for what is foreach in the oth positio +n of @numbers2 ie. #"250690" and the 399999th position returning what +ever it #finds lets say 000001 #then have my @new = whatever the result is, so ("250690", #"000001");

I hope i have given you an idea what i am trying to achieve, I am not sure how to achieve this, perhaps hashes? eitherway an example would be very helpful thanks!!

  • Comment on Using Positions from one array to find the value in the same position of another.
  • Download Code

Replies are listed 'Best First'.
Re: Using Positions from one array to find the value in the same position of another.
by AnomalousMonk (Archbishop) on Apr 29, 2016 at 21:08 UTC

    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:  <%-{-{-{-<

      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:  <%-{-{-{-<

Re: Using Positions from one array to find the value in the same position of another.
by NetWallah (Canon) on Apr 29, 2016 at 20:11 UTC
    This sounds like an xy+problem. You have expressed some ideas about how to achieve something without detail on WHAT you are trying to achieve.

    for Lookups, a hash would be a much better structure. Are you trying to come up with some sort of encryption scheme ?

    A few pointers:
    * @words would probably work better as %words (a hash).
    * When you do I/O using <>, you most likely need a "chomp"
    * If @numbers2 contains 0..40K in random order, why not sort it, so it is indexed easier ?
    Alternatively, create an index array that indirectly accesses @numbers2.

    In any case .. let us know what you want to build, and I'm sure you will get many helpful suggestions, and perhaps, even code.

            This is not an optical illusion, it just looks like one.

      Try XY Problem instead.   (xy+problem is actually an N-problem: it is Non-existent. :)


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

      Yes a form of encryption is the final result, this is just a step but the idea is to use a sentence determind by <> and push each word into an array. @numbers2 is in a random order because i want it to be, on my computer there are 26 random lists like @numbers2 to be used to switch out ID's. Because if someone knew the 6 digits where positions of a word in the language, they could just get their own list and count till they found the word. The purpose of the random @numbers2 is to switch the 6 digit keys that represent position with othe random 6 digit keys. So far i have been thinking about and am thinking of making a hash where keys are numbers (0..$#numbers2) and values are @numbers 2 so that when i give it position 0 (for the word "a" it will return a random number such as 300460 etc. Your right when i go back and optimize this i may well just make @words a %hash
Re: Using Positions from one array to find the value in the same position of another.
by choroba (Cardinal) on Apr 29, 2016 at 20:17 UTC
    Just use @indexes to retrieve data from @numbers2. You can use an array slice:
    my @new = @numbers2[@indexes];

    For longer sentences, you might notice that the first_index loop is too slow. A hash from words to positions would be much faster to find the position.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,