bash: string to integer conversion

Bash arrays are odd, globally scoped critters which can only be indexed by integer values… but the values don’t have to be contiguous so they are sort of like hash maps…

All you need is something to convert strings to integers, for example:

_map_string_to_integer() {
	echo ${*} | od -t u1 | sed 's,[^ ]*,,;s, ,,g;' | tr -d '\n'
	echo
}

And you can use it like this:

% fun[$(_map_string_to_integer this)]="is neat"
% echo ${fun[$(_map_string_to_integer this)]}
is neat

if that is of any use to anyone….

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: