Run f# script with parameters - f#-interactive

I try to run f# script using fsi.exe via batch file
CALL "path\fsi.exe" --quiet --exec --use:"path\FindAndDelete.fsx" arg1 arg2 arg3
I need my script program operate with arg1 arg2 and arg3. But the command:
for arg in Environment.GetCommandLineArgs() do
printfn "%s" arg
prints all the parameters and say that arg1 is wrong parameter,but I need to operate only with arg1, arg2,arg3 in my script. Although what is the best way to run f# script with parameters and operate with them?

I personally use fsi.CommandLineArgs. The 0th argument is the filename so you can use Array.tail to get the args.
let args : string array = fsi.CommandLineArgs |> Array.tail
let first = args.[0]
let second = args.[1]
Alternatively if you do decide to use GetCommandLineArgs() it looks like the first argument is the 4th element in the list, aka args.[3]
fsi --exec outputGetCommandlineArgs.fsx woah yay no
outputs
[|"pathtofsi\fsi.exe"; "--exec"; "outputGetCommandlineArgs.fsx"; "woah"; "yay"; "no"|]

Related

Redis Lua script - how to pass array as argument to a Lua script in nodejs?

I am calling a Lua script from nodejs. I want to pass an array as argument. I am facing problem to parse that array in Lua.
Below is an example:
var script = 'local actorlist = ARGV[1]
if #actorlist > 0 then
for i, k in ipairs(actorlist) do
redis.call("ZADD","key", 1, k)
end
end';
client.eval(
script, //lua source
0,
['keyv1','keyv2']
function(err, result) {
console.log(err+'------------'+result);
}
);
It gives me this error:
"ERR Error running script (call to f_b263a24560e4252cf018189a4c46c40ce7d1b21a): #user_script:1: user_script:1: bad argument #1 to 'ipairs' (table expected, got string)
You can do it just by using ARGV:
local actorlist = ARGV
for i, k in ipairs(actorlist) do
and pass arguments in console like this:
eval "_script_" 0 arg1 arg2 argN
You can only pass in strings into Redis lua script.
If you need an array of values to be pass into Redis lua script, you can do this:
let script = `
if #ARVG > 0 then
for i, k in ipairs(ARGV) do
redis.call("ZADD","key", 1, k)
end
end`;
client.eval(
script, //lua source
0,
...['keyv1','keyv2'],
function(err, result) {
console.log(err+'------------'+result);
}
);
The key here is to pass keyv1 and keyv2 as a separate params when calling eval. (I am using es6 syntax here)

In Scala, read a file where specified path includes multiple environment variables

For a file path name such as
val path = "$HOME/projects/$P1/myFile.txt"
is there a simpler way to resolve the path and read myFile.txt than this,
import java.io.File
val resolvedPath = path.split(File.separator).map{ s =>
if (s.startsWith("$")) sys.env(s.drop(1))
else s }.
mkString(File.separator)
val res = io.Source.fromFile(resolvedPath).getLines
The way you have seems good to me, but if you are so inclined or need to do something quickly, you could use Process to get the return of executing a bash command:
import scala.sys.process._
val cleanedPath = Seq("bash", "-c", "echo " + path).!!.trim
You can even use this idea to read the file if you want:
val text = Seq("echo", "-c", "cat " + path).!!
One difference between these and your code is that your code will throw an exception if an environment variable is missing, while bash returns an empty string for that variable. If you wish to mimic that, you could use sys.env.get(s.tail).getOrElse("") instead of sys.env(s.drop(1)) or use the dictionary val myEnv = sys.env.withDefaultValue("").
See System.getenv(). You'll be able to find the variables and replace them with the value to resolve your path.

How to execute octave code line-by-line

