View Full Version : matching check of looped variable, with all items in a variable length array - How?
I have searched for this but cannot see.
I have a loop in a loop. The parent loop builds an array. The 'child' loop, is meant to check the variable, on each run, against all the items in the previously-created array. If the two match, a conditional performs.
It works presently but the variable is duplicated. I tried flagging last night and using the term, last. But I couldn't get it to work.
Maybe I'm not on the right lines?
Plz help.
Bazz
push (@nearbyBusinessSubTypeListed, $nearbyBusinessSubTypeListed);
foreach my $listed (@nearbyBusinessSubTypeListed) {
if ($nearbyBusinessSubType eq $nearbyBusinessSubTypeListed) {
my $match = $nearbyBusinessSubType;
next;
}
}
print "<li><a href=\"/cgi-bin/nearby?baseBusiness=$baseBusiness&category=$nearbyBusinessType&subCategory=$nearbyBusinessSubType\">$listed</a></li>\n";
nkrgupta
01-25-2006, 01:00 PM
I have searched for this but cannot see.
.....
It works presently but the variable is duplicated. I tried flagging last night and using the term, last. But I couldn't get it to work.
.....
[/code]
Could you please explain what do you mean by variable is duplicated and what exactly do you want to do on a successful match?
ah,
Each time the loop retunrs, it outputs its data. This output can be repetitive like this..
breakfast
lunch
lunch
breakfast
tea
dinner
lunch.
I want it to read (in whatever order),
breakfast
lunch
dinner
tea
with no duplicates.
here is the last incarnation of my code.
push (@nearbyBusinessSubTypeListed, $nearbyBusinessSubType);
foreach my $listed (@nearbyBusinessSubTypeListed) {
if ($nearbyBusinessSubType eq $listed) {
my $match = 1;
last;
}
}
print "m= $match\n";
#print "<li><a href=\"/cgi-bin/nearby?baseBusiness=$baseBusiness&category=$nearbyBusinessType&subCategory=$nearbyBusinessSubType\">$nearbyBusinessSubTypeDeHyphenated</a></li>\n";
FishMonger
01-25-2006, 03:48 PM
I don't understand your logic in using the inner loop. You're first pushing the value of $nearbyBusinessSubType onto the array, then you're looping through the array to see if that same value is in the array; that doesn't make sense and is probably the cause of the duplication.
BTW, it would be more efficient to use a hash instead of an array. With a hash, you wouldn't need to iterate over each and every value multiple times.
groan :) hashes are my latest mental blockage.
Hi FishMonger,
since you explain it like that, I would tend to agree that is doesn't make sense. So to put it into the hash, I should remove the lop and the line above it?
I'm going to read about hashes again tonight and see if, finally, I can understand them.
Bazz
ok, so I now have it in a hash and it displays just as did, the array.
%hash = (%hash, "$nearbyBusinessType", "$nearbyBusinessSubTypeDeHyphenated");
print "<li><a href=\"/cgi-bin/nearby?baseBusiness=$baseBusiness&category=$nearbyBusinessType&subCategory=$nearbyBusinessSubType\">$hash$nearbyBusinessSubTypeDeHyphenated</a></li>\n";
To try to make sure each item prints out only once I tried this
unless (exists ($hash{"$nearbyBusinessSubTypeDeHyphenated"})){
%hash = (%hash, "$nearbyBusinessType", "$nearbyBusinessSubTypeDeHyphenated")
}
print "$hash$nearbyBusinessSubTypeDeHyphenated\n";
am I even close?
bazz
cyber11
01-26-2006, 06:12 PM
Hashes can be difficult to wrap your brain around but once gaining a reasonable understanding are extremely powerful.
Heres a simple dedupe routine
@words=qw(breakfast
lunch
lunch
breakfast
tea
dinner
lunch
);
%seen=();
foreach $word (@words){
unless($seen{$word}){
$seen{$word} = 1;
print qq~$word\n~;
}
}
and a tally example, sorted descending
%word_freq=();
foreach $word (@words){
$word_freq{$word}++;
}
foreach $word (reverse sort{$word_freq{$a}<=>$word_freq{$b}} keys %word_freq){
print qq~$word_freq{$word}|$word\n~;
}
A very helpful website for me to understand hashes was WDVL (http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/index10.html)
Article by Aaron Weiss.
-Bill
Bill thanks for the reply.
Unfortunately, however, this gives the sem output as what I had. :)
push (@nearbyBusinessSubTypeListed, $nearbyBusinessSubType);
%seen=();
foreach $word (@nearbyBusinessSubTypeListed){
unless($seen{$word}){
$seen{$word} = 1;
print "$word\n";
}
}
could the difficulty be caused by the fact that this is inside a loop?
Bazz
cyber11
01-26-2006, 07:37 PM
One of the problems is I don't know what the contents of your 2 arrays is.
Could you post a small sampling of real data?
-Bill
No probs,
push (@nearbyBusinessSubTypeListed, $nearbyBusinessSubType);
That means that
@nearbyBusinessSubTypeListed = (Restaurant Restaurant Restaurant Restaurant Inn);
%seen=();
foreach $word (@nearbyBusinessSubTypeListed){
unless($seen{$word}){
$seen{$word} = 1;
print "$word\n";
}
}
and of course, the array, $nearbyBusinessSubTypeListed, is created inside a loop.
I just want to have the output display as, Restaurant Inn. (in any order).
bazz
cyber11
01-26-2006, 09:11 PM
push (@nearbyBusinessSubTypeListed, $nearbyBusinessSubType);
# That means that
@nearbyBusinessSubTypeListed = (Restaurant Public-House Restaurant);
For the sake of this example that would be a 3 element array
I added the qw so that it would read as 3 elements
@nearbyBusinessSubTypeListed = qw(Restaurant Public-House Restaurant);
%seen=();
foreach $word (@nearbyBusinessSubTypeListed){
unless($seen{$word}){
$seen{$word} = 1;
print "$word\n";
}
}
and my result is
Restaurant
Public-House
what was the result you were expecting?
-bill
Heh, That is the result I am expecting but it isnt happening presumably because its being done in a loop.
I'll try another plan :)
Bazz
cyber11
01-26-2006, 09:45 PM
I don't think its the loop.
To get unique elements requires some sort of loop.
If what you posted was any indcation it was the input into the array.
@nearbyBusinessSubTypeListed = (Restaurant Public-House Restaurant);
which will not work.
My question is how many elements long is @nearbyBusinessSubTypeListed in your script?
-Bill
actually, when I
print "@nearbyBusinessSubTypeListed"; I get this:
RestaurantRestaurantInnRestaurantInn
The number of elements is variable because it is generated dynamically. It presently has restaurants shown, once, for every time one is found in the loop. This could increase to 1500 times.
bazz
cyber11
01-26-2006, 10:16 PM
My point is, are you iterating over the array?
or are you atemting to iterate a single element, the variable?
So if you check the length of @nearbyBusinessSubTypeListed
print scalar(@nearbyBusinessSubTypeListed);
what do you get?
@nearbyBusinessSubTypeListed = qw(Restaurant Public-House Restaurant);
print scalar(@nearbyBusinessSubTypeListed);
should give 3 as a result
However this should not work at all
@nearbyBusinessSubTypeListed =(Restaurant Public-House Restaurant);
print scalar(@nearbyBusinessSubTypeListed);
-Bill
I get a 1 (The nunber one), for each item. That's what I meant when I said that it's created in a loop. Is this why it's hard to compare them for a match?
I have also achieved a similar result using a hash. But I haven;t made any further progress.
bazz
cyber11
01-26-2006, 10:40 PM
When you generate the array use a newline like this.
push (@nearbyBusinessSubTypeListed, qq~$nearbyBusinessSubType\n~);
It may have to be chomped off at some point but this should do it.
To test check length of array as in last post.
-Bill
The qw seems to make no difference. It's still scalar.
I had also done it in a hash
%hash = (%hash, "$nearbyBusinessType", "$nearbyBusinessSubTypeDeHyphenated");
print scalar(%hash);
This gives the output like a fraction: 1/8 or 2/8 or 3/8
Haven't worked that out yet.
bazz
cyber11
01-28-2006, 02:40 PM
I guess I'm not being claer enough.
this is incorrect and gives an error
@nearbyBusinessSubTypeListed =(Restaurant Public-House Restaurant);
But add the qw now a 3 element array
@nearbyBusinessSubTypeListed = qw(Restaurant Public-House Restaurant);
It is the equivalent of:
@nearbyBusinessSubTypeListed = ("Restaurant", "Public-House", "Restaurant");
In my last post I ask you to try this:
When you generate the array use a newline like this.
push (@nearbyBusinessSubTypeListed, qq~$nearbyBusinessSubType\n~);
Did you try that?
Eventhough dynamically generated arrays should have as many elements as you pushed onto them, something seems to be going wrong at that point.
Without seeing more of your script its hard to tell what the problem really is.
-Bill
I've pm'd you.
yes I have tried what you suggested and I get a 1 outputting each time. so instead of Restaurant Restaurant Inn, I get a 1 for each.
netroact
01-28-2006, 06:55 PM
Well, if this isn't a secret thread, I hope you will share the answer with me. I have a similar question.
nope, not a secret thread.
The rsult that worked for me and provided by cyber11 (Thanks again), was this
unless($seen{$nearbyBusinessSubTypeDeHyphenated}){
$seen{$nearbyBusinessSubTypeDeHyphenated} = 1;
print "<li><a href=\"/cgi-bin/nearby?baseBusiness=$baseBusiness&category=$nearbyBusinessType&subCategory=$nearbyBusinessSubType\">$hash$nearbyBusinessSubTypeDeHyphenated</a></li>\n";
} # end of unless
Bazz
netroact
01-28-2006, 07:21 PM
Thanks!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.