Re: how to test if an element belongs to an array or hash



biomahui@xxxxxxxxx wrote:

If I don't use iteration, how to test if an element belongs to an array
or hash?

Although there is a possible interpretation to the above question I very much doubt that it is really the question you really inteded to ask.
Please define the terms "an element" and "belongs an array or hash".


You can find out if a _string_ exists as a _key_ in a hash using exists($my_hash{$the_string})

There is no direct way to see if a string appears in an array or in the values of a hash - you need to use a loop or build a(nother) hash (for details see FAQ).

Given a scalar variable it is only possbile to find out if that scalar is part of some known array or hash using a loop (a bit like the one in the FAQ but with a couple of backslashes).

To answer the question you actually asked...

There is (to all normal intents and purposes) no way given a scalar variable to know if that scalar is an value in some unknown array or hash.

my $foo = 1;
my @bar = (2);

for my $element ( $foo, @bar ) {
   print "The value is $element and it is ";
   print "not " unless some_impossible_function($element);
   print "an array element\n";
}

If some_impossible_function() could be written this would say:

The value is 1 and it is not an array element
The value is 2 and it is an array element

.



Relevant Pages

  • Duck Typed Concepts for Ruby (was Re: A use case for an ordered hash)
    ... An Sequencable mixin can be defined that implements all sorts of operations such as append, concat, splice, sort, etc. ... extending an instance of Array with Sorted if the array is known to be sorted. ... Now returning to the thread at hand we can see that the difference between the associative array and hash hierarchies is that the hash hierarchy depends upon Hashable keys. ...
    (comp.lang.ruby)
  • Re: Suggestions for double-hashing scheme
    ... >> The items that are being moved are the items in the hash table itself, ... >> which are of fixed size (they are in an array, ... > typedef struct { ... One "uchar" aka 'unsigned char' is plenty to hold a probe ...
    (comp.programming)
  • [SUMMARY] Index and Query (#54)
    ... This was a fun quiz for me because I really don't know anything about indexing ... We see in initializethat the index is just a Hash. ... an Array of symbolic document names where the word can be found ). ...
    (comp.lang.ruby)
  • Re: Comment on how to uniquely track your objects in C# / hash table / get hash code
    ... Array, correct? ... This is largely irrelevant to the issue of performance, since hash ... where both insertions and lookups happen frequently, ... about fast lookups for balanced red/black binary trees. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: problem with hash & sort array
    ... Then I put the hash into an array with the sort ... The twist is I have to identify the duplicate ... a real data structure, and in the next place I make the exact opposite ...
    (comp.lang.perl.misc)