SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search an Array [Perl]

    I am newish to perl and am having difficulties writing a script to search an array from user input, I have no idea where to start

    I have the basic code, just as an example
    Code:
    print("Please enter the users name: ");
    $name = <STDIN>;
    @names = ('Bob', 'Mary', 'June', 'David');
    So if the name is found it would say "name found", if it isn't then it would say "name not found, names you can choose from are <names in the array".

    If someone could post and an example/or give any help it would be gratefully appreciated
    Last edited by -CP-; Mar 9, 2007 at 15:32. Reason: title was "Search and Array", should have been "Search an Array"

  2. #2
    SitePoint Wizard bronze trophy KevinR's Avatar
    Join Date
    Nov 2004
    Location
    Moon Base Alpha
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one way

    Code:
    print("Please enter the users name: ");
    $name = <STDIN>;
    chomp $name;
    @names = ('Bob', 'Mary', 'June', 'David');
    if (grep {$_ eq $name} @names) {
       print "found";
    }
    else {
       print "not found";
    }

  3. #3
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Kevin!

    Exactly what I was looking for


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
  •