entropia has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i have a program in perl that is a little slow that uses indexed arrays like this:
undef %index_array; $index_array["hola"]="mundo"; $index_array["hello"] = "world";
does someone traslade a indexed array to c language, i canīt.

Replies are listed 'Best First'.
Re: INDEXED ARRAYS ON C?
by Zaxo (Archbishop) on Feb 11, 2002 at 10:58 UTC

    You need to use curlies for that. The type you are using is called a hash. Perl arrays use square braces, but require integers to index them, just as C does. $index_hash{"hola"}="mundo"; $index_hash{"hello"} = "world"; There is no corresponding type in C, though C++ std::map is close.

    Please take a few minutes to look at the Perl Monks Site FAQ, you'll find out how to format your questions more effectively. Code tags are good to know about.

    After Compline,
    Zaxo

Re: INDEXED ARRAYS ON C?
by derby (Abbot) on Feb 11, 2002 at 11:35 UTC
    entropia,

    In addition to what Zaxo tells you, you could also do a freshmeat search for "hash."

    Just a quick vocabulary session - an indexed array is that data structure which is directly accessed by its position within the array (by an integer - array[0] array[1] etc). an associatve array, or hash or key-value pair, is that data structure which is directly (or indirectly but definetly position independent) accessed by its "key" (by a string - hash{"monks"}).

    If you go the freshmeat route, search for "hash" be sure to seperate the hash algorithm stuff (MD5, SHA-1, shash, etc) from the hash data structure stuff (glibc, libdict, ght, etc) - they're slightly related but different beasties.

    -derby