Author |
Message |
K6 #1 / 16
|
 array match
Hi all Im trying to compare two arrays but my code continues to mach within the
match has been made. My code: print "Content-type: text/html\n\n";
($type) = split(/\,/,$animal);
($type2) = split(/\,/,$animal2); if ($type eq $type2) { next; Quote: } else {
print "$type is not in in our list<br>"; Quote: } } }
###################################### The output to looks like this: cat is not in in our list cat is not in in our list cat is not in in our list cat is not in in our list dog is not in in our list dog is not in in our list dog is not in in our list dog is not in in our list mouse is not in in our list mouse is not in in our list mouse is not in in our list rat is not in in our list rat is not in in our list rat is not in in our list ######################################## I want the output to look like this: cat is not in in our list dog is not in in our list
|
Mon, 17 May 2004 13:08:06 GMT |
|
 |
Anno Sieg #2 / 16
|
 array match
Quote: > Hi all > Im trying to compare two arrays but my code continues to mach within the
> match has been made.
You should say more about what you mean by "compare" and "match". Both terms are so generic, they don't mean very much without context. Quote: > My code: > print "Content-type: text/html\n\n";
> ($type) = split(/\,/,$animal);
> ($type2) = split(/\,/,$animal2); > if ($type eq $type2) { > next; > } else { > print "$type is not in in our list<br>"; > } > } }
loops, but, as you have seen, that is more tricky than it appears to be. What you hoped to acquire with the split() operations remains a secret, however. In Perl, when a problem involves deciding whether a given string is a member of a set of strings, the solution almost invariably involves a hash. In particular, the exists operator tells if a given string is among the keys of a hash. So all you have to do is set up
my %h;
The second line involves a hash slice. It sets all values of %h to undef, but we only need the keys, not the values. Now your program amounts to simply:
print "$animal is not in our list\n" unless exists $h{ $animal}; } It's one of the points where Perl makes simple things simple. Anno
|
Mon, 17 May 2004 13:55:07 GMT |
|
 |
brian norma #3 / 16
|
 array match
Quote: > Hi all > Im trying to compare two arrays but my code continues to mach within the
a > match has been made. > My code: > print "Content-type: text/html\n\n";
> ($type) = split(/\,/,$animal);
> ($type2) = split(/\,/,$animal2); > if ($type eq $type2) { > next; > } else { > print "$type is not in in our list<br>"; > } > } }
Why are you performing a split on the $animal & $animal2 variables? The foreach takes each array element in turn. One way is to set a flag and only print the message if the flag is not set so.... something like this ..... $flag=0;
$flag=1 if ($animal2 eq $ animal); Quote: }
print "Not in the list\n" if ! $flag; of course you could make array2 a join'ed string (at the very start) and use index rather than a loop ... e.g.
print "$animal is not in the list\n" if (index($all_animal2,$animal)==-1); Now you figure out how to avoid erroneous sub-string matching ....what fun!
|
Mon, 17 May 2004 14:22:04 GMT |
|
 |
Paul Boardma #4 / 16
|
 array match
Quote:
> Hi all > Im trying to compare two arrays but my code continues to mach within the
> match has been made. > My code: > print "Content-type: text/html\n\n";
> ($type) = split(/\,/,$animal);
> ($type2) = split(/\,/,$animal2); > if ($type eq $type2) { > next; > } else { > print "$type is not in in our list<br>"; > } > } }
The logic behind your program is in error. How about #!/usr/bin/perl -w use strict;
print "Content-type: text/html\n\n";
my $found;
if($animal eq $animal2){ $found = 1; } } unless($found){ print "$animal is not in out list<br>"; } Quote: }
HTH Paul
|
Mon, 17 May 2004 14:19:51 GMT |
|
 |
Graham Woo #5 / 16
|
 array match
