Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    grepping problem.

    Hi,

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

    Code:
      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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Simple, don't use the array. You already have the names as hash keys, so simply do a hash lookup instead the the grep.

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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.

    Code:
     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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,840
    Thanks
    2
    Thanked 160 Times in 155 Posts
    Code:
      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 /> ); 
        }
      }

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •