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

    matching a word of text with an array item.

    Hi,

    I ahve a section of text like this

    The quick brown fox jumps over the lazy dog.
    My array is like this
    @array = 'brown','lazy';

    My attempt is to use grep but I think I am trying to use the wrong structure.

    Code:
    my @text = $text;
    
    foreach my word (@text){
      if (grep $word, @text){
        do this;
      } else {
        do that;
      }
    }
    Should I put the text into an array and grep on each of its words to try to match the @array?


    bazz
    Last edited by FishMonger; 05-05-2010 at 01:00 PM. Reason: fixed the code tag
    "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,874
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Bazz,

    Are you still needing help with this, or have you worked out the solution?

    If you need help, then please provide more info on what you need to accomplish. The code you posted is not doing what you think, so before I make any suggestions, I'd like to have a little clarification on what you need.

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    FishMonger; yes, I am still struggling. I have prgressed enough to realise I should be using a regex but I am not even sure that this is the best way.

    what if, say, I have a word like 'London' and a phrase like 'London Airport'. I need to show a different link for each of those scenarios.

    so far my thoughts have reached this point.

    pseudo code:
    Code:
    foreach my $word_or_phrase (@array_of_link_words){
      $href = "<a href='/example.com/script/$word_or_phrase'>$word_or_phrase</a>";
      $text =~ s/ $word_or_phrase/$href/g; #space should ensure only plain text can be converted.  Don't want a link to London Airport, being mashed up bby the single word London. 
    }
    Is this anyway close?

    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
    •