Quote:
> Hi all > Im trying to compare two arrays but my code continues to mach within the
> match has been made. > My code: > print "Content-type: text/html\n\n";
OK so far. Quote:
> ($type) = split(/\,/,$animal);
Not sure what you are attempting to do here. You are asking perl to split the single word (eg cat) at each comma and put the first element before the first comma into $type. Not sure why you would want to do this. Quote:
> ($type2) = split(/\,/,$animal2);
Same strange request here. You don't need to split the words as they are already single array elements that will be copied into $animal2 one at a time. Quote: This is logically what you are getting, if the first and second type matches you
etc. To get around this there is more than one way to do it, surprise surprise. Here are some: 1. Use labels with your loops as in:
if($animal eq $animal2){ next FIRST; } Quote: }
2. Jump out of the inner loop with last. That is, go to the last iteration of the inner loop when the text matches, this will have the same effect as going to the next iteration of the outer loop. 3. If you only have one occurrence of each text in each array, you would be far better to use a hash to compare them, then you don't need nested loops.
value. # The value doesn't matter, just the fact that it has a value at all.
unless($hash1{$_}){ # tests whether $_ exists in $hash1 print "$_ missing from list1\n"; } Quote: }
# you can repeat the exercise in reverse to check for elements present in array1 that # are not in array2
unless($hash2{$_}){ print "$_ missing from list2\n"; } Quote: }
Hope this helps. Doubtless this can be shortened and made more elegant. Graham Wood Quote: > next; > } else { > print "$type is not in in our list<br>"; > } > } } > ###################################### > The output to looks like this: > cat is not in in our list > cat is not in in our list > cat is not in in our list > cat is not in in our list > dog is not in in our list > dog is not in in our list > dog is not in in our list > dog is not in in our list > mouse is not in in our list > mouse is not in in our list > mouse is not in in our list > rat is not in in our list > rat is not in in our list > rat is not in in our list > ######################################## > I want the output to look like this: > cat is not in in our list > dog is not in in our list
|
Mon, 17 May 2004 18:43:56 GMT |
|
 |
Godzilla #6 / 16
|
 array match
Quote:
> Im trying to compare two arrays but my code continues to mach within the
> match has been made. > My code:
(snipped) Double looping is a relatively slow and inefficient way to approach this simple task. However, writing code which is slow and inefficient does comply with Perl 5 Cargo Cult dogma. I suppose your decision on how to do this would depend on your being a Perl 5 Cargo Cultist or your being a Perl programmer. Godzilla! -- TEST SCRIPT: ____________ #!perl
print "Forwards:\n\n";
{
{ print "$_ is not found in Array Two.\n\n"; } } print "\n\nBackwards:\n\n";
{
{ print "$_ is not found in Array One.\n\n"; } } exit; PRINTED RESULTS: ________________ Forwards: cat is not found in Array Two. dog is not found in Array Two. Backwards: bird is not found in Array One. snake is not found in Array One.
|
Mon, 17 May 2004 19:50:24 GMT |
|
 |
Damian Conw #7 / 16
|
 array match
> Double looping is a relatively slow and inefficient > way to approach this simple task. Of course, so is single looping: use Quantum::Superpositions;
print "Forwards:\n\n"; print map "$_ is not found in Array 1\n",
print "Backwards:\n\n"; print map "$_ is not found in Array 2\n",
;-) Damian
|
Mon, 17 May 2004 20:41:38 GMT |
|
 |
Godzilla #8 / 16
|
 array match
(snipped) Quote: > > Double looping is a relatively slow and inefficient > > way to approach this simple task. > Of course, so is single looping: > use Quantum::Superpositions; > print map "$_ is not found in Array 2\n",
Quantum is not ported for Win32, last I checked, else I would benchmark this for you. My experience is use of modules and especially use of the map () function, both are almost always slow and inefficient compared to more simple or, more imaginative programming methods. This is especially true with map. Like grep, map becomes less efficient with increasing data size. For your code, you are using both a module and map. I am not sure this would be the most efficient method. Certainly your memory overhead would be significantly bloated. My method will exhibit relatively the same efficiency with increasing data size. Memory bloat is minimized. Godzilla! -- [Do not pay any attention to what Godzilla says. It is a troll, and has no decent working knowledge of Perl or programming in general. Search groups.google.com to see a history of its posts and replies to these posts.]
|
Mon, 17 May 2004 21:16:32 GMT |
|
 |
Martien Verbrugge #9 / 16
|
 array match
On Thu, 29 Nov 2001 12:08:06 GMT,
Quote: > Hi all > Im trying to compare two arrays but my code continues to mach within the
> match has been made.
You're asking two frequently asked questions. What you ask in the subject is answered in Perl FAQ, section 4, question "How do I test whether two arrays or hashes are equal?". What you really seem to want to do is check whether an array contains a certain item, which is answered in Perl FAQ, section 4, Question "How can I tell whether a list or array contains a certain element?". Other questions around that same area may be of interest to you as well. Martien -- | Martien Verbruggen | That's not a lie, it's a Trading Post Australia Pty Ltd | terminological inexactitude. |
|
Mon, 17 May 2004 22:52:12 GMT |
|
 |
Damian Conw #10 / 16
|
 array match
> Quantum is not ported for Win32, last I checked, else > I would benchmark this for you. > For your code, you are using both a module and map. I am > not sure this would be the most efficient method. Certainly > your memory overhead would be significantly bloated. Well, I was obviously wrong when I decided to leave off that "I'm-not-being-serious-here" smiley. ;-) No, I wasn't really suggesting that solution as more efficient (though I do maintain that it's more elegant). On the other hand, Larry *is* still contemplating whether to include superpositions in Perl 6. If he does, then the optimal solution will eventually be:
Damian
|
Tue, 18 May 2004 01:39:50 GMT |
|
 |
Godzilla #11 / 16
|
 array match
(snipped) Quote: > > Quantum is not ported for Win32, last I checked, else > > I would benchmark this for you.
I downloaded your module a few minutes after reading your article. As of yet, I have not studied your methodology. Being candid, I have never had much luck, actually none, at successfully installing modules for my Win32 box, with or without xs files. In the past, I tried several and encountered way too many problems, such as very poor documentation, file names needing to be truncated to DOS eight plus three, other modules needing to be installed and so on. I decided those modules I do have installed, which are numerous, are more than ample for my needs. As you know, I personally am not one to use modules, save for a small handful, like Benchmark, LWP and Socket. This is not to say I do not learn from modules, do not "steal" from modules and write my own equal code into scripts. Don't be surprised if you read a script of mine which contains code similar to or simply ripped from your module. Quote: > > For your code, you are using both a module and map. I am > > not sure this would be the most efficient method. Certainly > > your memory overhead would be significantly bloated. > Well, I was obviously wrong when I decided to leave off that > "I'm-not-being-serious-here" smiley. ;-)
Oh. This is a risk all take with me. Each day, I am harassed and trolled without end by almost all participants here. My safest personal presumption to make about this group is all here are screaming arseholes until I can prove differently. Quote: > No, I wasn't really suggesting that solution as more efficient > (though I do maintain that it's more elegant).
Clearly your mind's eye view of eloquent is a masculine one. Boys and their toys. Girls and their curls. Quote: > On the other hand, Larry *is* still contemplating whether to > include superpositions in Perl 6. If he does, then the optimal > solution will eventually be:
These seems a logical inclusion. However, I do have valid reservations about including too much. My experience is today's Perl programmers rely too much on modules and default features. Almost all here are what I consider Copy & Paste Babies or more appropriately, Perl 5 Cargo Cultists. Few today, learn how to really program, how to write a program from A to Z, complete and final with no modules, no automatic features. These modules and features, discourage people from truly learning. It is clear and well evidenced, there are a lot of unimaginative, a lot of less-than-talented programmers populating this group. People will do better to learn how to program first, then enjoy these extra features, such as modules. My final comment is being born on a rural Oklahoma farm, barefoot, penniless, delivered by a Choctaw squaw and never being afforded a decent education having to work to simply survive, has taught me this lesson of learning lessons is truly a great lesson. Here are some benchmark results based on a snippet I grabbed from here. Godzilla! -- #!perl print "Content-type: text/plain\n\n"; use Benchmark; print "Run One:\n\n"; &Time; print "\n\nRun Two:\n\n"; &Time; print "\n\nRun Three:\n\n"; &Time; sub Time { timethese (1000000, { 'name1' =>
{ my $found;
{ if($animal eq $animal2) { $found = 1; } } unless($found) { $output = "$animal is not in out list<br>"; } }', 'name2' =>
{
{ $output = "$_ is not found in Array Two.\n\n"; } }', } ); } Run One: Benchmark: timing 1000000 iterations of name1, name2...
Run Two: Benchmark: timing 1000000 iterations of name1, name2...
Run Three: Benchmark: timing 1000000 iterations of name1, name2...
|
Tue, 18 May 2004 02:16:53 GMT |
|
 |
