when run Zimpl format file in lpsolve IDE,
set d := { "d1", "d2", "d3", "d4" };
param P[d] :=<"d1"> 11, <"d2"> 22;
do print P;
display following errors:
*** Error 142: Unknown index <> for symbol "P"
*** do print P;
*** ^^^ ```
In the reference of Zimpl user guide, do print dosnot support parameter. How to print parameter in zimpl file?
As far as I know, the do print command doesn't support printing arrays.
Try
do forall <i> in d : print P[i];
Related
I have a quick file file.awk with
function attr(attrname,str, a) {
if (!str) str=$0
match(str,"#" attrname "=([^,/]*)",a)
return a[1]
}
I am getting an error
awk: file.awk: line 139: syntax error at or near ,
where line 139 is the line with match()
Any idea whats wrong with the syntax?
You're trying to use the non-POSIX 3rd arg to match() but not using GNU awk which supports it. See https://www.gnu.org/software/gawk/manual/gawk.html#String-Functions.
As always, Ed Morton has it correct. The only reason I'm posting is that I can share with MacOS users that the awk man page includes the answer.
man awk yields:
match(s, r)
the position in s where the regular expression r occurs, or 0 if it does not. The variables RSTART and RLENGTH are set to the position and
length of the matched string.
So it is very clear that your call has an extra parameter.
I tried runnning awk with your function on MacOS and got this error:
awk: syntax error at source line 1 in function attr source file
context is
match(str,"#" attrname >>> "=([^,/]*)", <<<
The "<<<" is indicating awk doesn't like that comma. This is similar to your error message:
syntax error at or near ,
You absolutely don't need the match() function's capture array to get the attribute value you're seeking :
echo 'april#token=sha256hmackey/0.ts' \
\
| mawk 'function attr(___,_,__,____) { ____="\32\21"
if(_=="") {
_=$(_<_) }
return \
sub("[#]"(___)"=[^,\\/]*",____"&"____,_) \
\
? substr(_=__[split(_,__,____)-\
(_~_)],index(_,"=")+(_~"")) : ""
} {
print attr("token") }'
sha256hmackey
When I call f without parameter, I got the error Script Error: if does not allow unset! for its then-blk argument why ?
f: func['p [string! unset!]][
if unset? 'p print "unset"
]
'p evaluates to the word p. In order to test the type of the value referred by p, you need to use :p and provide a proper body block for if:
f: func ['p [string! unset!]][
if unset? :p [print "unset"]
]
>> f "123"
== none
>> f
unset
i am pretty new to perl, and i was trying to set up a web server which runs perl...
i did make it work with another script, but with this one i got this error:
Server error!
The server encountered an internal error and was unable to complete
your request.
Error message: End of script output before headers: index.pl
If you think this is a server error, please contact the webmaster.
Error 500
localhost Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
This is my script:
#!"C:\xampp\perl\bin\perl.exe"
use strict;
use warnings;
#Open file and define $pcontent as content of body.txt
open(FILE,"body.txt");
local $/;
my $pcontent = <FILE>;
close(FILE)
#Open file and define $ptitle as content of title.txt
open(FILE,"title.txt");
local $/;
my $ptitle = <FILE>;
close(FILE)
#open html code
print "Content-type: text/html\n\n";
print "<html>";
#set html page title
print "<head>";
print "<title>$ptitle</title>";
print "</head>";
print "<body>";
#set the <body> of the html page
if ($pcontent = ""){
print "
<H1>ERROR OCCURED!</h1>"
} else{
print $pcontent;
};
#close the html code
print "</body>";
print "</html>";
The reason it isn't working is because your Perl code has syntax errors which prevent it from compiling. You can check your code for syntax errors by running
perl -c yourscript.pl
And if we do that we find:
syntax error at yourscript.pl line 11, near ")
If we look at line 11, we see that the line before is missing a semicolon at the end of the statement.
close(FILE) # <--- need semicolon here.
But there are a few other problems with this script:
You should avoid the use of global filehandles (FILE) and instead use lexical filehandles. One advantage is that since they are automatically destroyed at the end of their scope (assuming no references) they will be automatically closed for you.
You should use the three-argument form of open which will help you catch certain bugs
You should check that your open succeeds and report an error if it doesn't
You should only localize $/ within a small block, otherwise it will affect other things in your program that you may not want it to
If this script grows to be anything other than a trivial example, you should use a templating system rather than printing a bunch of HTML.
Your conditional is wrong; you need to use the eq operator for string equality, or == for numerical equality. The = operator is for assignment.
Putting that all together, here is how I would write it:
use strict;
use warnings;
#Open file and define $pcontent as content of body.txt
my $pcontent = do {
open my $fh, '<', 'body.txt' or die "Can not open body.txt: $!";
local $/;
<$fh>;
};
#Open file and define $ptitle as content of title.txt
my $ptitle = do {
open my $fh, '<', 'title.txt' or die "Can not open title.txt: $!";
local $/;
<$fh>;
};
#open html code
print "Content-type: text/html\n\n";
print "<html>";
#set html page title
print "<head>";
print "<title>$ptitle</title>";
print "</head>";
print "<body>";
#set the <body> of the html page
if ($pcontent eq ""){
print "<H1>ERROR OCCURED!</h1>"
} else{
print $pcontent;
};
#close the html code
print "</body>";
print "</html>";
I was reading a tutorial on awk scripting, and observed this strange behaviour, Why this awk script while executing asks for a number repeatedly even with out a loop construct like while or for. If we enter CTRL+D(EOF) it stops prompting for another number.
#!/bin/awk -f
BEGIN {
print "type a number";
}
{
print "The square of ", $1, " is ", $1*$1;
print "type another number";
}
END {
print "Done"
}
Please explain this behaviour of the above awk script
awk continues to work on lines until end of file is reached. Since in this case the input (STDIN) never ends as you keep entering number or hitting enter, it causes an endless loop.
When you hit CTRL+D you indicate the awk script that EOF is reached there by exiting the loop.
try this and enter 0 to exit
BEGIN {
print "type a number";
}
{
if($1==0)
exit;
print "The square of ", $1, " is ", $1*$1;
print "type another number";
}
END {
print "Done"
}
From the famous The AWK Programming Language:
If you don't provide a input file to the awk script on the command line, awk will apply the program to whatever you type next on your terminal until you type an end-of-file signal (control-d on Unix systems).
sry opening a new thread but awk is driving me nuts! >< im trying to run a few command assignments inside a single awk but i cant get it to work please help if this is ez for u :P i can't get the syntax to work
edit: im using /bin/bash
for f in `seq $nlinpE $loopE`;
do
awk -F ","'
BEGIN {}
'$f' { dataI2[$f]=$2;
dataI3[$f]=$3;
dataI4[$f]=$4;
noD1[$f]=$dataI1[$f];
noD2[$f]=$dataI2[$f];
noD3[$f]=$dataI3[$f];
noD1i[$f]=`echo "$nlinpN1 + $dataI1"|bc -l`;
noD2i[$f]=`echo "$nlinpN1 + $dataI2"|bc -l`;
noD3i[$f]=`echo "$nlinpN1 + $dataI3"|bc -l`;
}
'${noD1i[$f]}' {
dataIi2[$f]=$2;
dataIi3[$f]=$3;
dataIi4[$f]=$4;
}
'${noD2i[$f]}' {
dataIii2[$f]=$2;
dataIii3[$f]=$3;
dataIii4[$f]=$4;
}
'${noD2i[$f]}' {
dataIiii2[$f]=$2;
dataIiii3[$f]=$3;
dataIiii4[$f]=$4;
}
END{}
' <aoa_5.inp;
done
input is like:
17, 3.22854114, 0.562598288, 0.384291202
18, 2.96085286, 0.085116826, 0.285071939
19, 3.40070796, 2.27838659, 0.302027524
20, 3.20035744, 0.333615214, 0.262585849
21, 2.85644341, 0.258691043, 0.369726121
22, 3.73537922, 1.3267405, 0.295917094
23, 3.69372559, 1.32601321, 0.306054831
24, 3.28857207, 0.63199228, 0.378117412
25, 3.27523994, 0.695856452, 0.377585977
imjust assigning variables atm, getting the number w/o commas
i get this syntax type of error:
awk: 9: unexpected character '`'
awk: 10: unexpected character '`'
awk: 11: unexpected character '`'
(standard_in) 2: syntax error
(standard_in) 2: syntax error
awk: line 1: syntax error at or near {
^C
thx
Maybe this will help you clean up your syntax a little so we can understand what you're trying to do.
BEGIN and END blocks are optional. Ignoring patterns for the moment, an awk program might look like this.
BEGIN {
# Things to be done before you start processing rows.
}
{
# Things to be done for each row.
}
END {
# Things to be done after processing the last row.
}
If you don't happen to need BEGIN or END blocks, it might look more like this.
{
# Things to be done for each row.
}
This awk program assigns the value of $2, $3, and $4 to the variable dataI, and prints it once for each row.
{
dataI = sprintf("%s %s %s", $2, $3, $4);
print dataI;
}
That assignment has no effect on the values of $2, $3, and $4.