"Unexpected_end, expecting keyword_end" - ruby

I have an issue in one of my functions in my code. I am new to Ruby, so I am unsure of where my syntax error is. My irb is giving me a syntax error related to my end keywords, but I believe the syntax is correct
def function1
print "function 1 \n"
print "Please type 4 lines \n"
i = 0
fptr = (File.new("myFile.txt", "w"))
while i < 4
line = gets
fptr.write(line "\n")
i++
end
fptr.close()
end
This function should print two output lines, open a txt file, take in 4 lines of user input, and write them to the said file.

The problem is that i++ is not valid Ruby. Use i += 1 instead.

Related

Reading from a .txt file and looping until new line

I am having trouble understanding the output of my very little program which should read characters from a text file until it finds a new line.
It correctly outputs the characters and stops, but I don't understand why it still outputs the newline ('\n) character in the terminal and doesn't end before getting to it.
I know that I could use getline() or find another way but I would really understand the reason behind this behaviour.
Thank you in advance!
Edo
Code:
int main() {
std::ifstream in_file;
in_file.open("../responses.txt");
char c;
while(c != '\n'){
in_file.get(c);
std::cout << c << std::endl;
}
return 0;
}
Output:
A
B
C
D
E
Time elapsed: 000:00:000
The

change the position of a line in a file using sed

I would like to know how to change the position of a line in a file (preferably using sed). For example, consider the file that contains
goal identifier statement
let statement 1
let statement 2
forall statement
other statements
I would like to be able to do this
goal identifier statement
forall statement
let statement 1
let statement 2
other statements
where I change the position of the forall line and bring it after the goal line. forall and goal are regexps that can be used to identify the lines.
you can try, for move line 4 to line 2, I want to move line A to line B, where A>B
sed -n '2{h; :a; n; 4{p;x;bb}; H; ba}; :b; p' file
or A<B
sed -n '2{h; d}; 4{p; x;}; p' file
you get, in first case: move line 4 to line 2
goal identifier statement
forall statement
let statement 1
let statement 2
other statements
you get, in second case: move line 2 to line 4
goal identifier statement
let statement 2
forall statement
let statement 1
other statements
Explanation
sed -n ' #silent option ON
2{ #if is line 2
h #Replace the contents of the hold space with the contents of the pattern space
:a #label "a"
n #fetch the next line
4{ #if is line 4
p #print line 4
x #Exchange the contents of the hold and pattern spaces
bb #goto "b"
}
H #appends line from the pattern space to the hold space, with a newline before it.
ba #goto "a"
}
:b #Label "b"
p #print
' file
EDIT
If You want use regex for identify the lines, you can modify first command
sed -n '/goal/{p;n;h;:a;n;/forall/{p;x;bb};H;ba};:b;p' file
$ cat r.awk
BEGIN {
forall_re = "^forall" # examples of regexps
goal_re = "^goal"
}
function tag(l) { # tag a line
if (l ~ goal_re ) return "goal"
else if (l ~ forall_re) return "forall"
else return "rest"
}
{ # store entire file in array; give a tag to every line
lines[NR] = $0
tags[NR] = tag($0)
}
function swap0(a, i, j, tmp) {
tmp = a[i]; a[i] = a[j]; a[j] = tmp
}
function swap(i, j) {
swap0(lines, i, j); swap0(tags, i, j)
}
function rise(i) {
# TODO: add error check
while (i - 1 > 0 && tags[i - 1] != "goal") {
swap(i, i - 1); i--
}
}
function process( i) {
for (i = 1; i <= NR; i++)
if (tags[i] == "forall") rise(i)
}
function dump( i) { # print the array
for (i = 1; i <= NR; i++)
print lines[i]
}
END {
process()
dump()
}
An example of input file
$ cat r.txt
goal identifier statement
let statement 1
let statement 2
forall statement A
other statements
goal identifier statement
let statement 1
let statement 2
forall statement B
other statements
Usage:
$ awk -f r.awk r.txt
goal identifier statement
forall statement A
let statement 1
let statement 2
other statements
goal identifier statement
forall statement B
let statement 1
let statement 2
other statements
sed is for simple substitutions on individual lines, that is all. For anything else you should use awk for every desirable attribute of software (clarity, simplicity, portability, etc.:
$ awk 'NR==FNR{if (/forall/) {f=FNR; v=$0} next} FNR!=f; /goal/{print v} ' file file
goal identifier statement
forall statement
let statement 1
let statement 2
other statements
sed -r '/goal/{ # if match "goal" line
:X # this is a lable for branch command
N # append next line
/forall[^\n]*$/{ # if match "forall" line move to "goal" line below
s#^([^\n]*)(.*)(\n[^\n]*)$#\1\3\2#
b # after move finished branch to end
}
bX # branch to :X for appending next line
}' file
goal identifier statement
forall statement
let statement 1
let statement 2
other statements
A less terrible way using vim to find a line in $FILENAME using regex $REGEX_EXPRESSION and move that line to $LINE_NUMBER:
vim -c "g:$REGEX_EXPRESSION:m$LINE_NUMBER" -cwq "$FILENAME"
explanation: -c is command in vim, so it goes to the first line that matches that regex and then moves it to the line number specified, and then does the command wq (or write and quit).