Helgi Bri #12 / 16
|
 array match
On Thu, 29 Nov 2001 17:16:53 -0800, "Godzilla!" Quote:
>I downloaded your module a few minutes after reading your >article. As of yet, I have not studied your methodology. >Being candid, I have never had much luck, actually none, >at successfully installing modules for my Win32 box, >with or without xs files. >In the past, I tried several and encountered way too many >problems, such as very poor documentation, file names needing >to be truncated to DOS eight plus three, other modules needing >to be installed and so on.
That's strange. I use a Win32 system ( as well as Linux, Solaris, HP-UX and Mac ) and on my box I currently have 71 third party modules, 62 installed with ppm and 9 installed the old-fashioned way with winzip, Makefile.PL, nmake, nmake test, nmake install. Every single one has installed without any trouble whatsoever, almost instantly, with full and complete documentation, working filenames etc. etc. You must have one seriously fu'd system. Regards, Helgi Briem
|
Tue, 18 May 2004 11:42:38 GMT |
|
 |
Scott R. Godi #13 / 16
|
 array match
| | > Double looping is a relatively slow and inefficient | > way to approach this simple task. | | Of course, so is single looping: | | use Quantum::Superpositions; |
| | print "Forwards:\n\n"; | | print map "$_ is not found in Array 1\n",
| | print "Backwards:\n\n"; | | print map "$_ is not found in Array 2\n",
| | ;-) | | Damian The above is why I love Perl so much :) Cool stuff like that just blows me away. I don't know HOW you managed to think of this, Damian, but my hat's off to you. I think this also belongs in the Examples section of the Documentation for Quantum::Superpositions, as I wouldn't have immediately seen the usefulness of 'map' in this context if I hadn't seen this post. Shaking my head in wonderment as I look at how sweet that is, and wishing I had seen or thought of it earlier. :) -- unmunge e-mail here: #!perl -w print map {chr(ord($_)-3)} split //, "zhepdvwhuCzhegudjrq1qhw"; # ( damn spammers. *shakes fist* take a hint. =:P )
|
Thu, 20 May 2004 09:25:05 GMT |
|
 |
Damian Conw #14 / 16
|
 array match
Quote:
> | > | print "Forwards:\n\n"; > | > | print map "$_ is not found in Array 1\n",
> | > | print "Backwards:\n\n"; > | > | print map "$_ is not found in Array 2\n",
> | > | ;-) > | > | Damian
> The above is why I love Perl so much :) Ditto. > Shaking my head in wonderment as I look at how sweet that is, and > wishing I had seen or thought of it earlier. :) It could get even lovelier if superpositions make it into the core of Perl 6. For instance, if C<printf> were able to natively handle superpositions, the whole thing might reduce to:
;-) Damian
|
Fri, 21 May 2004 00:17:55 GMT |
|
 |
Benjamin Goldber #15 / 16
|
 array match
[snip] Quote: > On the other hand, Larry *is* still contemplating whether to > include superpositions in Perl 6. If he does, then the optimal > solution will eventually be:
You mean %s, not $_, don't you? PS: Is there any reason you used map in your earlier code, instead of a for or foreach modifier? eg: print "$_ is not found in Array 2\n"
print "$_ is not found in Array 1\n"
-- Klein bottle for rent - inquire within.
|
Fri, 21 May 2004 06:38:52 GMT |
|
|
Page 1 of 2
|
[ 16 post ] |
|
Go to page:
[1]
[2] |
|