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!!
|
|---|