
fashimpaur
User
Aug 6, 2002, 5:12 AM
Post #2 of 6
(1074 views)
|
|
Re: [msds] comparing two arrays which contain strings
[In reply to]
|
Can't Post
|
|
msds, First of all, I have not gone through your complete code. I have only gone so far as to look for noticeable errors and point them out. You can learn more if I do not make the script work for you. First, your chomp method was performed in a separate loop. Try using it as you read the data to help performance. Use it like this: while (my $read2 = <FH>){ chomp $read2; ... do something here with data ... }
Next, the split function takes as paramaters, a regex and a scalar string to split. Your code is trying to use a split on a space character. Try using the function like this: @wordlist = split(/ /, $read2); Also, when you are filling the @wordlist, you keep overwriting the contents of the array, not adding to it. Use the push function to add the new elements to the array. Like this: while (my $read2 = <FH>){ chomp $read2; push @wordlist, (split(/ /, $read2)); } Now, all words read from the filehandle will be in @wordlist. You also have a problem with your variable names. Try using strict. It forces you to declare variables before they are used and prevents namespace pollution. This would have kept you from trying to use the variable $last instead of $lastt when you are loading the hash %lexicon. Now, as constructive criticism, you do not need to read the same file again. You did not change the contents so, the words in @wordlist will be the same as the words in @tokens. Now that you have the words in @wordlist, you can search @wordlist like this: my $word2find = "house"; $wordfound = 0; foreach my $word(@wordlist){ next if $word ne $word2find; $wordfound = 1; } if ($wordfound) { ... do something } else { ... do something else } As far as which is faster, searching an array or searching a hash, I can only say that it depends on how much you know. If your program can remember exactly which key in the hash contains the value you are searching for, I would like to see how you do it. Either way, you have to search the array or the values of the hash. I hope this helps. Please post back if you have further questions or just to say that the help you got at PerlGuru solved the problem. Also, if this did help, please spread the word about what a great site this is. Good Luck, Dennis $a="c323745335d3221214b364d545". "a362532582521254c3640504c3729". "2f493759214b3635554c3040606a0", print unpack"u*",pack "h*",$a,"\n\n";
|