String Comparison inside loop Maple

I am new to maple. I am having a problem with a small program that will read data from file, perform some calculations and then write back to file. Data being read from the file is required to be compared in order to extract x,y and z data. I can read properly from the file and do the comparison but when I read inside a loop the comparison doesnot work, It seems the compare function is not working at all inside the loop.
Can anyone please tell me whats wrong with my code?
with(StringTools):
file:=fopen("C:\\Fileread.txt",READ,TEXT);
cryptf:=fopen("C:\\Filewrite.txt",WRITE,TEXT);
line :=readline(file);
while (line <> 0) do
if type(Compare(line, "x"),boolean) then
line := readline(file);
x[1] = convert(line, decimal, hex);
elif Compare(line, "y") then
line := readline(file);
Y[1]= convert(line, decimal, hex);
elif Compare(line, "Z") then
line := readline(file);
Z[1] = convert(line, decimal, hex);
else
d=1;
end if;
line :=readline(file);
writeline(cryptf,X[1]);
od;
close(file);
close(cryptf);

Parsing file, ignoring comments and blank lines

As the title says, I am trying to parse a file but ignore comments (started with #) or blank lines. I have tried to make a system for this, yet it always seems to ignore that it should be ignoring comments and/or blank lines.
lines := strings.Split(d, "\n")
var output map[string]bool = make(map[string]bool)
for _, line := range lines {
if strings.HasPrefix(line, "#") != true {
output[line] = true
} else if len(line) > 0 {
output[line] = true
}
}
When run (this is part of a function), it outputs the following
This is the input ('d' variable):
Minecraft
Zerg Rush
Pokemon
# Hello
This is the output when printed ('output' variable):
map[Minecraft:true Zerg Rush:true Pokemon:true :true # Hello:true]
My issue here is that it still keeps the "" and "# Hello" values, meaning that something failed, something I haven't been able to figure out.
So, what am I doing wrong that this keeps the improper values?
len(line) > 0 will be true for the "# Hello" line, so it will get added to output.
Currently, you are adding lines that either don't start with a # or are not empty. You need to only add lines that satisfy both conditions:
if !strings.HasPrefix(line, "#") && len(line) > 0 {
output[line] = true
}

Remove the desired content from a text

I would like to get a working code to simply remove from a text line a specific part that always begins with "(" and finish with ")".
Sample text : Hello, how are you (it is a question)
I want to remove this part: "(it is a question)" to only keep this message "Hello, how are you"
Lost...
Thanks
One way using Regular Expressions;
input = "Hello, how are you (it is a question)"
dim re: set re = new regexp
with re
.pattern = "\(.*\)\s?" '//anything between () and if present 1 following whitespace
.global = true
input = re.Replace(input, "")
end with
msgbox input
If the part to be removed is always at the end of the string, string operations would work as well:
msg = "Hello, how are you (it is a question)"
pos = InStr(msg, "(")
If pos > 0 Then WScript.Echo Trim(Left(msg, pos-1))
If the sentence always ends with the ( ) section, use the split function:
line = "Hello, how are you (it is a question)"
splitter = split(line,"(") 'splitting the line into 2 sections, using ( as the divider
endStr = splitter(0) 'first section is index 0
MsgBox endStr 'Hello, how are you
If it is in the middle of the sentence, use the split function twice:
line = "Hello, how are you (it is a question) and further on"
splitter = split(line,"(")
strFirst = splitter(0) 'Hello, how are you
splitter1 = split(line,")")
strSecond = splitter1(UBound(Splitter1)) 'and further on
MsgBox strFirst & strSecond 'Hello, how are you and further on
If there is only one instance of "( )" then you could use a '1' in place of the UBound.
Multiple instances I would split the sentence and then break down each section containing the "( )" and concatenate the final sentence.

Resources