Jump to content

C++ reading out an unknown length array

StephanTW

 

Basicly I am wondering if there is a way to read out an array that looks like this:

 

[["rhs_weap_ak103","rhs_acc_dtk2","rhs_acc_2dpZenit","rhs_acc_1p29",["rhs_30Rnd_762x39mm_polymer",30],[],""],["rhs_weap_rpg7","","","rhs_acc_pgo7v3",["rhs_rpg7_PG7VS_mag",1],[],""],["rhs_weap_pya","","","",["rhs_mag_9x19_17",17],[],""],["rhs_uniform_flora_patchless",[["rhs_30Rnd_762x39mm_bakelite_U",1,30],["ACE_morphine",1]]],["rhs_6b23_6sh92_headset",[["rhs_30Rnd_762x39mm",1,30],["ACE_morphine",1]]],["rhs_sidor",[["rhs_30Rnd_762x39mm",1,30],["ACE_morphine",1]]],"rhs_6b47_bala","rhsusf_oakley_goggles_clr",["rhssaf_zrak_rd7j","","","",[],[],""],["ItemMap","","TFAR_fadak","ItemCompass","ItemWatch","rhs_1PN138"]]


where the only bit of the multi level array that can be almost any length is

["rhs_30Rnd_762x39mm",1,30],["ACE_morphine",1]

in the 3 locations it is mentioned in the first code bit.
I am a complete beginner with coding and haven't found anything similar, so maybe I am just looking for something that can't be done .
and for those wondering its a load-out export for arma 3.
 

RAM 32 GB of Corsair DDR4 3200Mhz            MOTHERBOARD ASUS ROG Crosshair VIII Dark Hero
CPU Ryzen 9 5950X             GPU dual r9 290's        COOLING custom water loop using EKWB blocks
STORAGE samsung 970 EVo plus 2Tb Nvme, Samsung 850 EVO 512GB, WD Red 1TB,  Seagate 4 TB and Seagate Exos X18 18TB

Psu Corsair AX1200i
MICROPHONE RODE NT1-A          HEADPHONES Massdrop & Sennheiser HD6xx
MIXER inkel mx-1100   peripherals Corsair k-95 (the og 18G keys one)  and a Corsair scimitar

Link to comment
Share on other sites

Link to post
Share on other sites

Is this array data being read from an interface of is it literally the above array as a string from a file?

Link to comment
Share on other sites

Link to post
Share on other sites

41 minutes ago, StephanTW said:

Basicly I am wondering if there is a way to read out an array that looks like this:

I am a complete beginner with coding and haven't found anything similar, so maybe I am just looking for something that can't be done .
and for those wondering its a load-out export for arma 3.
 

There are a few ways you could handle this. Some really bizarre regex is one of them. What I would do is the old C way, but using some new C++ features (e.g. stringstream):

// this isn't real code, just the idea

char gigantic_string_from_arma = "[["rhs_weap_ak103","rhs_acc_dtk2","rhs_acc_2dpZenit","rhs_acc_1p29",["rhs_30Rnd_762x39mm_polymer",30],[],""],["rhs_weap_rpg7","","","rhs_acc_pgo7v3",["rhs_rpg7_PG7VS_mag",1],[],""],["rhs_weap_pya","","","",["rhs_mag_9x19_17",17],[],""],["rhs_uniform_flora_patchless",[["rhs_30Rnd_762x39mm_bakelite_U",1,30],["ACE_morphine",1]]],["rhs_6b23_6sh92_headset",[["rhs_30Rnd_762x39mm",1,30],["ACE_morphine",1]]],["rhs_sidor",[["rhs_30Rnd_762x39mm",1,30],["ACE_morphine",1]]],"rhs_6b47_bala","rhsusf_oakley_goggles_clr",["rhssaf_zrak_rd7j","","","",[],[],""],["ItemMap","","TFAR_fadak","ItemCompass","ItemWatch","rhs_1PN138"]]";

