I am trying to write something to a file but pascal adds a bunch of whitespaces between each record's value and also puts 2 records on one line.
The first file is just a list of strings.
The second file (the one I create through the program) should have title and description.
How can I get rid of the whitespaces pascal is adding?
program Wiki;
{$mode objfpc}
TYPE wiki=record
title:string;
description:string;
end;
var
f:text ;
g:file of wiki ;
row:wiki;
fileName: string;
oldFileName:string;
begin
writeln('Old file name:');
readln(oldFileName);
ASSIGN(f,oldFileName);
RESET(f);
writeln('New file name:');
readln(fileName);
ASSIGN(g,fileName);
REWRITE(g);
REPEAT
Readln(f,row.title);
writeln('give a description:');
Writeln(row.title);
Readln(row.description);
Write(g,row)
until EOF(f);
CLOSE(f);
CLOSE(g);
writeln;
writeln('press enter to close.');
readln();
end.
In objfpc mode without {$H+}, I guess that row.description is a fixed size Turbo Pascal style ShortString. It is 255 characters long, and that is probably why you get all that whitespace.
Rather write the output file as a text file:
var
f: Text;
g: Text;
and:
Writeln(g, row.title, ';', row.description);
That should produce text output like:
Finding Nemo;The adventures of two fish trying to find the lost son of one of them
Toy Story;The adventures of a merry bunch of toys
etc.
Related
Im just starting up with pascal and I'm doing the good old bhaskara solver with the following code:
Program bhaskara;
var
a,b,c: real;
begin
writeln('Ingrese a, b y c');
readln(a,b,c);
if sqr(b) >= 4*a*c then
begin
writeln('tiene raices reales');
end
else
begin
writeln('no tiene raices reales');
end
readln(a);
end.
The last line: readln(a), which is there just to pause the program and see the output is making the program not compile(program works fine without it), it says:
bhaskara.pas(15,2) Fatal: Syntax error, ";" expected but "identifier READLN" found
Im sure it's something simple but i can't find the answer, please help.
Pascal requires a semicolon as a statement separator between statements.
Your else block is a statement and because it is not the final line
of the program and is followed by your readln(a), it requires a ';'
after it.
In fact, because your else clause contains only a single statement,
it does not require the begin & end.
So you could simply write
else
writeln('no tiene raices reales');
readln(a);
You need a semicolon (;) after the "end" statement right before the readln statement.
I want to take the follow data variable:
"Nebraska-Iowa"
"Washington-Arkansas"
"Illinois-Utah"
and transform it so that it orders the character groups around the hyphen to be in alphabetical order:
"Iowa-Nebraska"
"Arkansas-Washington"
"Illinois-Utah"
Is there an easy way to do this? I need to split the string around the hyphen, rearrange if necessary, and than paste back together.
UPDATE
After playing with Matthew's answer, I have decide to generalize this for any number of states with the following dataset:
Nebraska-Iowa
Washington-Arkansas-Texas
Illinois-Utah
Colorado
Here is the code I am trying to build. What I am struggling with is building an array that I loop through, pull out the appropriate word, and then pasting them back together after arranging. Please help!
/*Example dataset*/
data have;
format text $50.;
input text;
datalines;
Nebraska-Iowa
Washington-Arkansas-Texas
Illinois-Utah
Colorado
run;
/*Rearrange strings in dataset*/
data arrangestrings;
set have;
length result $50;
howmanyb = countc(text,'-');
howmany = howmanyb + 1;
array state[howmany] _character_;
do i=1 to howmany;
state[i] = scan(text, i, '-');
end;
call sortc(of state(*));
result = catx("-", state[*]);
keep result;
run;
I don't think you need to go to the trouble of defining a user-defined format for a task like this. The built-in scan method is your friend here:
data have;
format text $50.;
input text;
datalines;
Nebraska-Iowa
Washington-Arkansas
Illinois-Utah
run;
data want;
set have;
length word1 word2 result $50;
word1 = scan(text, 1, '-');
word2 = scan(text, 2, '-');
result = ifc(word1 <= word2, text, catx('-', word2, word1));
run;
proc print data=want;
run;
Check out the documentation on the built-in functions that I used (scan, ifc, catx) if you're not familiar with them:
http://support.sas.com/documentation/cdl/en/allprodslang/67244/HTML/default/viewer.htm#syntaxByType-function.htm
im new using Pascal and i have to program a game called seven eleven, can you guys give me some tips?
Ive tried:
program sevele;
var capitalinicial: integer
begin
writeln('ingrese un capital')
readln(capitalinicial)
writeln('su capital es capitalinicial')
Answering your question, There are a few things you need to know:
You have to put a semicolon ; at the end of every sentence, except after a keyword that denotes the beginning of a control structure (for,while,if,else,etc) or after a begin or end keyword.
Use the same amount of begins and ends keywords. Note that the last end in the program is followed by a dot.
When you use writeln you can print one or more variables or strings. You have to use simple quotes ' to print strings, and just the name of a variable without any quote to print it's value, also you need to separe the diferent arguments with commas ','.
for example:
program example;
var
a,b:integer;
begin
a:=3;
b:=5;
writeln ('this is just a string');
writeln (a);
writeln (a,b);
writeln ('the value of a is: ',a,' and the value of b is: ',b);
readln;
end.
The code you attemped to write probably is:
program sevele;
var capitalinicial: integer;
begin
writeln('ingrese un capital');
readln(capitalinicial);
writeln('su capital es ',capitalinicial);
readln; //use this to give you time to view the output
end.
This is how it works
program sevele;
var capitalinicial: integer;
begin
writeln('ingrese un capital');
readln(capitalinicial);
writeln('su capital es',capitalinicial);
end.
what is the simplest way of reading a random block of characters from a text file using bash?
A block is a set of characters which begin with X and end with X, where X is a character sequence, usually it will be "\n\n"
We can assume that file has short lines, less than 200 characters each.
Blocks don't have more than 20 lines.
I have seen threads like get random line, get text from between two tokens, but it's not exacly what I need.
I can write a simple program in C that will read how many blocks are in file, get a random number from a given range and then search for a block with this ID, but there must be an easier way.
Example:
X = "\n\n"
File: (the .'s are not in the file, I used them to make "empty" line at the begginning and end of code)
.
first line
second line and some other text
fourth line
sixth line
seventh line, more textęęę
.
Running the script for first time, output:
fourth line
Running the script for the second time, output:
first line
second line and some other text
Yours faithfully,
user2420535
To get a uniformly random block from a file of blank-line-separated blocks in one pass,
awk -v RS='\n\n' '
BEGIN { srand(); }
rand() < 1.0/NR { s=$0; }
END { print s; }
' file
This is a simple case of Reservoir Sampling.
Reading this Pascal BNF grammar I can't understand why is a ; required to appear after end in a function definition. After a function-heading is seen, a function-block that's block may appear:
function-declaration =
function-heading ";" function-body |
function-heading ";" directive |
function-identification ";" function-body .
function-body =
block .
When a begin appear, that's part of a statement-par, that's part of a block, it's processed by statement-part, right?
block =
declaration-part statement-part .
statement-part =
begin statement-sequence end .
Note statement-part. There's no ; here after end keyword and this is not part of a statement-sequence. So, I don't get how the compiler claims about lack of ; after end keyword, like in this example:
function myabs(i : integer) : integer;
begin
if i < 0 then begin i := -i; end; < -- it's process by statement-sequence, so, ';' may appear
myabs := i;
end; <-- it is the semicolon what about I'm speaking
What am I missing? am I reading wrong the grammar? all Pascal compilers I've tried give an error if I omit this.
ANTLRWorks is your best friend here.
If you try some pascal grammar such as http://www.monperrus.net/martin/pascal-antlr3 using antlrworks (http://www.antlr3.org/works/) you'll see that a program like
program first;
function myabs(i : integer) : integer;
begin
end;
begin
end.
will be parsed like this
so you can see exactly what's happening.
ps. the pascal grammar link I've provided to you has a problem with one specific token, but I bet you can workaround this ;-)
ps2. update - antlrworks screenshot to help #Jack
You don't have to have a semi-colon after an end. Simple as that.
Semi-colon is used to separate statements. So you only need to have a semi-colon after an end if it is not the last statement. If it is the last statement you should instead have a full stop.
Now, there could also be some error in the BNF that means that according to the BNF you don't have to have a semi-colon where you actually need it, but the only way to figure that out is to analyze the whole BFN in detail, which I don't feel is constructive. :-)
But in this case I think what you have missed is that a procedure or function declaration must end with a semi-colon.
Procedure and functions do not need to be terminated with a semi-colon, but they must be separated by one:
From the Pascal BNF
proc-and-func-declaration:
proc-or-func
proc-and-func-declaration ; proc-or-func