Newbie: does an array contain a string 
Author Message
 Newbie: does an array contain a string

Here is an easy one for a seasoned veteran.  I've already spent a good
deal of time trying things out and just don't want to waste any more
time.

assuming:

$astring        = some_function();


#              ^^^^^^^^
#              ^^^^^^^^
        &do_something;

Quote:
} else {

        &do_something_else;

Quote:
}

How do I check to see if a given string is one of the elements of an array?
--
|  Alan McKay  |  Sys-Admin  | Acadia University |  Wolfville, NS |  B0K 1S0  |

|     *** mon Canada inclut le Quebec   *     my Canada includes Quebec   *** |
|         geaeuserte Meinungen sind meine eigene, nicht etwa die der Uni      |


Sat, 22 Mar 1997 01:57:57 GMT  
 Newbie: does an array contain a string

Quote:

>Here is an easy one for a seasoned veteran.  I've already spent a good
>deal of time trying things out and just don't want to waste any more
>time.
>assuming:

>$astring    = some_function();


will

do?

Warning: This veteran started learning perl on friday.
--
                  People who like this sort of thing
              will find this the sort of thing they like.



Sat, 22 Mar 1997 18:05:50 GMT  
 Newbie: does an array contain a string

:Here is an easy one for a seasoned veteran.  I've already spent a good
:deal of time trying things out and just don't want to waste any more
:time.
:
:assuming:

:
:$astring       = some_function();
:

:#              ^^^^^^^^
:#              ^^^^^^^^
:       &do_something;
:} else {
:       &do_something_else;
:}
:
:How do I check to see if a given string is one of the elements of an array?

From ftp.cis.ufl.edu:/pub/perl/doc/faq.gz

5.5) How can I tell whether an array contains a certain element?

    There are several ways to approach this.  If you are going to make
    this query many times and the values are arbitrary strings, the
    fastest way is probably to invert the original array and keep an
    associative array lying about whose keys are the first array's values.


        undef %is_blue;

    Now you can check whether $is_blue{$some_color}.  It might have been
    a good idea to keep the blues all in an assoc array in the first place.

    If the values are all small integers, you could use a simple
    indexed array.  This kind of an array will take up less space:




    Now you check whether $is_tiny_prime[$some_number].

    If the values in question are integers instead of strings, you can save
    quite a lot of space by using bit strings instead:


        undef $read;

    Now check whether vec($read,$n,1) is true for some $n.

--

Real programmers can write assembly code in any language.   :-)  



Sat, 22 Mar 1997 20:03:07 GMT  
 Newbie: does an array contain a string

Quote:

>Here is an easy one for a seasoned veteran.  I've already spent a good
>deal of time trying things out and just don't want to waste any more
>time.

>assuming:


>$astring    = some_function();


>#              ^^^^^^^^
>#              ^^^^^^^^
>    &do_something;
>} else {
>    &do_something_else;
>}

>How do I check to see if a given string is one of the elements of an array?

A straightforward way to do it might be


  {
    &do_something;
  }
  else
  {
    &do_something_else;
  }

if you were going to be scanning the array frequently then making an
associative array with keys of the elements of an_array might speed
things up 'cos it hashes into the keyspace e.g.

  undef %in_an_array;

  ...

  if ($in_an_array{$astring})
  {
    &do_something;
  }
  else
  {
    &do_something_else;
  }

but this means you end up maintaining both an array and a hash, so it's a
matter of judgement whether the performance benefits outweigh the effort

execution).

Of course, this is perl and there are other ways to do this :-)

Hope this helps,

Mike
--
The "usual disclaimers" apply.    | Meiko
Mike Stok                         | 130C Baker Ave. Ext

Meiko tel: (508) 371 0088 x124    |



Sat, 22 Mar 1997 20:20:34 GMT  
 Newbie: does an array contain a string

# Here is an easy one for a seasoned veteran.  I've already spent a good
# deal of time trying things out and just don't want to waste any more
# time.
#
# assuming:
#

#
# $astring      = some_function();
#

# #              ^^^^^^^^
# #              ^^^^^^^^
#       &do_something;
# } else {
#       &do_something_else;
# }
#
# How do I check to see if a given string is one of the elements of an array?

The easy answer is don't use an array, us an association:

%a_map = ("one",   1,
          "two",   2,
          "three", 3,
          "four",  4);

$astring = &some_fn();

if (defined $a_map{$astring}) {
  &do_something;

Quote:
} else {

  &do_something_else;

Quote:
}

--
David Avraamides
Goldman, Sachs & Co.
(212) 357-1346



Sun, 23 Mar 1997 03:04:21 GMT  
 Newbie: does an array contain a string

Quote:


>>Here is an easy one for a seasoned veteran.  I've already spent a good
>>deal of time trying things out and just don't want to waste any more
>>time.

>>assuming:


>>$astring        = some_function();


>will

>do?

What if $astring = 'onetwo', or even $astring = 'reef'?

Anno



Mon, 24 Mar 1997 01:20:20 GMT  
 Newbie: does an array contain a string


Quote:
>5.5) How can I tell whether an array contains a certain element?

>    There are several ways to approach this.  If you are going to make
>    this query many times and the values are arbitrary strings, the
>    fastest way is probably to invert the original array and keep an
>    associative array lying about whose keys are the first array's values.

I don't care whose keys are the first array's values, but I would
never lie about it.

Anno



Mon, 24 Mar 1997 01:32:26 GMT  
 Newbie: does an array contain a string


: Here is an easy one for a seasoned veteran.  I've already spent a good
: deal of time trying things out and just don't want to waste any more
: time.
:
: assuming:
:

:
: $astring        = some_function();
:

try this...


: #              ^^^^^^^^
: #              ^^^^^^^^
:         &do_something;
: } else {
:         &do_something_else;
: }

Hope this helps.
--
Michael Lacey of Hero Drinks Group (uk)



Wed, 26 Mar 1997 02:24:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. sort of array containing strings doesn't work

2. finding files containing string x but not string y

3. Associative array containing arrays

4. GDBM Array containing arrays as values

5. Newbie Q: Replacing a string by an array

6. newbie:? string sub through an array

7. Newbie questions: treating a scalar string as an array of chars, and the $_

8. trimming strings in an array - newbie question

9. Dealing with Strings Containing '\'s

10. Searching a file for a string containing ++

11. Replacing text strings contains more than 1 dot

12. String containing $ problem

 

 
Powered by phpBB® Forum Software