PDA

View Full Version : Multi-Content Key+Variable Arrays



LuckY
07-19-2005, 04:22 PM
I'm working on an array class that is based on my Putty class which provides objects that can be assigned any type. The reason I started the Putty class is because I challenged myself to create something like PHP variables. Obviously, nothing exactly equivalent is possible in C++, but I'm trying to come as close as possible. Any way, I started writing this array class yesterday and it's in a working state (see below), but I thought I'd come to my coding home away from home for some feedback from you fine folks. In the code below, I'm demonstrating most of the functionality of the array class as it exists today, but I started wondering to myself if such a class (as neato mosquito as it is to me) would even be of much use to someone. It will definitely will come in handy for parsing strings or delimited files, but... I don't know... What are your thoughts? Does something like this seem like something you might even consider using? Are there any features that you think should be included? One of the problems with this class (and it's intrinsic to C++) is that you cannot arbitrarily receive back the internally-represented type. In other words, if you had an array with various types/values such as:
arr = { 100, "ABC", 9.9 }
for example, you would only be able to retrieve one of the elements as it's true type if you typecast it. This is not a problem if you are dealing with known fields and you've defined the type for each field, but you cannot just write something like:
string str = arr[0];
where you are simply guessing at what might be in the field.

Please, share your thoughts.


#include <iostream>
#include <string>
#include "Putty.h"
#include "array.h"
using namespace std;
using namespace putty;

int main() {
// emulate reading a line of text from a file
string user_data = "Griffin, Peter, (213)555-1212, 42, Quahog, Rhode Island";
string field_keys = "last, first, phone, age, city, state";
string delimiters = ",";
array arr;

// similar to explode, but assigned with user-defined keys in user-specified order
arr.parse_string(user_data, delimiters, field_keys);

// the contents from the call to parse_string persist
cout << "Parsed String (user-specified keys & order)...\n";
cout << arr << endl;

// similar to implode, but order fields using user-specified keys
cout << "\nJoined Fields (user-specified keys & order)...\n";
cout << arr.join_fields("|", delimiters, field_keys) << endl;

// explode fields into an array, with default of sequential, numeric keys
cout << "\nExploding (default keys; ordered by fields)...\n";
cout << explode(delimiters, user_data) << endl;

// implode array into a colon-delimited string
cout << "\nImploding (ordered by keys)...\n";
cout << implode(":", arr) << endl;

// empty the array
arr.clear();

// any type may be used as a key or a value
cout << "\nAny type of key with any type of value:\n";
arr[1024] = "One Kilobyte";
arr["MB"] = 10240;
arr[3.14159265] = "PI";
arr[99] = 100;
arr[true] = "Yes";
arr[false] = true;
arr['X'] = "ecks";
cout << arr << endl;

return 0;
}

Parsed String (user-specified keys & order)...
{
age => 42,
city => Quahog,
first => Peter,
last => Griffin,
phone => (213)555-1212,
state => Rhode Island
}

Joined Fields (user-specified keys & order)...
Griffin|Peter|(213)555-1212|42|Quahog|Rhode Island

Exploding (default keys; ordered by fields)...
{
0 => Griffin,
1 => Peter,
2 => (213)555-1212,
3 => 42,
4 => Quahog,
5 => Rhode Island
}

Imploding (ordered by keys)...
42:Quahog:Peter:Griffin:(213)555-1212:Rhode Island

Any type of key with any type of value:
{
X => ecks,
0 => 1,
1 => Yes,
99 => 100,
3.14159 => PI,
MB => 10240,
1024 => One Kilobyte
}

Fordy
07-20-2005, 04:36 AM
You might want to look at boost::any

http://www.boost.org/doc/html/any.html

LuckY
07-20-2005, 08:55 AM
Thanks for the link, Fordy, however I am familiar with boost's any class. What I am most curious about at the moment is the array class I mentioned/demonstrated above. It works like a PHP array and I've even included implode() and explode() functions (I will likely "borrow" a bit more functionality). Have you any ideas, comments, or suggestions for such a thing? Would you imagine it could prove useful in any respect or on any level?