Split json like string with C++20 ranges/views - c++
I have the following string:
std::string ob = R"({ "U" : 972448093270,"u" : 972448098626,"pu" : 972448093117,"b" : [["54019.38","0.414"],["54016.91","0.002"],["54016.80","0.180"],["54016.51","0.001"],["54012.74","0.001"],["54012.38","0.001"],["54011.65","0.044"],["54011.57","0.005"],["54011.40","0.001"],["54011.27","0.200"],["54011.26","0.200"],["54011.25","0.784"],["54011.12","0.200"],["54011.11","0.312"],["54011.04","1.920"],["54010.67","0.123"],["54010.39","0.025"],["54008.59","0.074"],["54008.58","0.167"],["54008.06","0.010"]]})";
Starting from "b" I want all the numbers split and joined in a vector, so effectively I want my vector to look like this:
54019.38 0.414 54016.91 etc
I have implemented the followint with C++20 ranges/views
#include <iostream>
#include <vector>
#include <ranges>
auto to_vector(auto&& rng)
{
std::vector<std::ranges::range_value_t<decltype(rng)>> v;
if constexpr (std::ranges::sized_range<decltype(rng)>) {
v.reserve(std::ranges::size(rng));
}
std::ranges::copy(rng, std::back_inserter(v));
return v;
}
int main()
{
std::string ob = R"({ "U" : 972448093270,"u" : 972448098626,"pu" : 972448093117,"b" : [["54019.38","0.414"],["54016.91","0.002"],["54016.80","0.180"],["54016.51","0.001"],["54012.74","0.001"],["54012.38","0.001"],["54011.65","0.044"],["54011.57","0.005"],["54011.40","0.001"],["54011.27","0.200"],["54011.26","0.200"],["54011.25","0.784"],["54011.12","0.200"],["54011.11","0.312"],["54011.04","1.920"],["54010.67","0.123"],["54010.39","0.025"],["54008.59","0.074"],["54008.58","0.167"],["54008.06","0.010"]]})";
auto digits = ob | std::views::drop_while([](auto i) {return i != 'b'; }) | std::views::split('"') | std::views::join;
auto vec = to_vector(digits);
for (auto const& item : vec) {
std::cout << item <<" ";
}
}
Unfortunately instead of expected output I got
`b : [ [ 5 4 0 1 9 . 3 8 , 0 . 4 1 4 ] , [ 5 4 0 1 6 . 9 1 , 0 . 0 0 2 ] , [ 5 4 0 1 6 . 8 0 , 0 . 1 8 0 ] , [ 5 4 0 1 6 . 5 1 , 0 . 0 0 1 ] , [ 5 4 0 1 2 . 7 4 , 0 . 0 0 1 ] , [ 5 4 0 1 2 . 3 8 , 0 . 0 0 1 ] , [ 5 4 0 1 1 . 6 5 , 0 . 0 4 4 ] ...etc
So I have each character in a vector individually instead of splitting whatever was between "'s
Can someone please help me fix this, thanks in advance.
Related
Create duplicate rows in SAS and change values of variables
I have been so confused on how to implement this in SAS. I am trying to create duplicate rows if the value of "2" occurs more than once between the variables (member1 -member4). For example, if a row has the value 2 in member2, member3, and member4, then I will create 2 duplicate rows since the initial row will serve for the first variable and the duplicate rows will be for member 3 and 4. On the duplicate row for member3 for example, member 2 and 4 will be missing if their values is equal to 2. Basically the value "2" can only occur once per row. let's assume sa1 to sa4 corresponds to other variables of member1 to member4 respectively. When we create a duplicate row for each member, the other variables should be missing if they have a value of "1". For example, if the duplicate row is for member 3, then values that equal "1" for sa1, sa2 and sa4 should be set to missing. There are other variables in the dataset that will have same values for all duplicate rows as initial rows. Duplicate rows will also have a suffix for the ID to indicate the parent rows. This is an example of the data I have id member1 member2 member3 member4 sa1 sa2 sa3 sa4 1 0 2 2 0 0 1 1 0 2 2 2 0 5 . 1 0 0 3 2 2 3 2 1 1 0 1 Then this is the output I am trying to achieve id member1 member2 member3 member4 sa1 sa2 sa3 sa4 1 0 2 . 0 0 1 . 0 1_1 0 . 2 0 0 . 1 0 2 2 . 0 5 . . 0 0 2_1 . 2 0 5 . 1 0 0 3 2 . 3 . 1 . 0 . 3_1 . 2 3 . . 1 0 . 3_2 . . 3 2 . . 0 1 Will appreciate any help. Thank you!
You need to count the number of '2's. You also need to remember where they used to be. "I had the spots removed for good luck, but I remember where the spots formerly were." data have ; input id :$10. member1 member2 member3 member4 sa1 sa2 sa3 sa4 ; cards; 1 0 2 2 0 0 1 1 0 2 2 2 0 5 . 1 0 0 3 2 2 3 2 1 1 0 1 4 2 0 0 0 . . . . 5 0 0 0 0 . . . . ; data want ; set have ; array m member1-member4 ; array x [4] _temporary_; do index=1 to dim(m); x[index]=m[index]=2; end; n2 = sum(of x[*]); if n2<2 then output; else do counter=1 to n2; id=scan(id,1,'_'); if counter > 1 then id=catx('_',id,counter-1); counter2=0; do index=1 to dim(m); if x[index] then do; counter2+1; if counter = counter2 then m[index]=2; else m[index]=.; end; end; output; end; drop index n2 counter counter2; run; Results Obs id member1 member2 member3 member4 sa1 sa2 sa3 sa4 1 1 0 2 . 0 0 1 1 0 2 1_1 0 . 2 0 0 1 1 0 3 2 2 . 0 5 . 1 0 0 4 2_1 . 2 0 5 . 1 0 0 5 3 2 . 3 . 1 1 0 1 6 3_1 . 2 3 . 1 1 0 1 7 3_2 . . 3 2 1 1 0 1 8 4 2 0 0 0 . . . . 9 5 0 0 0 0 . . . .
I think your expecting us to code the whole thing for you... I dont get your logic explanation of what you want - but to start off with: create a new dataset rename all the variables on the way in - prefix with O_ (Original) code however you like to see how many values contain 2 (HOWMANYTWOS) do ROW = 1 to HOWMANYTWOS 4.1 again go through the values on the O_ variables you have 4.2 if the ROW - corresponds to your increasing counter its the 2 you wish to keep and so you dont touch it - if the 2 does not correspond to your ROW - make it . 4.3 output the record with a new(if required) ID a start for you: data NEW; set ORIG (rename=(MEMBER1-MEMBER4=O_MEMBER1-O_MEMBER4 ID=O_ID etc..) HOWMANYTWOS = sum(O_MEMBER1=2,O_MEMBER2=2,O_MEMBER3=2,O_MEMBER4=2); do ROW = 1 to HOWMANYTWOS; /* This is stepping through and creating the new rows - you need to step through the variables to see if you want to make them null before outputting... NOTE do not change O_ variables only create/update the variables going to the output dataset (The O_ version is for checking against only) ID = ifc(ROW = 1, O_ID, catx("_", O_ID, ROW); /* create a counter output; end; run; Sorry - Not got sas here and its been a little while
Aggregate dummy variables to multiple categorical variables
I have 8 dummy variables (0/1). Those 8 variables have to be aggregated to one categorical variable with 8 items (categories). Normally, people should have just marked one out of the 8 dummy variables, but some marked multiple ones. When a Person has marked two items, the first value should go into the first categorical variable, whereas the second value should go to the second categorical variable. When there are 3 items marked, the third values should go into a third categorical variable and so on (up to 3). I know how to aggregate the dummies to a categorical variable, but I do not know which approach there is to divide the values to different variables, based on the number of marked dummies. If the problem is not clear, please tell me. It was difficult for me to describe it properly. Edit: My approach is the follwoing: local MCM_zahl4 F0801 F0802 F0803 F0804 F0805 F0806 F0807 F0808 gen MCM_zaehl_4 = 0 foreach var of varlist `MCM_zahl4' { replace MCM_zaehl_4 = MCM_zaehl_4 + 1 if `var' == 1 } tab MCM_zaehl_4 /* MCM_zaehl_4 | Freq. Percent Cum. ------------+----------------------------------- 0 | 31 4.74 4.74 1 | 598 91.44 96.18 2 | 22 3.36 99.54 3 | 3 0.46 100.00 ------------+----------------------------------- Total | 654 100.00 */ gen bildu2 = -999999 gen bildu2_D = -999999 replace bildu2 = 1 if F0801 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 2 if F0802 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 3 if F0803 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 4 if F0804 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 5 if F0805 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 6 if F0806 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 7 if F0807 == 1 & MCM_zaehl_4 == 1 replace bildu2 = 8 if F0808 == 1 & MCM_zaehl_4 == 1 Then I split all cases MCM_zaehl_4 > 1 manually in three variables. E. g. for two mcm: replace bildu2 = 5 if ID == XXX replace bildu2_D = 2 if ID == XXX For that approach I'd need an auomation, because for more observations I won't be able to do it manually.
If I understood you correctly, you could try the following to aggregate your multiples dummy variables into multiple aggregate columns based on the number of answers that the person marked. It assumes the repeated answers are consecutive. I reduced your problem to 6 dummy (a1-a6) and people can answer up to 3 questions. clear input id a1 a2 a3 a4 a5 a6 1 1 0 0 0 0 0 2 1 1 0 0 0 0 3 1 1 1 0 0 0 4 1 1 1 0 0 0 5 0 1 0 0 0 0 6 1 0 0 0 0 0 7 0 0 0 0 1 0 8 0 0 0 0 0 1 end egen n_asnwers = rowtotal(a*) gen wanted_1 = . gen wanted_2 = . gen wanted_3 = . local i = 1 foreach v of varlist a* { replace wanted_1 = `v' if `v' == 1 & n_asnwers == 1 replace wanted_2 = `v' if `v' == 1 & n_asnwers == 2 replace wanted_3 = `v' if `v' == 1 & n_asnwers == 3 local ++i } list /* +------------------------------------------------------------------------------+ | id a1 a2 a3 a4 a5 a6 n_asnw~s wanted_1 wanted_2 wanted_3 | |------------------------------------------------------------------------------| 1. | 1 1 0 0 0 0 0 1 1 . . | 2. | 2 1 1 0 0 0 0 2 . 1 . | 3. | 3 1 1 1 0 0 0 3 . . 1 | 4. | 4 1 1 1 0 0 0 3 . . 1 | 5. | 5 0 1 0 0 0 0 1 1 . . | |------------------------------------------------------------------------------| 6. | 6 1 0 0 0 0 0 1 1 . . | 7. | 7 0 0 0 0 1 0 1 1 . . | 8. | 8 0 0 0 0 0 1 1 1 . . | +------------------------------------------------------------------------------+ */
using get() and appending those chars to a string is giving me nonsense
I am supposed to use a program that (one char at a time) reads a GPS log file, and parses it. I'm getting normal responses when I use some .log files, and not with others (especially not long ones). The code that grabs the information and stores it is below: class ReadInput{ public: string massiveString; }; void ReadInput::assign(string inMassiveString){ //assign the string passed to it to the object this->massiveString.empty(); //clear whatever might be in the string this->massiveString=inMassiveString; //copy the string into where it belongs. } int main(int argc, const char * argv[]) { ifstream inputStream; inputStream.open("gps3.log"); int i = 1; char temp; string tempString; ReadInput *sentence[258]; //this is the part that grabs the characters while(1){ // if (!inputStream.eof()){ // inputStream.get(temp); if (temp!=' ') { tempString.append(&temp); //this appends the characters to a temporary string // cout << i << "\t" << tempString << endl; //testing the input } if (temp == '\n') { //if this is the end of the line, pass it on. sentence[i]=new ReadInput; sentence[i]->assign(tempString); //the part that copies the temporary string to the object cout << i << "\t" << sentence[i]->massiveString << endl; //another testing line i++; tempString.clear(); } } else{ break; } } } it is very important that I read it character by character. what it should be reading looks like this: $GPGGA,155008.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*53 and here is an example of a problematic reading. in the format of entry#, stringStoredInEntry. 1 $GPGGA,155002.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*58 2 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 3 $GPRMC,155002.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*48 4 $GPGGA,155003.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*59 5 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 6 $GPGSV,3,1,12,17,64,038,42,06,63,250,43,28,44,150,45,02,25,239,38*7B 7 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,23,51,36,223,37*71 8 F 9 $ G P R M C , 1 5 5 0 0 3 . 0 0 0 , A , 3 79 10 $ G P G G A , 1 5 5 0 0 4 . 0 0 0 , 3 7 3 2 . 7 2 3 9 , N , 0 7 7 2 6 . 9 9 5 6 , W , 1 , 0 5 , 1 . 7 , 8 9 . 1 , M , - 3 3 . 6 , M , , 0 0 0 0 * 5 E 11 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E 12 $ G P R M C , 1 5 5 0 0 4 . 0 0 0 , A , 3 7 3 2 . 7 2 3 9 , N , 0 7 7 2 6 . 9 8 $GPGGA,155009.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*52 29 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 30 $GPRMC,155009.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*43 31 $GPGGA,155010.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5A 2 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E !33 $!G!P!R!M!C!,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)3),)1)7),)0)6),)2)8),)0)2),)2)4),),),),),),),),)3).)2),)1).)7),)2).)7)*)3)E) *42 $*G*P*G*S*V*,*3*,*1*,*1*2*,*1*7*,*6*4*,*0*3*8*,*4*1*,*0*6*,*6*3*,*2*5*0*,*4*2*,*2*8*,*4*4*,*1*5*0*,*4*3*,*0*2*,*2*5*,*2*3*9*,*3*8***7*F* +43 $+G+P+G+S+V+,+3+,+2+,+1+2+,+2+4+,+1+7+,+2+9+4+,+2+3+,+1+0+,+2+1+,+1+7+6+,+2+2+,+1+2+,+2+5+,+3+1+6+,+2+4+,+5+1+,+3+6+,+2+2+3+,+3+5+*+7+5+ ,44 $,G,P,G,S,V,,,3,,,3,,,1,2,,,2,0,,,3,4,,,0,6,2,,,,,0,1,,,0,7,,,0,5,1,,,,,0,4,,,0,6,,,0,5,2,,,,,3,2,,,0,2,,,0,3,3,,,*,7,F, -45 $-G-P-R-M-C-,-1-5-5-0-1-3-.-0-0-0-,-A-,-3-7-3-2-.-7-2-3-9-,-N-,-0-7-7-2-6-.-9-9-5-6-,-W-,-0-.-0-0-,-8-0-.-3-0-,-1-4-1-0-1-4-,-,-,-A-*-4-8- .46 $.G.P.G.G.A.,.1.5.5.0.1.4...0.0.0.,.3.7.3.2...7.2.3.9.,.N.,.0.7.7.2.6...9.9.5.6.,.W.,.1.,.0.5.,.1...7.,.8.9...0.,.M.,.-.3.3...6.,.M.,.,.0.0.0.0.*.5.E. /47 $/G/P/G/S/A/,/A/,/3/,/1/7/,/0/6/,/2/8/,/0/2/,/2/4/,/,/,/,/,/,/,/,/3/./2/,/1/./7/,/2/./7/*/3/E/ 048 $0G0P0R0M0C0,0105050001040.0000000,0A0,030703020.070203090,0N0,00070702060.090905060,0W0,000.00000,08000.03000,0104010001040,0,0,0A0*040F0 149 $1G1P1G1G1A1,1115151011151.1010101,131713121.171213191,1N1,10171712161.191915161,1W1,111,10151,111.171,18191.101,1M1,1-13131.161,1M1,1,101010101*151F1 250 $2G2P2G2S2A2,2A2,232,21272,20262,22282,20222,22242,2,2,2,2,2,2,2,232.222,212.272,222.272*232E2 351 $3G3P3R3M3C3,3135353031353.3030303,3A3,333733323.373233393,3N3,30373732363.393935363,3W3,303.30303,38303.33303,3134313031343,3,3,3A3*343E3 452 $4G4P4G4G4A4,4145454041464.4040404,434743424.474243494,4N4,40474742464.494945464,4W4,414,40454,414.474,48484.494,4M4,4-43434.464,4M4,4,404040404*45444 553 $5G5P5G5S5A5,5A5,535,51575,50565,52585,50525,52545,5,5,5,5,5,5,5,535.525,515.575,525.575*535E5 654 $6G6P6R6M6C6,6165656061666.6060606,6A6,636763626.676263696,6N6,60676762666.696965666,6W6,606.60606,68606.63606,6164616061646,6,6,6A6*646D6 755 $7G7P7G7G7A7,7175757071777.7070707,737773727.777273797,7N7,70777772767.797975767,7W7,717,70757,717.777,78787.797,7M7,7-73737.767,7M7,7,707070707*75757 856 $8G8P8G8S8A8,8A8,838,81878,80868,82888,80828,82848,8,8,8,8,8,8,8,838.828,818.878,828.878*838E8 957 $9G9P9R9M9C9,9195959091979.9090909,9A9,939793929.979293999,9N9,90979792969.999995969,9W9,909.90909,98909.93909,9194919091949,9,9,9A9*949C9 :58 $:G:P:G:G:A:,:1:5:5:0:1:8:.:0:0:0:,:3:7:3:2:.:7:2:3:9:,:N:,:0:7:7:2:6:.:9:9:5:6:,:W:,:1:,:0:5:,:1:.:7:,:8:8:.:9:,:M:,:-:3:3:.:6:,:M:,:,:0:0:0:0:*:5:A: ;59 $;G;P;G;S;A;,;A;,;3;,;1;7;,;0;6;,;2;8;,;0;2;,;2;4;,;,;,;,;,;,;,;,;3;.;2;,;1;.;7;,;2;.;7;*;3;E; <60 $<G<P<G<S<V<,<3<,<1<,<1<2<,<1<7<,<6<4<,<0<3<9<,<4<2<,<0<6<,<6<4<,<2<5<0<,<4<3<,<2<8<,<4<3<,<1<5<1<,<4<2<,<0<2<,<2<5<,<2<3<9<,<3<8<*<7<C< =61 $=G=P=G=S=V=,=3=,=2=,=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,,,,,3.2,1.7,2.7*3E 34 $GPRMC,155002.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*48 25 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,24,51,36,223,35*74 46 $GPGGA,155003.000,3732.7239,N,07726.9956,W,1,05,1.7,89.1,M,-33.6,M,,0000*59 27 $ 5 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 6 $GPGSV,3,1,12,17,64,038,42,06,63,250,43,28,44,150,45,02,25,239,38*7B 7 $GPGSV,3,2,12,24,17,294,22,10,21,176,22,12,25,316,23,51,36,223,37*71 8 F 9 $ G P R M C , 1 5 5 0 0 3 . 0 0 0 , A , 3 79 10 $ G P G G A , 1 5 5 0 0 4 . 0 0 0 , 3 7 3 2 . 7 2 3 9 , N , 0 7 7 2 6 . 9 9 5 6 , W , 1 , 0 5 , 1 . 7 , 8 9 . 1 , M , - 3 3 . 6 , M , , 0 0 0 0 * 5 E 11 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E 12 $ G P R M C , 1 5 5 0 0 4 . 0 0 0 , A , 3 7 3 2 . 7 2 3 9 , N , 0 7 7 2 6 . 9 9 5 6 , W , 0 . 0 0 , 8 0 . 3 0 8 $GPGGA,155009.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*52 1 29 $GPGSA,A,3,17,06,28,02,24,,,,,,,,3.2,1.7,2.7*3E 4 1 30 $GPRMC,155009.000,A,3732.7239,N,07726.9956,W,0.00,80.30,141014,,,A*43 1 31 $GPGGA,155010.000,3732.7239,N,07726.9956,W,1,05,1.7,89.0,M,-33.6,M,,0000*5A , 2 $ G P G S A , A , 3 , 1 7 , 0 6 , 2 8 , 0 2 , 2 4 , , , , , , , , 3 . 2 , 1 . 7 , 2 . 7 * 3 E , !3 $!G!P!R!M!C!,!1!5!5!0!1!0!.!0!0!0!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),)0)6),)2)8),)0)2),)2)4),),),),),),),),)3).)2),)1).)7),)2).)7)*)3)E) ) *2 $*G*P*G*S*V*,*3*,*1*,*1*2*,*1*7*,*6*4*,*0*3*8*,*4*1*,*0*6*,*6*3*,*2*5*0*,*4*2*,*2*8*,*4*4*,*1*5*0*,*4*3*,*0*2*,*2*5*,*2*3*9*,*3*8***7*F* * +3 $+G+P+G+S+V+,+3+,+2+,+1+2+,+2+4+,+1+7+,+2+9+4+,+2+3+,+1+0+,+2+1+,+1+7+6+,+2+2+,+1+2+,+2+5+,+3+1+6+,+2+4+,+5+1+,+3+6+,+2+2+3+,+3+5+*+7+5+ + ,4 $,G,P,G,S,V,,,3,,,3,,,1,2,,,2,0,,,3,4,,,0,6,2,,,,,0,1,,,0,7,,,0,5,1,,,,,0,4,,,0,6,,,0,5,2,,,,,3,2,,,0,2,,,0,3,3,,,*,7,F, , -5 $-G-P-R-M-C-,-1-5-5-0-1-3-.-0-0-0-,-A-,-3-7-3-2-.-7-2-3-9-,-N-,-0-7-7-2-6-.-9-9-5-6-,-W-,-0-.-0-0-,-8-0-.-3-0-,-1-4-1-0-1-4-,-,-,-A-*-4-8- - .6 $.G.P.G.G.A.,.1.5.5.0.1.4...0.0.0.,.3.7.3.2...7.2.3.9.,.N.,.0.7.7.2.6...9.9.5.6.,.W.,.1.,.0.5.,.1...7.,.8.9...0.,.M.,.-.3.3...6.,.M.,.,.0.0.0.0.*.5.E. . /7 $/G/P/G/S/A/,/A/,/3/,/1/7/,/0/6/,/2/8/,/0/2/,/2/4/,/,/,/,/,/,/,/,/3/./2/,/1/./7/,/2/./7/*/3/E/ / 08 $0G0P0R0M0C0,0105050001040.0000000,0A0,030703020.070203090,0N0,00070702060.090905060,0W0,000.00000,08000.03000,0104010001040,0,0,0A0*040F0 0 19 $1G1P1G1G1A1,1115151011151.1010101,131713121.171213191,1N1,10171712161.191915161,1W1,111,10151,111.171,18191.101,1M1,1-13131.161,1M1,1,101010101*151F1 1 20 $2G2P2G2S2A2,2A2,232,21272,20262,22282,20222,22242,2,2,2,2,2,2,2,232.222,212.272,222.272*232E2 2 31 $3G3P3R3M3C3,3135353031353.3030303,3A3,333733323.373233393,3N3,30373732363.393935363,3W3,303.30303,38303.33303,3134313031343,3,3,3A3*343E3 3 42 $4G4P4G4G4A4,4145454041464.4040404,434743424.474243494,4N4,40474742464.494945464,4W4,414,40454,414.474,48484.494,4M4,4-43434.464,4M4,4,404040404*45444 4 53 $5G5P5G5S5A5,5A5,535,51575,50565,52585,50525,52545,5,5,5,5,5,5,5,535.525,515.575,525.575*535E5 5 64 $6G6P6R6M6C6,6165656061666.6060606,6A6,636763626.676263696,6N6,60676762666.696965666,6W6,606.60606,68606.63606,6164616061646,6,6,6A6*646D6 6 75 $7G7P7G7G7A7,7175757071777.7070707,737773727.777273797,7N7,70777772767.797975767,7W7,717,70757,717.777,78787.797,7M7,7-73737.767,7M7,7,707070707*75757 7 86 $8G8P8G8S8A8,8A8,838,81878,80868,82888,80828,82848,8,8,8,8,8,8,8,838.828,818.878,828.878*838E8 8 97 $9G9P9R9M9C9,9195959091979.9090909,9A9,939793929.979293999,9N9,90979792969.999995969,9W9,909.90909,98909.93909,9194919091949,9,9,9A9*949C9 9 :8 $:G:P:G:G:A:,:1:5:5:0:1:8:.:0:0:0:,:3:7:3:2:.:7:2:3:9:,:N:,:0:7:7:2:6:.:9:9:5:6:,:W:,:1:,:0:5:,:1:.:7:,:8:8:.:9:,:M:,:-:3:3:.:6:,:M:,:,:0:0:0:0:*:5:A: : ;9 $;G;P;G;S;A;,;A;,;3;,;1;7;,;0;6;,;2;8;,;0;2;,;2;4;,;,;,;,;,;,;,;,;3;.;2;,;1;.;7;,;2;.;7;*;3;E; ; <0 $<G<P<G<S<V<,<3<,<1<,<1<2<,<1<7<,<6<4<,<0<3<9<,<4<2<,<0<6<,<6<4<,<2<5<0<,<4<3<,<2<8<,<4<3<,<1<5<1<,<4<2<,<0<2<,<2<5<,<2<3<9<,<3<8<*<7<C< < =1 $=G=P=G=S=V=,=3=,=2=,=1=2=,=2=4=,=1=7=,=2=9=4=,=1=3=,=1=0=,=2=1=,=1=7=6=,=2=2=,=1=2=,=2=5=,=3=1=6=,=2=6=,=5=1=,=3=6=,=2=2=3=,=3=6=*=7=7= = >2 $>G>P>G>S>V>,>3>,>3>,>1>2>,>2>0>,>3>4>,>0>6>1>,>,>0>1>,>0>7>,>0>5>1>,>,>0>4>,>0>5>,>0>5>2>,>,>3>2>,>0>2>,>0>3>2>,>*>7>E> > ?3 $?G?P?R?M?C?,?1?5?5?0?1?8?.?0?0?0?,?A?,?3?7?3?2?.?7?2?3?9?,?N?,?0?7?7?2?6?.?9?9?5?6?,?W?,?0?.?0?0?,?8?0?.?3?0?,?1?4?1?0?1?4?,?,?,?A?*?4?3? ? #4 $#G#P#G#G#A#,#1#5#5#0#1#9#.#0#0#0#,#3#7#3#2#.#7#2#3#9#,#N#,#0#7#7#2#6#.#9#9#5#6#,#W#,#1#,#0#4#,#2#.#3#,#8#8#.#9#,#M#,#-#3#3#.#6#,#M#,#,#0#0#0#0#*#5#D# # A5 $AGAPAGASAAA,AAA,A3A,A1A7A,A0A6A,A2A8A,A0A2A,A,A,A,A,A,A,A,A,A4A.A8A,A2A.A3A,A4A.A3A*A3A0A A B6 $BGBPBRBMBCB,B1B5B5B0B1B9B.B0B0B0B,BAB,B3B7B3B2B.B7B2B3B9B,BNB,B0B7B7B2B6B.B9B9B5B6B,BWB,B0B.B0B0B,B8B0B.B3B0B,B1B4B1B0B1B4B,B,B,BAB*B4B2B B 1-7 are perfect. great. then 8 gets weird. and it never get's normal. Where is this coming from? is this problems with the .log file? is this my code? where is this nonsense creeping in? and, more importantly, how is it messing with the integers to the left? How can anything consistently make an integer that nonsensical?
using tempString.push_back(temp); instead of tempString.append(&temp); fixed everything.
List of lists in Scala? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question I have a grid (that is declared as a List (of Lists)) and each 3*3 block for example is indexed as 0, the next 3*3 block is indexed as 1, etc. I need to write a method that returns the elements (integers) in a particular block given an index. For example : b(0) should give 1 0 0 4 1 2 7 0 0 1 0 0 | 4 0 2 | 5 0 7 | 4 1 2 | 0 0 3 | 6 8 0 | 7 0 0 | 0 0 9 | 0 0 1 | ---------------------- 1 0 0 | 4 0 2 | 5 0 7 | 9 0 2 | 0 4 3 | 1 8 0 | 7 1 0 | 0 8 9 | 8 0 0 | ---------------------- 1 0 0 | 4 0 2 | 5 0 7 | 4 0 2 | 0 1 9 | 6 2 0 | 7 0 0 | 0 0 9 | 0 0 1 | ----------------------
implicit class MatrixOps[T](val l: List[List[T]]) extends AnyVal { def getBlock(index: Int, size: Int = 3): List[T] = { l map (_ drop index * size) take size flatMap (_ take size) } } override def main(args: Array[String]): Unit = { val x = List( List(1, 2, 3), List(4, 5, 6) ) println(x.getBlock(0)) println(x.getBlock(1)) }
Try with following function, def block(i:Int, grid:List[List[Int]]) = grid. grouped(3). toList(i / 3). map(_.grouped(3).toList(i % 3)) Here's that example with nice print method def print(q:List[List[Int]]) = q.map(_.mkString("\t")).mkString("\n") val grid = (1 to 81).toList.grouped(9).toList scala> print(block(1)) res37: String = 4 5 6 13 14 15 22 23 24 (Sorry, Im too lazy to write a sudoku board as an example :) ) To get the list of integers, try flattern. scala> block(1).flattern res38: List[Int] = List(4, 5, 6, 13, 14, 15, 22, 23, 34)
How to compare int with char?
Ok basically, I have a file containing a 9x9 grid (sudoku puzzle). Now this sudoku puzzle is only partially completed, the empty spaces are replaced with "_" (underscores). So here is how the file looks. 5 _ _ _ _ _ 1 7 _ 1 _ 6 5 _ 9 _ 4 _ 4 7 2 1 _ 6 _ _ _ 9 _ _ _ _ _ 5 _ _ _ 1 8 _ 9 5 4 _ _ 6 _ _ 4 _ 2 3 8 9 _ 4 _ _ _ _ 9 3 _ _ 9 _ 7 _ 3 _ 5 _ 2 6 3 9 5 8 7 1 4 And here is my code: for(int row=0;row<9;row++) { for(int column=0;column<9;column++) { fin >> num; if(num == '\95') sudokuPuzzle[row][column] = 0; else sudokuPuzzle[row][column] = num; cout << sudokuPuzzle[row][column] << " "; } cout << endl; } The problem I'm having is on this line: if(num == '\95') I looked in an ASCII table for the decimal value of underscore and that was what I found. However, whenever the cout statement in my loop executes, it shows that my array is filled with just the first number in the file, 5. How do I make a proper comparison between integers and characters? FYI: essentially what I am attempting to do is replace all underscores with 0.
Use if (num=='_') It really is that simple. But I think the problem is in another part of the code - when you read fin >> num; You will not get a satisfactory conversion from underscore to int: you need to read things into an intermediate string variable: fin >> myStringVariable; Then see whether the character is a number or an underscore, and act on it. EDIT complete program: #include <iostream> #include <fstream> int main(void) { std::ifstream fin ("sudoku.txt"); std::string line; int pos; int sudokuPuzzle[9][9]; for(int row=0;row<9;row++) { getline(fin, line); std::cout << "as read in: " << line << std::endl; while((pos = line.find("_", 0))!=std::string::npos) { line[pos] = '0'; } // show that it worked: std::cout << "after substitution: " << line << std::endl; // make a copy of line to process with strtok: char *dup = strdup(line.c_str()); // and now numerically: sudokuPuzzle[row][0] = atoi(strtok(dup, " ")); std::cout << "after conversion: " << sudokuPuzzle[row][0] << " "; for(int column=1;column<9;column++) { sudokuPuzzle[row][column] = atoi(strtok(NULL, " ")); std::cout << sudokuPuzzle[row][column] << " "; } std::cout << std::endl; std::cout << std::endl; free(dup); } } Produces the following output. I think this is self-explanatory. as read in: 5 _ _ _ _ _ 1 7 _ after substitution: 5 0 0 0 0 0 1 7 0 after conversion: 5 0 0 0 0 0 1 7 0 as read in: 1 _ 6 5 _ 9 _ 4 _ after substitution: 1 0 6 5 0 9 0 4 0 after conversion: 1 0 6 5 0 9 0 4 0 as read in: 4 7 2 1 _ 6 _ _ _ after substitution: 4 7 2 1 0 6 0 0 0 after conversion: 4 7 2 1 0 6 0 0 0 as read in: 9 _ _ _ _ _ 5 _ _ after substitution: 9 0 0 0 0 0 5 0 0 after conversion: 9 0 0 0 0 0 5 0 0 as read in: _ 1 8 _ 9 5 4 _ _ after substitution: 0 1 8 0 9 5 4 0 0 after conversion: 0 1 8 0 9 5 4 0 0 as read in: 6 _ _ 4 _ 2 3 8 9 after substitution: 6 0 0 4 0 2 3 8 9 after conversion: 6 0 0 4 0 2 3 8 9 as read in: _ 4 _ _ _ _ 9 3 _ after substitution: 0 4 0 0 0 0 9 3 0 after conversion: 0 4 0 0 0 0 9 3 0 as read in: _ 9 _ 7 _ 3 _ 5 _ after substitution: 0 9 0 7 0 3 0 5 0 after conversion: 0 9 0 7 0 3 0 5 0 as read in: 2 6 3 9 5 8 7 1 4 after substitution: 2 6 3 9 5 8 7 1 4 after conversion: 2 6 3 9 5 8 7 1 4 BONUS 5 3 9 8 2 4 1 7 6 1 8 6 5 7 9 2 4 3 4 7 2 1 3 6 8 9 5 9 2 4 3 8 7 5 6 1 3 1 8 6 9 5 4 2 7 6 5 7 4 1 2 3 8 9 7 4 5 2 6 1 9 3 8 8 9 1 7 4 3 6 5 2 2 6 3 9 5 8 7 1 4
Subtracting 48 from character will give you numeric equivalent of it. e.g suppose if you have char num = '0'; to convert this char '0' into numeric 0 you can use int num1= num - 48; You can just define variable 'num' of type 'char' and use following sudokuPuzzle[row][column] = num - 48; char num; for(int row=0;row<9;row++) { for(int column=0;column<9; { fin >> num; if(num == '_') sudokuPuzzle[row][column] = 0; else sudokuPuzzle[row][column] = num - 48; cout << sudokuPuzzle[row][column] << " "; } cout << endl; }
In \nnn, the nnn is in octal. 9 is not an octal digit. I'm pretty sure -Wall would have told you that, but I could be wrong about that. However, if num is an int, then: fin >> num; is unlikely to do what you want, since _ is not an integer. I would think that the streaming input operator would set the error bit on fin in that case.
You say the file looks like this: 5 _ _ _ _ _ 1 7 _ 1 _ 6 5 _ 9 _ 4 _ 4 7 2 1 _ 6 _ _ _ 9 _ _ _ _ _ 5 _ _ _ 1 8 _ 9 5 4 _ _ 6 _ _ 4 _ 2 3 8 9 _ 4 _ _ _ _ 9 3 _ _ 9 _ 7 _ 3 _ 5 _ 2 6 3 9 5 8 7 1 4 That means that the first line of the file could be expressed as follows: char line[] = { '5', '_', '_', '_', '_', '_', '1', '7', '_' }; Although this is a char array, it's not a string because I didn't add the trailing nul. What does this array contain? The first char, line[0] represents the ASCII character '5'. That's not the same as the integer value, 5, it is the ASCII code for '5'. Looking at an ascii table we can see that '5' is ASCII character 53. So, line[0], if printed as an ASCII character, will display '5', but if printed as an integer value will display '53'. #include <iostream> int main() { char fourtyTwoPlus11 = 53; std::cout << fourtyTwoPlus11 << "\n"; std::cout << static_cast<int>(fourtyTwoPlus11) << "\n"; return 0; } Live demo: http://ideone.com/JEUcLO Your file appears to already contain ASCII characters rather than integer values. So for each character, you actually need to convert to numeric values. You could do something like the following: enum { NumColumns = 9, UnknownValue = -1 }; char line[NumColumns] = { '5', '_', '_', '_', '_', '_', '1', '7', '_' }; int values[NumColumns]; for (size_t i = 0; i < NumColumns; ++i) { char col = line[i]; if (col == '_') values[i] = UnknownValue; else values[i] = col - '0'; } The integer values are in the range 0-9, which we obtain using our knowledge that the ASCII table stores digits in the sequence 0, 1, 2 ... 9. So '0' - '0' == 0, and '9' - '0' = 9. I chose to use -1 to represent the Unknown Value but you could just as easily use 0 here.
For this kind of situation (get an input of either a char or an int), I usually use this trick: char c; int n; cin >> c; if(c != '_'){ cin.putback(c); cin >> n; } fin is similar.