rsriram has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help in using arrays
by Corion (Patriarch) on Mar 14, 2007 at 08:38 UTC | |
The code seems very similar to your code in Searching and printing through a array - did my reply not work for you? If you can tell me where my reply does not work for you I can maybe help you to adapt my code to your problem. It would also help to have a complete, self-contained program which initializes @all and reads the data from a __DATA__ section so we can run the code and reproduce the problem without any further setup on our part. Something like this:
| [reply] [d/l] [select] |
|
Re: Help in using arrays
by davorg (Chancellor) on Mar 14, 2007 at 08:51 UTC | |
If you are going to revisit a previous question then you should, at least, include a link to the earlier discussion so that we can see what advice has already been offered and ignored. Update: I've now taken a closer look at your code and I can't see the problem that you are having. The code below is based on your code above and has been expanded to create a standalone program. It seems to work as expected and print out the whole line. I therefore suspect that your problem is in the bits of your code that you aren't showing us.
And the output I get is:
There is, of course, a potential bug in this code as it checks for the existance of your numbers anywhere in a line of input and you specifically want to check for it in the second column. You can therefore change the central loop of my code to look lie this (and note that I've also been able to change the match operator to an equality operator).
Finally, there's no point in checking for more than one match, so it's worth exiting the inner loop once a match is found.
-- "The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg | [reply] [d/l] [select] |
by Anno (Deacon) on Mar 14, 2007 at 14:42 UTC | |
I suppose you have written it this way to stay close to the OP's coding style, but it should be mentioned that indexing the @all array is not necessary and not particularly perlish. Now it becomes apparent that what the loop does is really a grep operation: Finally, it turns out that grep can be replaced with a hash lookup when the list @all is replaced by a hash: Anno Update: Stylistic change, pomposity tuned down a notch. | [reply] [d/l] [select] |
by davorg (Chancellor) on Mar 14, 2007 at 15:08 UTC | |
I agree with everything you say - and would almost certainly have said it too if I had had more time this morning :) Just one small point. At the point when you switch to using "grep", you lose the tiny optimisation that you get from using "last" in the loop.
-- "The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg | [reply] |
by Anno (Deacon) on Mar 14, 2007 at 15:13 UTC | |
|
Re: Help in using arrays
by johngg (Canon) on Mar 14, 2007 at 09:48 UTC | |
The output is
I hope this is of use. Cheers, JohnGG | [reply] [d/l] [select] |
|
Re: Help in using arrays
by radiantmatrix (Parson) on Mar 15, 2007 at 14:02 UTC | |
It seems to me like you're over-thinking your algorithm. What you want to express in code is "for each line of data, if the value in the second column is contained in my pre-existing dataset (@all), print the line". Perl is very expressive, so it allows you to say that almost directly:
However, a knowledge of design patterns yields a more elegant solution suggested by the Schwartzian Transform.
The transformation of array to hash allows the check to be very simple: "does our dataset contain this value?".
<–radiant.matrix–>
Ramblings and references The Code that can be seen is not the true Code I haven't found a problem yet that can't be solved by a well-placed trebuchet | [reply] [d/l] [select] |