+ Reply to Thread
Results 1 to 3 of 3

Thread: Is string in array?

  1. #1
    Programmer smith is an unknown quantity at this point
    Join Date
    Jun 2006
    Posts
    153

    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. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,648
    Blog Entries
    57
    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Programmer KevinADC is an unknown quantity at this point
    Join Date
    Jan 2007
    Posts
    125
    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. fread into array position
    By kenna in forum C and C++
    Replies: 0
    Last Post: 08-17-2007, 08:03 AM
  2. Replies: 2
    Last Post: 06-27-2007, 10:03 PM
  3. [Help] String manipulation.
    By afiser in forum Visual Basic Programming
    Replies: 1
    Last Post: 05-31-2007, 03:24 PM
  4. Python 2D array question
    By annannienann in forum Python
    Replies: 3
    Last Post: 04-23-2007, 04:36 PM
  5. Accessing a string by array
    By Saint in forum C# Programming
    Replies: 3
    Last Post: 10-03-2006, 07:33 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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