If I start an octave interactive shell, I can type:
octave:1> function y = foo(x)
> y = x + 2;
> endfunction
octave:2> foo(7)
ans = 9
The interpreter knows to wait for the rest of the function definition.
However, if I do
octave:1> eval("function y = foo(x)");
octave:2> eval("y = x + 2;");
octave:3> eval("endfunction");
it evaluates each line as if it were alone. So it defines a function foo which does nothing, and gives errors for the second two lines.
Is there any way to get eval to operate the same as the interpreter? Ultimately, I would like to create an octave script which executes another script, but is able to do other things in between. Is there any way to tell eval to wait for the rest of the command (the way the interactive environment does)? Alternatively, is there a way to feed commands to the interactive interpreter programmatically?
Thank you.
To answer your exact question, I see two immediate ways to do it:
octave> eval ("function y = foo(x) ...
y = x + 2; ...
endfunction")
octave> eval ("function y = foo(x)\n y = x + 2;\n endfunction")
The thing is that you can't split each line in multiple evals, it doesn't make sense. You want to pass a single string with all of the code. Also, you can use the source function to execute code from other files.
Without knowing all the details of what're you're trying to do, I'm guessing you could have your code use input to wait for input from the other code. Or just turn your other script into functions, and call them from your main script.

How to pass index of array in cmd line argument which is function pointer and call to specific function in perl?

Suppose I have 2 functions in Perl. I would create a array of references of that two functions. & in command line argument I'll pass only that index of array which call specific function and if I don't give any argument then it'll call all functions which referenced were in array(Default case).
So, can any help me to do this?
## Array content function pointers
my #list= {$Ref0,$Ref1 }
my $fun0Name = “fun0”;
my $Ref0 =&{$fun0Name}();
my $fun1Name = “fun1”;
my $Ref1 =&{$fun1Name}();
#### Two functions
sub fun0() {
print "hi \n";
}
sub fun1() {
print "hello \n";
}
##### Now in cmd argument if i passed Test.pl -t 0(index of array ,means call to 1st function)
##### if i give test.pl -t (No option ) ....then i call both function.
Creating a function pointer (called a code reference in Perl) is easy enough:
sub foo {
say "foo!";
}
sub bar {
say "bar!";
}
my $foo_ref = \&foo;
my $bar_ref = \&bar;
Putting things in an array is pretty easy:
my #array = ( $foo_ref, $bar_ref );
Reading arguments from the command line is pretty easy:
my $arg = shift #ARGV;
Looking things up in an array is also pretty easy:
my $item = $array[$arg];
Which part are you having trouble with?

Perl IO::Pipe does not work within arrays

im trying the following:
I want to fork multiple processes and use multiple pipes (child -> parent) simultaneously.
My approach is to use IO::Pipe.
#!/usr/bin/perl
use strict;
use IO::Pipe;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my #ua_processes = (0..9);
my $url = "http://<some-sample-textfile>";
my #ua_pipe;
my #ua_process;
$ua_pipe[0] = IO::Pipe->new();
$ua_process[0] = fork();
if( $ua_process[0] == 0 ) {
my $response = $ua->get($url);
$ua_pipe[0]->writer();
print $ua_pipe[0] $response->decoded_content;
exit 0;
}
$ua_pipe[0]->reader();
while (<$ua_pipe[0]>) {
print $_;
}
In future i want to use multiple "$ua_process"s in an array.
After execution i got the following errors:
Scalar found where operator expected at ./forked.pl line 18, near "] $response"
(Missing operator before $response?)
syntax error at ./forked.pl line 18, near "] $response"
BEGIN not safe after errors--compilation aborted at ./forked.pl line 23.
If i dont use arrays, the same code works perfectly. It seems only the $ua_pipe[0] dont work as expected (together with a array).
I really dont know why. Anyone knows a solution? Help would be very appreciated!
Your problem is here:
print $ua_pipe[0] $response->decoded_content;
The print and say builtins use the indirect syntax to specify the file handle. This allows only for a single scalar variable or a bareword:
print STDOUT "foo";
or
print $file "foo";
If you want to specify the file handle via a more complex expression, you have to enclose that expression in curlies; this is called a dative block:
print { $ua_pipe[0] } $response-decoded_content;
This should now work fine.
Edit
I overlooked the <$ua_pipe[0]>. The readline operator <> also doubles as the glob operator (i.e. does shell expansion for patterns like *.txt). Here, the same rules as for say and print apply: It'll only use the file handle if it is a bareword or a simple scalar variable. Otherwise, it will be interpreted as a glob pattern (implying stringification of the argument). To disambiguate:
For the readline <>, we have to resort to the readline builtin:
while (readline $ua_pipe[0]) { ... }
To force globbing <>, pass it a string: <"some*.pattern">, or preferably use the glob builtin.

Resources