I am trying to use nested if statements in Cobol. As far as I can tell I am following the style guides, but keep receiving the error:
file_name.cob:64: Error: syntax error, unexpected ELSE
^^ (This is the second ELSE statement)
The purpose of the code is to function as a Caesar cipher, but it seems to only be the nested if statements that are producing the error. I tried putting the nested statements after the ELSE clause of the initial IF statement, but that was unsuccessful as well.
edit: I am using open-cobol, and and compiling with the -free option
IF CharCount < 26
ADD firstnum, CharCount GIVING stringShift.
DISPLAY stringShift.
IF FUNCTION MOD(stringShift, 26) IS NOT ZERO
MOVE FUNCTION MOD(stringShift, 26) to stringShift
DISPLAY stringShift
MOVE abc(stringShift:stringShift) TO newChar
DISPLAY newChar
STRING newString DELIMITED BY "", newChar DELIMITED BY SIZE INTO newString
DISPLAY newString
ELSE
STRING newString DELIMITED BY "", searchChar DELIMITED BY SIZE INTO newString
DISPLAY newString
END-IF
ELSE
STRING newString DELIMITED BY "", searchChar DELIMITED BY SIZE INTO newString
DISPLAY newString
END-IF.
Thanks!
Just wanted to share the answer here, as it was answered in the comp.lang.cobol google group. It was the two periods after the first ADD and DISPLAY lines that were causing the problems. It now compiles successfully.
The lines should appear as:
ADD firstnum, CharCount GIVING stringShift
DISPLAY stringShift
Related
I have searched a lot about it on SO and solutions like "" the part where comma is are giving errors. Moreover it is using C++ :)
char *msg = new char[40];
msg = "1,2, Hello , how are you ";
char msg2[30];
strcpy_s(msg2, msg);
char * pch;
pch = strtok(msg2, ",");
while (pch != NULL)
{
cout << pch << endl;
pch = strtok(NULL, ",");
}
Output I want :
1
2
Hello , how are you
Out put it is producing
1
2
Hello
how are you
I have tried putting "" around Hello , how are you. But it did not help.
The CSV files are comma separated values. If you want a comma inside the value, you have to surround it with quotes.
Your example in CSV, as you need your output, should be:
msg = "1,2, \"Hello , how are you \"";
so the value Hello , how are you is surrounded with quotes.
This is the standard CSV. This has nothing to do with the behaviour of the strtok function.
The strtok function just searches, without considering anything else, the tokens you have passed to it, in this case the ,, thus it ignores the ".
In order to make it work as you want, you would have to tokenize with both tokens, the , and the ", and consider the previous found token in order to decide if the , found is a new value or it is inside quotes.
NOTE also that if you want to be completely conforming with the CSV specification, you should consider that the quotes may also be escaped, in order to have a quote character inside the value term. See this answer for an example:
Properly escape a double quote in CSV
NOTE 2: Just for completeness, here is the CSV specification (RFC-4180): https://www.rfc-editor.org/rfc/rfc4180
I have the following string:
{'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921}
I would like to verify the format of the string that the word 'variable' is the second word and i would like to retrive the string after the last '/' in the 3rd string (In this example 'D_foo').
how could i verify this and retrive the sting i search?
I tried the following:
regexp(str,'{''\w+'',{''variable'',''([(a-z)|(A-Z)|/|_])+')
without success
REMARK
The string to analysis is not splited after the komma, it is only due to length of the string.
EDIT
my string is:
'{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
and not a cell, which could be understood. I added the sybol ' at the start and end of the string to symbolizied that it is a string.
I realise that you mention using regexp in the question, but I'm not sure if this is a requirement? If other solutions are acceptable you could try this:
str='{''output'',{''variable'',''VGRG_Pos_Var1/Parameters/D_foo''},''date'',734704.60904050921}';
parts1=textscan( str, '%s','delimiter',{',','{','}'},'MultipleDelimsAsOne',1);
parts2=textscan( parts1{1}{3}, '%s','delimiter',{'/',''''},'MultipleDelimsAsOne',1);
string=parts2{1}{end}
match=strcmp(parts1{1}{2},'variable')
To answer the first part of your question, you can write this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %this holds the cell containing the two strings
if cmpstr(temp{1}(1), 'variable')
%do stuff
end
For the second part you can do this:
str = {'output',{'variable','VGRG_Pos_Var1/Parameters/D_foo'},'date',734704.60904050921};
temp = str(2); %like before, this contains the cell
temp = temp{1}(2); %this picks out the second string in the cell
temp = char(temp); %turns the item from a cell to a string
res = strsplit(temp, '/'); %splits the string where '/' are found, res is an array of strings
string = res(3); %assuming there will always be just 2 '/'s.
I did a program to remove a group of Characters From a String. I have given below that coding here.
void removeCharFromString(string &str,const string &rStr)
{
std::size_t found = str.find_first_of(rStr);
while (found!=std::string::npos)
{
str[found]=' ';
found=str.find_first_of(rStr,found+1);
}
str=trim(str);
}
std::string str ("scott<=tiger");
removeCharFromString(str,"<=");
as for as my program, I got my output Correctly. Ok. Fine. If I give a value for str as "scott=tiger" , Then the searchable characters "<=" not found in the variable str. But my program also removes '=' character from the value 'scott=tiger'. But I don't want to remove the characters individually. I want to remove the characters , if i only found the group of characters '<=' found. How can i do this ?
The method find_first_of looks for any character in the input, in your case, any of '<' or '='. In your case, you want to use find.
std::size_t found = str.find(rStr);
This answer works on the assumption that you only want to find the set of characters in the exact sequence e.g. If you want to remove <= but not remove =<:
find_first_of will locate any of the characters in the given string, where you want to find the whole string.
You need something to the effect of:
std::size_t found = str.find(rStr);
while (found!=std::string::npos)
{
str.replace(found, rStr.length(), " ");
found=str.find(rStr,found+1);
}
The problem with str[found]=' '; is that it'll simply replace the first character of the string you are searching for, so if you used that, your result would be
scott =tiger
whereas with the changes I've given you, you'll get
scott tiger
I am trying to read data from a text file and split the read line based on quotes. For example
"Hi how" "are you" "thanks"
Expected output
Hi how
are you
thanks
My code:
getline(infile, line);
ch = strdup(line.c_str());
ch1 = strtok(ch, " ");
while (ch1 != NULL)
{
a3[i] = ch1;
ch1 = strtok(NULL, " ");
i++;
}
I don't know what to specify as delimiter string. I am using strtok() to split, but it failed. Can any one help me?
Please have a look at the example code here. You should provide "\"" as delimiter string to strtok.
For example,
ch1 = strtok (ch,"\"");
Probably your problem is related with representing escape sequences. Please have a look here for a list of escape sequences for characters.
Given your input: "Hi how" "are you" "thanks", if you use strtok with "\"" as the delimiter, it'll treat the spaces between the quoted strings as if they were also strings, so if (for example) you printed out the result strings, one per line, surrounded by square brackets, you'd get:
[Hi how]
[ ]
[are you]
[ ]
[thanks]
I.e., the blank character between each quoted string is, itself, being treated as a string. If the delimiter you supplied to strtok was " \"" (i.e., included both a quote and a space) that wouldn't happen, but then it would also break on the spaces inside the quoted strings.
Assuming you can depend on every item you care about being quoted, you want to skip anything until you get to a quote, ignore the quote, then read data into your input string until you get to another quote, then repeat the whole process.
I'm trying to split all the words in a string into an array in AS3. The obvious answer of course would be to simply do this:
str.split(/\s/);
The problem here is that I need to be able to tell whether the split occurred on a newline or a space. I'm trying to put the words of a string into draggable boxes, and I want the ones after a newline to go, well, on a new line.
Any idea the best way to go about this? Clearly, the above split method will get rid of the crucial newline character that will tell me what I need to know. Should I use a regex.exec with a while loop, or is there any way to use split to preserve the characters I need?
Example string :
This is an example string
with spaces as well as newlines
and needs a regex
1/ Split the string on newline, get array#1.
array#1 = [ "This is an example string","with spaces as well as
newlines","and needs a regex" ]
2/ For each element in array#1 , split based on your current regex which will break the strings
only on spaces as newlines have already been dealth with, this 2-D array is array#2
array#2 = [
["This","is","an","example","string"] ,
["with","spaces","as","well","as","newlines"],
["and","needs","a","regex"]
]
3/ Process elements of array#2 as you want.
First split you string at the newline
var lines:Array = str.split("\n");
Now you can loop on you lines and split each of these in to seperate words
for(var i:int = 0; i < lines.length; i++){
var words = str[i].split(" ");
for(var j:int = 0; j < words.length; j++){
trace("word", words[i]);
}
trace("newline");
}