Julia: string comparison with special characters
I need to read a text file containing data csv
with headers separating individual blocks of data. Headings always start with a dollar sign $
. So my text file looks like this:
$Header1
2
1,2,3,4
2,4,5,8
$Header2
2
1,1,0,19,9,8
2,1,0,18,8,7
What I want to do is if the program reaches $Header2
, I want to read all subsequent lines after it until it reaches, say, the $Header3
end of the file. I think I can use Julia's `cmp 'for this. I tried with a small file that contains the following text:
# file julia.txt
Julia
$Julia
and my code reads:
# test.jl
fname = "julia.txt"
# set some string values
str1 ="Julia";
str2 ="\$Julia";
# print the strings and check the length
println(length(str1),",",str1);
println(length(str2),",",str2);
# now read the text file to check if you are able to find the strings
# str1 and str2 above
println ("Reading file...");
for ln in eachline(fname)
println(length(ln),",",ln);
if (cmp(str1,ln)==0)
println("Julia match")
end
if (cmp(str2,ln)==0)
println("\$Julia match")
end
end
what I get as output from the above code:
5,Julia
6,$Julia
Reading file...
6,Julia
7,$Julia
I don't understand why I am getting character length 6 for line Julia
and 7 for line $Julia
when they are read from a file. I checked the text file by including white spaces and there are none. What am I doing wrong?
source to share
The problem is that the strings returned eachline
contain a newline character at the end.
You can use chomp
to remove it:
julia> first(eachline("julia.txt"))
"Julia\n"
julia> chomp(first(eachline("julia.txt")))
"Julia"
Alternatively, you can simply use ==
instead cmp
to check if two strings are equal. Both are used from ccall
before memcmp
, but ==
only does it for strings of the same length and is therefore probably faster.
source to share