Help teach MagiNinjA how to use maps!

MagiNinjA

New member
Here is some code of a script I wrote. It just takes in a string and replaces certain values with other values for URL building.

Code:
string messageReplace(string s)
{
	s = replace_string(s, "%", "%25");
	s = replace_string(s, "~", "%7E");
	s = replace_string(s, "`", "%60");
	s = replace_string(s, "!", "%21");
	s = replace_string(s, "@", "%40");
	s = replace_string(s, "#", "%23");
	s = replace_string(s, "$", "%24");
	s = replace_string(s, "^", "%5E");
	s = replace_string(s, "&", "%26");
	s = replace_string(s, "(", "%28");
	s = replace_string(s, ")", "%29");
	s = replace_string(s, "+", "%2B");
	s = replace_string(s, "=", "%3D");
	s = replace_string(s, "{", "%7B");
	s = replace_string(s, "}", "%7D");
	s = replace_string(s, "[", "%5B");
	s = replace_string(s, "]", "%5D");
	s = replace_string(s, "\\", "%5C");
	s = replace_string(s, "|", "%7C");
	s = replace_string(s, ":", "%3A");
	s = replace_string(s, ";", "%3B");
	s = replace_string(s, "\"", "%22");
	s = replace_string(s, "\'", "%27");
	s = replace_string(s, "<", "%3C");
	s = replace_string(s, ">", "%3E");
	s = replace_string(s, ",", "%2C");
	s = replace_string(s, "/", "%2F");
	s = replace_string(s, "?", "%3F");
	
	return s;
}

Now, this SCREAMS to me to use a map, but I really don't get how to use them, work with them or how to make one, for that matter. This sounds a bit way too n00bish, like, this guy should look it up! But even after taking a look in several scripts and in Veracity's tutorial, I'm still decently lost.

In a function like the above, how would a map work?

Also, would s = s.replace_string("?", "%3F"); work for implied parameters?

Thanks!
- MN
 

holatuwol

Developer
Code:
string [string] ascii_code;

ascii_code["~"] = "%7E";
ascii_code["`"] = "%60";
ascii_code["!"] = "%21";
ascii_code["@"] = "%40";
ascii_code["#"] = "%23";
ascii_code["$"] = "%24";
ascii_code["^"] = "%5E";
ascii_code["&"] = "%26";
ascii_code["("] = "%28";
ascii_code[")"] = "%29";
ascii_code["+"] = "%2B";
ascii_code["= "] = "%3D";
ascii_code["{"] = "%7B";
ascii_code["}"] = "%7D";
ascii_code["["] = "%5B";
ascii_code["]"] = "%5D";
ascii_code["\\"] = "%5C";
ascii_code["|"] = "%7C";
ascii_code[":"] = "%3A";
ascii_code[";"] = "%3B";
ascii_code["\""] = "%22";
ascii_code["\'"] = "%27";
ascii_code["<"] = "%3C";
ascii_code[">"] = "%3E";
ascii_code[","] = "%2C";
ascii_code["/"] = "%2F";
ascii_code["?"] = "%3F";

string url_encode(string s)
{
	s = s.replace_string( "%", "%25" );

	foreach key in ascii_code
		s = s.replace_string( key, ascii_code[key] );

	s = s.replace_string( " ", "+" );
	return s;
}

print( url_encode( "Give me 100% of everything!" ) );
 
I agree with MacMan104

For humor's sake though, This just popped into my head when looking at the OP's code:

Code:
string messageReplace(string s)
  {
  return replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(replace_string(s, "%", "%25"), "~", "%7E"), "`", "%60"), "!", "%21"), "@", "%40"), "#", "%23"), "$", "%24"), "^", "%5E"), "&", "%26"), "(", "%28"), ")", "%29"), "+", "%2B"), "=", "%3D"), "{", "%7B"), "}", "%7D"), "[", "%5B"), "]", "%5D"), "\\", "%5C"), "|", "%7C"), ":", "%3A"), ";", "%3B"), "\"", "%22"), "\'", "%27"), "<", "%3C"), ">", "%3E"), ",", "%2C"), "/", "%2F"), "?", "%3F");
  }

For character count and line count it's the shortest way to do it yet (other than just using the built in function). It may take less processor cycles too because of less copying of the string back to the variable then passing it to the function (the string is passed directly to the next function).

In many cases where a map can be used, it only makes the code more complex than it needs to be. This I think is one of those cases. Yes, what I put in above is way more complex than it need be too, but what I am asking is "is it really worth the effort in this case (if the functionality wasn't built in) to convert that code to using a map"?

I will add this note so that this entire post doesn't seem to turn into a threadjack: Part of knowing how to write maps is knowing when to write maps. In the question code (from MagiNinjA), it is shorter than the answer from Holatuwol (both line count and char count), seemed overall more simple, and easier to read.
 

MagiNinjA

New member
Ah, thank you! That makes much sense now.

efilnikufecin: ;D Love it.

and thank you for the url_encode and url_decode functions, macman104! I didn't see those before.
 
Top