...

View Full Version : grepping problem.



bazz
02-17-2010, 06:43 PM
Hi,

I am looping through an array and trying to grep to see if each variable, is on another array.



while (my ($images, $text )=$sth->fetchrow_array) {
$images_hash{$images}=$text;
push (@images_in_db,$images);
}
print Dumper \%images_hash; #ok
print qq( aidb=@images_in_db <br />); #ok
print qq( imgs=@images <br /> ); #ok

foreach my $image (sort @images){
if (grep $image, \@images_in_db ){

print qq( image match $image <br /> );
} else {
print qq( no image match <br /> );
}
}



Currently shows all to be a match when clearly they are not. :(

@images_in_db=image06.jpg image07.jpg image08.jpg

@images=image01.jpg image02.jpg image03.jpg image04.jpg image05.jpg image06.jpg image07.jpg image08.jpg

Can anyone see what I am overlooking please? Maybe I should grep in the while loop before building the second array, @images_in_db?

bazz

FishMonger
02-17-2010, 07:33 PM
Simple, don't use the array. You already have the names as hash keys, so simply do a hash lookup instead the the grep.

bazz
02-17-2010, 10:38 PM
Thanks FishMonger,

Maybe I am confusing things again but, there isn't always a value to the hash key. If no accompanying text snippet has been assigned to the image, the value will be ''.

here's what I am doing..
1. get list of images in the dir 'Gallery'
2. get list of records in the db, which show the gallery and accompanying text for the image - if the image is actually in the gallery.
3. where the image is in the gallery, output the key, the value and a checked checkbox.
4. where the image is yet to be assigned to the gallery, the same is outputted but the checkbox is to be unchecked.

So, all images will output with any accompanying text and a checkbox whether they are in the gallery or not. The checkbox enables the adding/removal of images to/from the gallery.

I think my issue is in looking through the hash keys as I loop through the array of images.



foreach my $image (sort @images){
#print qq( aidb=@images_in_db <br />); #ok
#print qq( ppi=$image : ihpi=$images_hash{$image}<br />); #ok
#print qq( imgs=@images <br /> ); #ok

print keys (%images_hash); #prints the keys as if one joined string ie no spaces between them.

if ( (keys (%images_hash) ) eq $image ){

print qq( image match $image <br /> );
} else {
print qq( no image match $image <br /> );
}
}




If I try to match $images_hash{$image} with $image, I would need a value to be there and that won't always be so.

I feel like I should be getting this more easily so either I am forgetting stuff or my manflu is getting very serious!

bazz

FishMonger
02-17-2010, 10:49 PM
while (my ($images, $text )=$sth->fetchrow_array) {
$images_hash{$images}=$text;
}

my @images_in_db = keys %images_hash; # only needed for your debug statement
print Dumper \%images_hash; #ok
print qq( aidb=@images_in_db <br />); #ok
print qq( imgs=@images <br /> ); #ok

foreach my $image (sort @images){
if (exists $images_hash{$image} ){

print qq( image match $image <br /> );
} else {
print qq( no image match <br /> );
}
}

bazz
02-17-2010, 11:06 PM
Thank you Fishmonger,

I'm gonna have to read up some more. If I wanted to show the *value* of the hash key, I would use %hash{$key}. So I need to find out why checking for the existence of $hash{$key} checks for the key and not the *value*. That hole in my knowledge threw me on this one.

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum