Changing characters from file in Lua - io

I have a semi-complex question/help request for Lua coding. I'm a beginning, though I do have a basic understanding of Lua. Mostly looking in to IO (Which I hope is the correct thing to look into). Anyway, my question how would I go about reading a file, converting all the characters in the file into others (like the character "A" into the character "B", even if it's in a word) and then append it at the end of the file.
Current code (I know it's not much, but the each tutorial for IO input/output on files is vague and differs. Plus I generally started today....)
Lua 5.1
file = io.open("dump.txt", a+)
modifyable = file:read()
From what I understand, I'm reading from the file "dump.txt", and using Append+, which should add it at the bottom. And then I'm reading the file, so that I can add the changes needed. Am I wrong in any way and/or what do I need to do to achieve my goal?
I also read around and say that file:write would allow me to write to this line, but would that work since I'm using a+?

You could do it with a+ but why have the file open while transforming:
file = io.open("dump.txt", 'r')
modifyable = file:read()
file:close()
modified = doModifications(modifyable) -- (like changing all A to B)
file = io.open("dump.txt", 'a')
file:write(modified)
If you want to use a+:
file = io.open("dump.txt", 'a+')
modifyable = file:read()
modified = doModifications(modifyable) -- (like changing all A to B)
file:write(modified)

Related

Is there a way to compare the format in which a line of a text file was written in Fortran?

I'm developing a Fortran program that must obtain some data from a text file and generate another text file using specific data from the first one.
The input file have many lines written in several specific formats which I know of. Although I know the formats, the lines in this file are generated in a "random way".
It would be much easier to generate the output file if I could compare the format in which each line was written, then I would know exactly what data I can get from that line of the input file to use it in the output file.
What I need is something like, for example, knowing that the format of the line read and stored in the LINHA variable is described in the FORMATO variable, do something like:
    
IF (FORMATO = '(1X, 15,3F8.1,2 (5A, 1X))') THEN
READ (LINHA, '(6X, F8.1)') my_variable
END IF
Because there might be another format such as
'(6A, 2F8.1, F8.6,2 (6A))'
in which, if I use the same READ statement, I will read an F8.1 variable in my_variable, however this value is not the correct one.
A (not so elegant) work-around that I can think of is to read the entire line using the advance = no option of read() and parse each character in the line separately. While doing so, you may count white spaces or other specific characters that you know of and then identify the different formats from there.
It would be helpful if you could give more specifications of the nature of the task.
The best option is to read without format, keeping each line in a character array. Then read the line variable as an internal file with the required format using the variable IOSTAT in order to check if the format is the correct.
INT max_size = 80
CHARACTER(LEN=max_size) :: line
READ(*,*) line
READ(line,'(1X, 15,3F8.1,2 (5A, 1X))',IOSTAT=ios) var1, var2, ...
Problem solved using a mixture of some of the suggestions posted.
I read each of the lines of the input file in an internal variable (RLINFILE) in the format '(A165)'. After that, I read all the contents of the string that I put in this internal variable in several dummy variables, using the format I knew of the lines from where I wanted to get some information (read all the information of the line in the desired and get IOSTAT = 0 guarantee that this is the correct line), so if the result of the reading is ok (IOSTAT = 0), it is because the line I just read was the correct one for the information I wanted, so I store the contents of some of the dummy variables that represent the values that interest me. In the code, the solution looked something like this:
OPEN(UNIT=LU1,FILE=RlinName,STATUS='OLD')
ilin = 0
formato = '(14X,A,1X,F7.1,1X,F7.1,5X,A,1X,A,1X,A,5X,A,I5,1X,A,I3,3F8.1,A,A,A,1X,A,2(1X,F8.2),1X,A,1X,A)'
DO WHILE (.TRUE.)
READ(LU1,'(A165)',END=300) RLINFILE
READ(RLINFILE,formato,IOSTAT=linhaok) dum2_a1,dum2_f1,dum2_f2,dum2_a2,dum2_a3,dum2_a4,dum2_a5,dum2_i1,dum2_a6,dum2_i2,dum2_f3,dum2_f4,dum2_f5,dum2_a7,dum2_a8,dum2_a9,dum2_a10,dum2_f6,dum2_f7,dum2_a11,dum2_a12
IF(linhaok.EQ.0) THEN
ilin = ilin+1
rlin_lshu(ilin) = dum2_a4
rlin_nbpa(ilin) = dum2_i1
rlin_ncir(ilin) = dum2_i2
rlin_ppij(ilin) = dum2_f3
rlin_pqij(ilin) = dum2_f4
rlin_tapn(ilin) = dum2_a7
END IF
END DO
300 CLOSE(UNIT=LU1)
The description of the problem you are trying to solve is a bit vague to me, but the simplest solutions that comes to my mind, given the description of the problem, is to modify the original code that generates the input data file, to write the used Fortran READ format before the data line in the input file. This way, you can read the format as a string and use it in the subsequent data IO in your second code.
If you describe the specific task your tryting to accomplish in more details, perhaps more experienced Fortranners could help.

How to read fetch wav file one by one in matlab?

this is my Matlab code, I want to read .wav file from the same file or another file.
str=['1.wav';'2.wav';'3.wav';'4.wav';'5.wav';];
for i=1:5
[y, fs]=wavread(str(i));
a = miraudio(str(i));
z = mirzerocross(a)
close all
end
it gives me error like..
Error using TRYFINAL (line 1)
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Your OP is failing because of how MATLAB character arrays are implemented (#patrik has a very good explanation in this recent question). If you want to use a character array every row must be the same length, requiring you to pad the entries somehow which, while doable, isn't very efficient. The alternative is to use cell arrays, as #nkjt suggested, which will work for the implementation outlined in your OP.
A more general approach, however, is to use the data structure returned by MATLAB's dir command to identify all of the *.wav files in a directory and perform some operation on all of them.
pathname = 'C:\somewavfiles'; % Full path to a folder containing some wav files
wavfiles = dir(fullfile(pathname, '*.wav')); % Obtain a list of *.wav files
% Loop over all the files and perform some operations
for ii = 1:length(wavfiles)
filepath = fullfile(pathname, wavfiles(ii).name); % Generate the full path to the file using the filename and the pathname specified earlier
[y, fs] = wavread(filepath);
a = miraudio(filepath);
z = mirzerocross(a);
end
I have used fullfile in a few places rather than concatenating strings with a slash in order to avoid compatibility issues between operating systems. Some use \ and others use /.
Also note that, as explained by the documentation, you can use wildcards (*) in dir calls to narrow down the list of files returned.

need guidance with basic function creation in MATLAB

I have to write a MATLAB function with the following description:
function counts = letterStatistics(filename, allowedChar, N)
This function is supposed to open a text file specified by filename and read its entire contents. The contents will be parsed such that any character that isn’t in allowedChar is removed. Finally it will return a count of all N-symbol combinations in the parsed text. This function should be stored in a file name “letterStatistics.m” and I made a list of some commands and things of how the function should be organized according to my professors' lecture notes:
Begin the function by setting the default value of N to 1 in case:
a. The user specifies a 0 or negative value of N.
b. The user doesn’t pass the argument N into the function, i.e., counts = letterStatistics(filename, allowedChar)
Using the fopen function, open the file filename for reading in text mode.
Using the function fscanf, read in all the contents of the opened file into a string variable.
I know there exists a MATLAB function to turn all letters in a string to lower case. Since my analysis will disregard case, I have to use this function on the string of text.
Parse this string variable as follows (use logical indexing or regular expressions – do not use for loops):
a. We want to remove all newline characters without this occurring:
e.g.
In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
In my younger and more vulnerableyears my father gave me some advicethat I’ve been turning over in my mindever since.
Replace all newline characters (special character \n) with a single space: ' '.
b. We will treat hyphenated words as two separate words, hence do the same for hyphens '-'.
c. Remove any character that is not in allowedChar. Hint: use regexprep with an empty string '' as an argument for replace.
d. Any sequence of two or more blank spaces should be replaced by a single blank space.
Use the provided permsRep function, to create a matrix of all possible N-symbol combinations of the symbols in allowedChar.
Using the strfind function, count all the N-symbol combinations in the parsed text into an array counts. Do not loop through each character in your parsed text as you would in a C program.
Close the opened file using fclose.
HERE IS MY QUESTION: so as you can see i have made this list of what the function is, what it should do, and using which commands (fclose etc.). the trouble is that I'm aware that closing the file involves use of 'fclose' but other than that I'm not sure how to execute #8. Same goes for the whole function creation. I have a vague idea of how to create a function using what commands but I'm unable to produce the actual code.. how should I begin? Any guidance/hints would seriously be appreciated because I'm having programmers' block and am unable to start!
I think that you are new to matlab, so the documentation may be complicated. The root of the problem is the basic understanding of file I/O (input/output) I guess. So the thing is that when you open the file using fopen, matlab returns a pointer to that file, which is generally called a file ID. When you call fclose you want matlab to understand that you want to close that file. So what you have to do is to use fclose with the correct file ID.
fid = open('test.txt');
fprintf(fid,'This is a test.\n');
fclose(fid);
fid = 0; % Optional, this will make it clear that the file is not open,
% but it is not necessary since matlab will send a not open message anyway
Regarding the function creation the syntax is something like this:
function out = myFcn(x,y)
z = x*y;
fprintf('z=%.0f\n',z); % Print value of z in the command window
out = z>0;
This is a function that checks if two numbers are positive and returns true they are. If not it returns false. This may not be the best way to do this test, but it works as example I guess.
Please comment if this is not what you want to know.

Unicode name from Char

I'm looking for a function that takes a Char as input and gives the unicode name of that code point (::Char->String), but I couldn't find any results on Hoogle. I assume that there is no builtin (If there is, please let me know) and so I wonder what's the best way to write this function and its inverse (::String->Maybe Char).
I know you'd have to read UnicodeData.txt or a similar document, but I don't know what the best/fastest function would be.
The unicode-names package contains the function
getCharacterName :: Char -> String
First of all, thanks to #TwanVanLaarhoven who provided an excellent answer. I did however need a function that did the reverse of getCharacterName.
What I originally wanted was a function that would read the file and not have it hard-coded, but I eventually realized that that would require unsafe IO operations.
What I decided to do was to copy UnicodeData.txt into notepad++ and use the following regex replacements:
write module UnicodeNames (characterToName,nameToCharacter) where
paste UnicodeData.txt
replace this: ^([\dA-F]+);([^<;>]+).*$|^([\dA-F]+);(?:[^;]*;){9}([^<;>]+).*$
with this: characterToName '\\x$1$3' = "$2$4"
append characterToName _ = ""
paste again
replace this (again): ^([\dA-F]+);([^<;>]+).*$|^([\dA-F]+);(?:[^;]*;){9}([^<;>]+).*$
with this: nameToCharacter "$2$4" = Just '\\x$1$3'
append nameToCharacter _ = Nothing
replace ^.*<.*$ with nothing to remove extra lines.
The file will be incredibly long and take forever to compile :-) In addition to having an inverse function, this method has the advantage of providing more names than the unicode-names package by using unicode 1.0 names as well. The two functions in this file rely on pattern matching to act as a dictionary from char to string and vice-versa. I would put my solution on PasteBin or somewhere else if it didn't use a ton of memory.

Reading all but last few lines of a data file

I can easily skip the header of a data file using getline, but then when I parse through the data file and get to the footer of the file, I end up stuck in a loop because the program is trying to parse columns of data that no longer exist. Is there an easy way to stop reading when there is no longer data in the line? It looks like there is a blank line followed by some footer information, but I cannot guarantee that all of my data files will look like that (i.e. I need something pretty generic).
Looking at your existing code (edit your question and put it there, not in a comment), I see you have nested loops. But what you really want is one loop with two reasons to exit.
while ((q < 16) && (liness >> temp)) { ... }
Read the line into a string, parse if only if you see \n at the end.

Resources