Generate dictionary with the help of two array

Discussion in 'C++' started by IPhone Lover, Sep 15, 2010.

  1. IPhone Lover

    IPhone Lover Guest

    Hi Folks

    I have two array

    keyArr = [@"name",@"city",@"street"]
    valueArr = [@"Bob",@"Newyork",@"dallas"]

    addDict = {@"name":mad:"Bob",@"city":mad:"Newyork",@"street":mad:"dallas"}

    Thanks for your all help..
     
    IPhone Lover, Sep 15, 2010
    #1
    1. Advertising

  2. IPhone Lover <>, on 15/09/2010 10:35:03, wrote:

    [subject is:
    "Generate dictionary with the help of two array"
    please state your questions in the body of your message]

    > Hi Folks
    >
    > I have two array
    >
    > keyArr = [@"name",@"city",@"street"]
    > valueArr = [@"Bob",@"Newyork",@"dallas"]
    >
    > addDict = {@"name":mad:"Bob",@"city":mad:"Newyork",@"street":mad:"dallas"}


    What language is that? This is comp.lang.c++, I hope you noticed it ;-)

    There are several different ways to implement (something like) the above
    in C++, assuming you're really interested in this language... are you?

    In such a case, please describe with greater detail the target you're
    aiming to.

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Sep 15, 2010
    #2
    1. Advertising

  3. * IPhone Lover, on 15.09.2010 19:35:
    >
    > I have two array
    >
    > keyArr = [@"name",@"city",@"street"]
    > valueArr = [@"Bob",@"Newyork",@"dallas"]
    >
    > addDict = {@"name":mad:"Bob",@"city":mad:"Newyork",@"street":mad:"dallas"}


    What programming language is this?

    It looks a little like Python, but the at signs are not Python.

    I suggest posting in a group dedicated to your programming language.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Sep 15, 2010
    #3
  4. IPhone Lover

    IPhone Lover Guest

    On Sep 15, 10:47 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * IPhone Lover, on 15.09.2010 19:35:
    >
    >
    >
    > > I have two array

    >
    > > keyArr = [@"name",@"city",@"street"]
    > > valueArr = [@"Bob",@"Newyork",@"dallas"]

    >
    > > addDict = {@"name":mad:"Bob",@"city":mad:"Newyork",@"street":mad:"dallas"}

    >
    > What programming language is this?
    >
    > It looks a little like Python, but the at signs are not Python.
    >
    > I suggest posting in a group dedicated to your programming language.
    >
    > Cheers & hth.,
    >
    > - Alf
    >
    > --
    > blog at <url:http://alfps.wordpress.com>


    Sorry to all of you messed up, but language does not bound the logic.
    I just mention '[]' instead of '()' to declare array.

    I have solved the problem by using switch case
    int cnt;
    for(NSString* s in valueArr){
    switch(cnt++):
    case 0:
    [dict setObject:s forKey:[keyArr objectAtIndex:0]
    as on
     
    IPhone Lover, Sep 16, 2010
    #4
  5. IPhone Lover <>, on 16/09/2010 01:17:36, wrote:

    > On Sep 15, 10:47 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > wrote:
    >> * IPhone Lover, on 15.09.2010 19:35:
    >>
    >>
    >>
    >>> I have two array

    >>
    >>> keyArr = [@"name",@"city",@"street"]
    >>> valueArr = [@"Bob",@"Newyork",@"dallas"]

    >>
    >>> addDict = {@"name":mad:"Bob",@"city":mad:"Newyork",@"street":mad:"dallas"}

    >>
    >> What programming language is this?
    >>
    >> It looks a little like Python, but the at signs are not Python.
    >>
    >> I suggest posting in a group dedicated to your programming language.
    >>
    >> Cheers& hth.,
    >>
    >> - Alf
    >>
    >> --
    >> blog at<url:http://alfps.wordpress.com>

    >
    > Sorry to all of you messed up, but language does not bound the logic.
    > I just mention '[]' instead of '()' to declare array.
    >
    > I have solved the problem by using switch case
    > int cnt;
    > for(NSString* s in valueArr){
    > switch(cnt++):
    > case 0:
    > [dict setObject:s forKey:[keyArr objectAtIndex:0]
    > as on


    Uh... is that an incomplete post or what?

    In any case, yes, the language does not bound the logic, but it can be
    tricky to understand what exactly you're aiming to if we happen to
    ignore the details of the language you're using... maybe you can express
    it more clearly in pseudo-code or in plain English.

    Here is an example about using std::map to create something along the
    lines of your request, this is just one of the possible approaches and
    even within the scope of this approach, the steps can be achieved in a
    number of different manners.

    Get a good C++ manual and don't forget to refer to the C++ FAQ available at:

    http://www.parashift.com/c -faq

    Good luck learning C++!

    //-------
    #include <iostream>
    #include <string>
    #include <map>

    using namespace std;

    struct address {
    string city;
    string street;
    };

    int main() {
    // create the database
    // first: string (name, the key of the map)
    // second: address (the data associated to the key)
    map<string, address> db;

    // insert data into the database

    // one approach
    // add and set the record directly
    db["Smith, John"].city = "New York";
    db["Smith, John"].street = "42th st.";

    // another approach
    // create a record
    // and assign it to a key
    address a = { "Roma", "Via dei Fori Imperiali" };
    db["Rossi, Mario"] = a;

    // print the database
    map<string, address>::iterator it;
    for(it = db.begin(); it != db.end(); ++it) {
    // one approach
    // access to the data
    // via iterator members
    cout << "Name: " << it->first << endl;
    cout << "City: " << it->second.city << endl;
    cout << "Street: " << it->second.street << endl;
    cout << "---" << endl;

    // another approach
    // access to the data
    // creating references
    const string& name = it->first;
    address& addr = it->second;
    cout << "Nome: " << name << endl;
    cout << "Citta': " << addr.city << endl;
    cout << "Strada: " << addr.street << endl;
    cout << "---" << endl;
    }
    }
    //-------

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Sep 16, 2010
    #5
  6. IPhone Lover

    IPhone Lover Guest

    On Sep 16, 3:50 pm, "Francesco S. Carta" <> wrote:
    > IPhone Lover <>, on 16/09/2010 01:17:36, wrote:
    >
    >
    >
    >
    >
    > > On Sep 15, 10:47 pm, "Alf P. Steinbach /Usenet"<alf.p.steinbach
    > > >  wrote:
    > >> * IPhone Lover, on 15.09.2010 19:35:

    >
    > >>> I have two array

    >
    > >>> keyArr = [@"name",@"city",@"street"]
    > >>> valueArr = [@"Bob",@"Newyork",@"dallas"]

    >
    > >>> addDict = {@"name":mad:"Bob",@"city":mad:"Newyork",@"street":mad:"dallas"}

    >
    > >> What programming language is this?

    >
    > >> It looks a little like Python, but the at signs are not Python.

    >
    > >> I suggest posting in a group dedicated to your programming language.

    >
    > >> Cheers&  hth.,

    >
    > >> - Alf

    >
    > >> --
    > >> blog at<url:http://alfps.wordpress.com>

    >
    > > Sorry to all of you messed up, but language does not bound the logic.
    > > I just mention '[]' instead of '()' to declare array.

    >
    > > I have solved the problem by using switch case
    > > int cnt;
    > > for(NSString* s in valueArr){
    > >     switch(cnt++):
    > >       case 0:
    > >           [dict setObject:s forKey:[keyArr objectAtIndex:0]
    > >      as on

    >
    > Uh... is that an incomplete post or what?
    >
    > In any case, yes, the language does not bound the logic, but it can be
    > tricky to understand what exactly you're aiming to if we happen to
    > ignore the details of the language you're using... maybe you can express
    > it more clearly in pseudo-code or in plain English.
    >
    > Here is an example about using std::map to create something along the
    > lines of your request, this is just one of the possible approaches and
    > even within the scope of this approach, the steps can be achieved in a
    > number of different manners.
    >
    > Get a good C++ manual and don't forget to refer to the C++ FAQ available at:
    >
    > http://www.parashift.com/c -faq
    >
    > Good luck learning C++!
    >
    > //-------
    > #include <iostream>
    > #include <string>
    > #include <map>
    >
    > using namespace std;
    >
    > struct address {
    >      string city;
    >      string street;
    >
    > };
    >
    > int main() {
    >      // create the database
    >      // first: string (name, the key of the map)
    >      // second: address (the data associated to the key)
    >      map<string, address> db;
    >
    >      // insert data into the database
    >
    >      // one approach
    >      // add and set the record directly
    >      db["Smith, John"].city = "New York";
    >      db["Smith, John"].street = "42th st.";
    >
    >      // another approach
    >      // create a record
    >      // and assign it to a key
    >      address a = { "Roma", "Via dei Fori Imperiali" };
    >      db["Rossi, Mario"] = a;
    >
    >      // print the database
    >      map<string, address>::iterator it;
    >      for(it = db.begin(); it != db.end(); ++it) {
    >          // one approach
    >          // access to the data
    >          // via iterator members
    >          cout << "Name: " << it->first << endl;
    >          cout << "City: " << it->second.city << endl;
    >          cout << "Street: " << it->second.street << endl;
    >          cout << "---" << endl;
    >
    >          // another approach
    >          // access to the data
    >          // creating references
    >          const string& name = it->first;
    >          address& addr = it->second;
    >          cout << "Nome: " << name << endl;
    >          cout << "Citta': " << addr.city << endl;
    >          cout << "Strada: " << addr.street << endl;
    >          cout << "---" << endl;
    >      }}
    >
    > //-------
    >
    > --
    >   FSC -http://userscripts.org/scripts/show/59948
    >  http://fscode.altervista.org-http://sardinias.com


    Waa.o great explanation. first of all i appreciate for the link which
    you refer me. that's really helpful.
    Thanks Francesco
     
    IPhone Lover, Sep 16, 2010
    #6
  7. IPhone Lover <>, on 16/09/2010 09:25:49, wrote:

    > On Sep 16, 3:50 pm, "Francesco S. Carta"<> wrote:


    <large snip>

    >> db["Smith, John"].street = "42th st.";


    Heck, make that "42nd" ;-)

    <another large snip>

    > Waa.o great explanation. first of all i appreciate for the link which
    > you refer me. that's really helpful.
    > Thanks Francesco


    You're welcome :)

    --
    FSC - http://userscripts.org/scripts/show/59948
    http://fscode.altervista.org - http://sardinias.com
     
    Francesco S. Carta, Sep 16, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    6
    Views:
    454
    Ilias Lazaridis
    Feb 21, 2006
  2. spidey12345
    Replies:
    6
    Views:
    937
    Thomas Fritsch
    Feb 8, 2007
  3. james_027
    Replies:
    1
    Views:
    338
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Olumide
    Replies:
    6
    Views:
    753
    John Bode
    May 17, 2010
  5. Jaewoong kim Crossbreeze
    Replies:
    1
    Views:
    133
    Jaewoong kim Crossbreeze
    Apr 17, 2009
Loading...

Share This Page