unsigned position = 0;
unsigned stack = 0;
bool is_string = false;
std::stringstream temp_str;
std::string str;
int num;

while (position != gigantic_string_from_arma.size());
{
  char c = gigantic_string_from_arm[position]; // get each character
  switch(c)
  {
  	case "[": //if '[', a new list is starting, so increment the stack/depth variable
    	stack++;
        break;
    case "]": // if ']', an open list is ending, so decrement the stack/depth variable
    	stack--;
        break;
    case "\"": // if an escaped double quote appears, it can be either the beginning or the end of a string
      {
          if (is_string) // is_string keeps track if the current character is part of a string
          {
              str = temp_string.str(); // get final string from stringstream (e.g. rhs_weap_ak103)
              temp_string.clear(); 
              //here you have the strings that you want to save and the depth of them in the stack/structure
          }
          is_string = !is_string; // invert is_string, to indicate when the string started and later when it ended
      }
      break;
    case ",": // commas separate different types of values
    	{
	    if (temp_string.size() > 0) // if the current character was part of a string, it should have matched an escaped double quote and temp_string been cleared at this point, so this is definitely not a string
            {
            	num = strtoi(temp_string.str()); // convert temp_string to an integer
                temp_string.clear();
                //here you have the number that you want to save and the depth of them in the stack/structure
            }
        }
    	break;
    default: // when the character is not a token
    	temp_string << c; // write char to string
  }
  postion++; // moves the offset of the gigantic_string to the next character
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, kurapika said:

Is this array data being read from an interface of is it literally the above array as a string from a file?

probs should have stated, but the array I export from the game and paste into the c++ file

RAM 32 GB of Corsair DDR4 3200Mhz            MOTHERBOARD ASUS ROG Crosshair VIII Dark Hero
CPU Ryzen 9 5950X             GPU dual r9 290's        COOLING custom water loop using EKWB blocks
STORAGE samsung 970 EVo plus 2Tb Nvme, Samsung 850 EVO 512GB, WD Red 1TB,  Seagate 4 TB and Seagate Exos X18 18TB

Psu Corsair AX1200i
MICROPHONE RODE NT1-A          HEADPHONES Massdrop & Sennheiser HD6xx
MIXER inkel mx-1100   peripherals Corsair k-95 (the og 18G keys one)  and a Corsair scimitar

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, StephanTW said:

probs should have stated, but the array I export from the game and paste into the c++ file

Okay, so basically you need some kind of deserialization to map the 'text' data into an actual C++ data structure. But to be honest: this is quite an advanced task for a complete beginner, especially when using C++.

You may get faster on the tracks using a scripting language at this point (except you're explicitly learning C++, then you should read something about on how C++ JSON deserializer/serializer work, but again: this is no easy stuff for beginners.).

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Forbidden Wafer said:

There are a few ways you could handle this. Some really bizarre regex is one of them. What I would do is the old C way, but using some new C++ features (e.g. stringstream):

 

okay, I see what you did there, but i don't suppose you'd be able to then assign the outputs as a letter or letter-number combo.

mhh what I am trying to basicly do is have it read it out and then put the items in a certain place in the arma 3 unit config layout...
Whats the max code length for this subforum, though 2k lines even for me feels too big for here

 

RAM 32 GB of Corsair DDR4 3200Mhz            MOTHERBOARD ASUS ROG Crosshair VIII Dark Hero
CPU Ryzen 9 5950X             GPU dual r9 290's        COOLING custom water loop using EKWB blocks
STORAGE samsung 970 EVo plus 2Tb Nvme, Samsung 850 EVO 512GB, WD Red 1TB,  Seagate 4 TB and Seagate Exos X18 18TB

Psu Corsair AX1200i
MICROPHONE RODE NT1-A          HEADPHONES Massdrop & Sennheiser HD6xx
MIXER inkel mx-1100   peripherals Corsair k-95 (the og 18G keys one)  and a Corsair scimitar

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, StephanTW said:

okay, I see what you did there, but i don't suppose you'd be able to then assign the outputs as a letter or letter-number combo.

mhh what I am trying to basicly do is have it read it out and then put the items in a certain place in the arma 3 unit config layout...
Whats the max code length for this subforum, though 2k lines even for me feels too big for here

 

Use GitHub Gists

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, kurapika said:

Okay, so basically you need some kind of deserialization to map the 'text' data into an actual C++ data structure. But to be honest: this is quite an advanced task for a complete beginner, especially when using C++.

You may get faster on the tracks using a scripting language at this point (except you're explicitly learning C++, then you should read something about on how C++ JSON deserializer/serializer work, but again: this is no easy stuff for beginners.).

somehow I always manage to start with just a small thing and think you know this could be done easier without manually copy and pasting things and it always somehow leads to advanced stuff XD

RAM 32 GB of Corsair DDR4 3200Mhz            MOTHERBOARD ASUS ROG Crosshair VIII Dark Hero
CPU Ryzen 9 5950X             GPU dual r9 290's        COOLING custom water loop using EKWB blocks
STORAGE samsung 970 EVo plus 2Tb Nvme, Samsung 850 EVO 512GB, WD Red 1TB,  Seagate 4 TB and Seagate Exos X18 18TB

Psu Corsair AX1200i
MICROPHONE RODE NT1-A          HEADPHONES Massdrop & Sennheiser HD6xx
MIXER inkel mx-1100   peripherals Corsair k-95 (the og 18G keys one)  and a Corsair scimitar

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, StephanTW said:

okay, I see what you did there, but i don't suppose you'd be able to then assign the outputs as a letter or letter-number combo.

Not sure what you mean by that. You have the strings, the numbers, the order in which they appear and list depth.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, StephanTW said:

somehow I always manage to start with just a small thing and think you know this could be done easier without manually copy and pasting things and it always somehow leads to advanced stuff XD

I know exactly how you feel 😄 And it's cool to have ambitious goals. But don't surrender if it'll not work on the first (or second, or third) try 🙂

Link to comment
Share on other sites

Link to post
Share on other sites

 

3 minutes ago, Forbidden Wafer said:

Not sure what you mean by that. You have the strings, the numbers, the order in which they appear and list depth.


https://community.bistudio.com/wiki/Arma_3:_Characters_And_Gear_Encoding_Guide

 

if you scroll down to Character configuration part of the link you see that
 

weapons[] = {arifle_MX_ACO_pointer_F, hgun_P07_F, Throw, Put};	

now what i would prefer it to be is that  i could make it
 

weapons[] = {mg, sa, Throw, Put};	

where mg = arifle_MX_ACO_pointer_F and sa = hgun_P07_F
but then ofcourse the arifle_MX_ACO_pointer_F
  and hgun_P07_F would be whatever they are in the array.
if this makes sense

RAM 32 GB of Corsair DDR4 3200Mhz            MOTHERBOARD ASUS ROG Crosshair VIII Dark Hero
CPU Ryzen 9 5950X             GPU dual r9 290's        COOLING custom water loop using EKWB blocks
STORAGE samsung 970 EVo plus 2Tb Nvme, Samsung 850 EVO 512GB, WD Red 1TB,  Seagate 4 TB and Seagate Exos X18 18TB

Psu Corsair AX1200i
MICROPHONE RODE NT1-A          HEADPHONES Massdrop & Sennheiser HD6xx
MIXER inkel mx-1100   peripherals Corsair k-95 (the og 18G keys one)  and a Corsair scimitar

Link to comment
Share on other sites

Link to post
Share on other sites

This is NOT an "array".

This is a snippet of JSON, so use JSON tool to read it is the best solution.

Please see https://github.com/nlohmann/json and use json.hpp to parse it. This is called "deserialize" a json string.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×