How to write multiple function calls in multiple lines in PowerShell? - function

I'm currently playing around on PowerShell, and I'm wondering how I can call multiple functions after eachother, but giving them each their own line.
I currently have this:
$leet= $text.replace("a", "4").replace("e", "1").replace("e", "3");
But I want it more like this:
$leet= $text
.replace("a", "4")
.replace("e", "1")
.replace("e", "3");
But PowerShell doesn't really like the newlines, and it doesn't work either when I add the ` to the end of each line followed by another one.
So, am I missing something, or is this not possible in PowerShell?

As Jeroen Mostert notes you can get line continuation for free by specifying the operator (.) and then placing the whitespace between it and the right-hand operand:
$leet = $text.
Replace("a", "4").
Replace("e", "1").
Replace("e", "3")
(note that the last call, Replace("e", "3"), does nothing - all the e's have already been replaced by the preceding call)

Related

Extract the value of a variable that matches a pattern in TCL

I have a list called parameters and the content of this list can be different but it will look something like:
var1=2;
var2=2'h2;
var3=2'h0;
....
This list comes from reading a file and done some preformating already. I just want to grab the value of var1 and store it in a variable. Eg whatever is in between '=' sign and ';' sign but only for var1 (in this case number 2). Equally I can remove all the lines that are not matching 'var1'.
Assuming your parameters list is already set, you can do something like:
foreach item $parameters {
if {[regexp "var1\\s*=\\s*(\\w+);" $item wholeMatch myVal]} {
break
}
}
puts "value is '$myVal'"
The regular expression I use allows for optional spaces before and after the equal sign. Take a look at Tcl's regex syntax and adjust as necessary.
It might be easier to just do a regex search through your whole file using, rather than parsing your file into a list. But again, take a look at Tcl's documentation.

Octave 4.0 crashing on audiowrite

I'm trying to split up the long line of audiowrite but when I do Octave 4.0 crashes I'm using Octave 4.0 (which is like matlab) on Ubuntu 16.04 64bit.
audio_prop='BitsPerSample',16,'Artist','artist rt','Title','title section rt','Comment','Comments section rt';
audiowrite('/tmp/test.wav',[sig_full_L(:) -1*sig_full_R(:)],44100,audio_prop)
Can I not split it up this way if not how can I split it up?
Someone asked for the full code so here it is (this causes octave 4.0 to crash)
fs =8000; % Sampling frequency
fs_rate=fs;
dursec=10; %%duration of signal in seconds
t=linspace(0,2*pi,dursec*fs);
freq=primes(fs/2*dursec);
freq=freq';
ya=zeros(1,length(t));
numfreq=numel(freq)
for ii=1:1:numel(freq)
ya = ya+sin(freq(ii,1)*t);
end
audio_prop='BitsPerSample',16,'Artist','artist rt','Title','title section rt','Comment','Comments section rt';
audiowrite('/tmp/test.flac',[ya(:) -1*ya(:)],44100,audio_prop)
I was told by the octave people to fill out a crash / bug report link below
https://savannah.gnu.org/bugs/index.php?47875
Here is one way to wrap a long list of arguments into a single variable,
in order to shorter lines or to allow for reuse in a further function.
First put the arguments in a cell array.
options = {"a", "b", 2};
Then use the cell array expansion, which is a bit hidden there :
Accessing multiple elements of a cell array with the ‘{’ and ‘}’
operators will result in a comma separated list of all the requested
elements
In our case,
options{:}
is interpreted as
"a", "b", 2
so calling
func(arg1, arg2, options{:})
is interpreted as
func(arg1, arg2, "a", "b", 2)
Which answers the question.
#ederag solved this for me. "Even the first line is wrong. Brackets ({})are missing around it. And it should be audio_prop{:} in the second line."
The code with the error should be:
audio_prop={'BitsPerSample',16,'Artist','artist rt','Title','title section rt','Comment','Comments section rt'};
audiowrite('/tmp/test.wav',[sig_full_L(:) -1*sig_full_R(:)],44100,audio_prop{:})

how to set text indices via a variable

i'm trying to implement a simple line highlighting mechanism in my tcl/tk text widget.
For this I would like to assign all characters marked with one tag to another tag.
as in
.window.text insert end "one line\n" line1
.window.text insert end "a chunk spanning\nmultiple lines" line2
.window.text insert end "thats all\n" line3
# get all text that is tagged as 'line2'
set selected [ .window.text tag ranges line2 ]
# and apply the 'highlighed' tag to it:
.window.text tag add highlighted $selected
Unfortunately this does notwork, as it gives me
bad text index "2.0 4.0"
Using the indices literally works fine:
.window.text tag add highlighted 2.0 4.0
But is not what i want. (I don't know anything about the tagged chunks apart from their tag)
So it seems that I cannot store the list of indices in a variable and use that with tag add (or tag remove for that matter).
Any hints how I can add a tag to an already tagged text?
Solution (in Tcl 8.5 and later):
.window.text tag add highlighted {*}$selected
If command A has given you a list of items to feed to command B, but command B expects each item to appear as an argument in its invocation, the list of items needs to be spliced, or expanded into separate arguments. In Tcl 8.5, this was facilitated by introducing a new syntactic rule that allowed the number of arguments provided to a command to be increased by expanding one of the existing arguments.
To borrow an example, the destroy ?window window ...? command cannot work with the list of windows returned by winfo children ., since each window path needs to be a separate argument. Writing
destroy [winfo children .]
would be evaluated as (say) destroy {.foo .bar .baz}, which won't work. However, using the new expansion prefix {*}
destroy {*}[winfo children .]
the line will be evaluated as destroy .foo .bar .baz, which will work.
One way to understand it is by thinking of the invocation as a list consisting of the command name and the arguments, and that the {*} is an instruction to splice the value of the following argument into that list at that point in the list.
Documentation: {*}

How to concatenate several lines in vim

I am using a the vim-screen plugin that enable me to write scripts , start an interpreter in the same window and send lines the the interpreter. Problem is that the interpreter do not accept statements written on several lines.
exemple:
This will work f:{[x] y:y+1; Z:y+1; :Z; };
But this won't
f:{[x] y:y+1;
Z:y+1;
:Z;
};
How can I write a vim function that I could call to reshape the lines in order to be sent to the interpreter?
EDIT:
I had no success in making this function, I would like to create a function that would, from a input like this (that would be visually selected)
F:{[a;b;r]
//ccc1
aaa1;
aaa2;
//ccc2
aaa3;
};
output something like this F:{[a;b;r] aaa1; aaa2; aaa3; };
So I created a bounty
If you want to actually modify the buffer, J / :join do that. If you just want to join the lines that are sent to the interpreter (but keep them split in the buffer), you can retrieve the selected lines with getline(), and then join() them. Here's an example command:
:command! -range Invoke echomsg join(getline(<line1>,<line2>), '')
Edit
Based on that, you can "massage" the List of lines returned by getline(). E.g. to ignore the commented lines:
:command! -range Invoke echomsg join(filter(getline(<line1>,<line2>), 'v:val !~# "^\\s*//"'), '')
Additionally strip leading whitespace (this becomes unwieldy in a single line; better use a function now):
:command! -range Invoke echomsg join(map(filter(getline(<line1>,<line2>), 'v:val !~# "^\\s*//"'), 'substitute(v:val, "^\\s\\+", " ", "g")'), '')
Standard continuation character in vimscript scripts is backslash in the beginning of the next line. So, this
f:{[x] y:y+1;
\ Z:y+1;
\ :Z;
\ };
should work.

Trying to redirect output of a command to a variable

>> set signal_name [get_fanout abc_signal]
{xyz_blah_blah}
>> echo $signal_name
#142
>> set signal_name [get_fanout abc_signal]
{xyz_blah_blah}
>> echo $signal_name
#144
>>
I tried other stuff like catch etc, and every where, it returns #number. My goal is to be able to print the actual value instead of the number - xyz_blah_blah.
I am new to tcl. Want to understand, if this is an array or a pointer to an array or something like that. When I try the exact same thing with a different command, which returns just a value, then it works. This is a new command which returns value in parenthesis.
Please help. Thanks.
Every Tcl command produces a result value, which you capture and use by putting the call of the command in [square brackets] and putting the whole lot as part of an argument to another command. Thus, in:
set signal_name [get_fanout abc_signal]
the result of the call to get_fanout is used as the second argument to set. I suggest that you might also like to try doing this:
puts "-->[get_fanout abc_signal]<--"
It's just the same, except this time we're concatenating it with some other small string bits and printing the whole lot out. (In case you're wondering, the result of puts itself is always the empty string if there isn't an error, and set returns the contents of the variable.)
If that is still printing the wrong value (as well as the right one beforehand, without arrow marks around it) the real issue may well be that get_fanout is not doing what you expect. While it is possible to capture the standard output of a command, doing so is a considerably more advanced technique; it is probably better to consider whether there is an alternate mechanism to achieve what you want. (The get_fanout command is not a standard part of the Tcl language library or any very common add-on library like Tk or the Tcllib collection, so we can only guess at its behavior.)