Multi-Content Key+Variable Arrays

This is a discussion on Multi-Content Key+Variable Arrays within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I'm working on an array class that is based on my Putty class which provides objects that can be assigned ...

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Multi-Content Key+Variable Arrays

    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.
    Code:
    #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;
    }
    Code:
    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
    }
    Last edited by LuckY; 07-19-2005 at 04:32 PM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You might want to look at boost::any

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

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    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?
    Last edited by LuckY; 07-20-2005 at 09:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dealing With Multi Dim Char Arrays
    By mike_g in forum C Programming
    Replies: 8
    Last Post: 06-15-2007, 01:52 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  4. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21