Closed Thread
Results 1 to 3 of 3

Thread: Is string in array?

  1. #1
    smith is offline Programmer
    Join Date
    Jun 2006
    Posts
    153
    Rep Power
    0

    Is string in array?

    I'm not good with perl but I've been told that I have to make this project in perl. Sorry for all of the questions.

    Here is my question: Is there a way in PERL to determine if a string is in an array? Basically search an array for a string?

    Code:
    @array1 = ("Bnn","Cnn","Dnn");
    $string = "Cnn";
    Is $string in @array1?
    Code:
    for (int i;;) {
       cout << "Smith";
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,448
    Blog Entries
    74
    Rep Power
    143
    I don't know perl, but in most languages you can loop through the index range of the array and compare each string in the array.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    KevinADC is offline Programmer
    Join Date
    Jan 2007
    Posts
    125
    Rep Power
    0
    one way:

    Code:
    @array1 = ("Bnn","Cnn","Dnn");
    $string = "Cnn";
    
    if (grep {$_ eq $string} @array1) {
       print "Found $string\n";
    }
    else {
       print "Nope, no $string\n";
    }
    but if @array1 is big, tens of thouands or more of elements, you might use a loop and "last" to break out early on the first true match. But if its just a few hundred/thousand strings, this way will be fine.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. string to array
    By lol33d in forum PHP Development
    Replies: 7
    Last Post: 09-10-2011, 11:20 PM
  2. string to array!!!
    By Hamed in forum PHP Development
    Replies: 1
    Last Post: 07-01-2011, 03:20 AM
  3. Add a string to a string array
    By Tannaz in forum C# Programming
    Replies: 1
    Last Post: 12-29-2010, 01:02 PM
  4. Export Array Contents in String Then Read Into Array Later
    By rsnider19 in forum PHP Development
    Replies: 3
    Last Post: 08-20-2010, 02:31 AM
  5. string array
    By norman_069 in forum C and C++
    Replies: 5
    Last Post: 06-22-2008, 06:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

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