How to calculate the number of characters on a line in Vimscript? - macvim

I need an attribute or function in Vimscript that does a certain task if the current line contains a certain number of characters. For instance:
if ‹chars_on_current_line› = 50 " for example
... perform task ...
endif
What can I use for the ‹chars_on_current_line› subexpression in Vimscript to get the length of the cursor line in characters?
(If it happens to help in any way, I'm using Macvim.)

To get the number of characters on the current line, use
virtcol('$')
For the number of bytes, use
col('$')

Related

Extrapolate two columns from txt with commas and text

I got a problem reading some data from a txt file. I appreciate any suggestion and thank you in advance!
I have a txt file with text/number on top, followed by two tab-separated columns (additionally, they have commas instead of dots).
I want to extract the two columns without text, and replace the commas with dots in order to plot them.
I tried with importdata to be able to replace the commas, but it separates every single character, so I get 36k elements instead of 2048.
Tried dlmread but it ignores the second column...
I have no idea how to proceed without modifying every single file manually.
here is an example of the file:
Data from FLMS012901__118__10-30-26-589.txt Node
Date: Tue Jul 05 10:30:26 CEST 2022
User: Myself
Number of Pixels in Spectrum: 2048
>>>>>Begin Spectral Data<<<<<
338,147 -2183,94
338,527 -2183,94
338,906 -2183,94
339,286 -2251,25
Any suggestions?
EDIT:
Apparently, there was already a solution, even though a bit slow:
% Read file in as a series of strings
fid = fopen('data.txt', 'rb');
strings = textscan(fid, '%s', 'Delimiter', '');
fclose(fid);
% Replace all commas with decimal points
decimal_strings = regexprep(strings{1}, ',', '.');
% Convert to doubles and join all rows together
data = cellfun(#str2num, decimal_strings, 'uni', 0);
data = cat(1, data{:});
On the sample that you provide, the following works:
>> [a,b,c,d] = textread("SO_73502149.txt","%f,%f %f,%f", "headerlines", 6);
>> format free
>> [a+b/1000, c+sign(c).*d/100]
ans =
338.147 -2183.94
338.527 -2183.94
338.906 -2183.94
339.286 -2251.25
However there are some possible traps, according to the way decimal figures are handled in your file, you should adapt the post-processing: If for 338.10 338,1 is printed in the file instead of 338,10 , the decoding would be a bit harder. Whenever c becomes zero, sign(c) would kill the decimal part. A less trivial post-processing would be required.

Function which removes html color from a string with sscanf

I've a big dilemma how can I do a condition to remove this type of color from my string (ex: {dd2e22}) using sscanf, which is only func I want to use. So the string provided will be some random text:
Te{dd2e22}xt is {3f53ec}here
The condition what I tried
sscanf(buf,"%[^\{[0-9a-fA-F]{6,8}\}]s",output);
This isn't working, the result are only first character "T".
Try using the format specifier:
"%*6X"
Analysis:
% -- starts a format specifier.
* -- tells scanf not to assign field to variable.
6x -- says that field is 6 hex digits.
See scanf format specifier
result are only first character "T".
Well, the next character is 'e', which matches the set \{[0-9a-fA-F]{6,8}\ and thus doesn't match the inverted set specified by '^'.
This task can be achieved with a regular expression. The standard library provides you with appropriate tools in the <regex> header.

Perl LibXML get last line number of Node

I use LibXML in Perl, which store the start line number of each node, but how i can get the last one?
I tried get last line number through..
..counting newlines in innerhtml of the node, but LibXML return the innerhtml in different formatting than original, so that the line number differ.
..node->getLastChild->line_number, but also havin no success.
Any Idea?
If line_number returned the first line of a node as you say, all you'd need is
my $s_line_num = $node->line_number();
my $e_line_num = $node->nextSibling()->line_number();
But it doesn't. What line_number returns is actually closer the number of the last line of the node. For that, we could simply look at the previous sibling's line number.
my $s_line_num = $node->previousSibling()->line_number();
my $e_line_num = $node->line_number();
But while that's what it returns for non-element nodes, it returns the last line number of the start tag (rather than of the element as a whole) for elements. That's completely useless.
Sorry, no can do!
If line_number returned the first line of a node as you say, all you'd need is
my $parser = XML::LibXML->new( XML_LIBXML_LINENUMBERS==>1);
my $e_line_num = $node->line_number();
=> 1 );

how I can fix this? python3.2

I'm trying to finish a program that converts text2bin and bin2text among other things, but I have a problem with the bin2text function, but only with some characters.
This is the part that is giving me problems:
def bin2text(self):
cadena2=' '
self.ventana.caja1.text()
split=[self.ventana.caja1.text()[x:x+8] for x in range(0,len(self.ventana.caja1.text()),8)]
for i in splits:
cadena2=cadena2+chr(int(i,2))
self.ventana.caja2.setText(cadena2)
I'm getting the error:
Invalid literal for int() with base 2 '100000 1.... '
on the line
cadena2=cadena2+chr(int(i,2))
(The ellipses dots aren't actually a part of the error but there's an arrow in the way in the figure)
when I try to go from binary to text, using "space" or any sign that the passing of ASCII to Bin, the number consists of six digits, I get the error. Seven or eight digit numbers work.
I need to know a how to add a 0 at the beginning of this series(0100000), to fix it
See figures below:
try this:
splits = self.ventana.caja1.text().split() #split by whitespace
for i in splits:
cadena2=cadena2+chr(int(i,2))
The problem is not the number of digits, the problem is the whitespace within the string you sent to 'int'

liblinear's train.exe: "Wrong input format at line 1"

I'm trying to run liblinear's train.exe on Windows:
>train ex1_train.txt
Wrong input format at line 1
Here's the beginning of the file. What's wrong?
17.592 1:6.1101
9.1302 1:5.5277
13.662 1:8.5186
11.854 1:7.0032
6.8233 1:5.8598
11.886 1:8.3829
4.3483 1:7.4764
12 1:8.5781
6.5987 1:6.4862
3.8166 1:5.0546
3.2522 1:5.7107
15.505 1:14.164
3.1551 1:5.734
7.2258 1:8.4084
0.71618 1:5.6407
3.5129 1:5.3794
5.3048 1:6.3654
0.56077 1:5.1301
3.6518 1:6.4296
5.3893 1:7.0708
Liblinear requires the same input format as LibSVM. And, from their README file,
The format of training and testing data file is:
<label> <index1>:<value1> <index2>:<value2> ...
Each line contains an instance and is ended by a '\n' character. For
classification, <label> is an integer indicating the class label
(multi-class is supported). For regression, <label> is the target
value which can be any real number. For one-class SVM, it's not used
so can be any number. The pair <index>:<value> gives a feature
(attribute) value: <index> is an integer starting from 1 and <value>
is a real number. The only exception is the precomputed kernel, where
<index> starts from 0; see the section of precomputed kernels. Indices
must be in ASCENDING order.
Since we don't have the entire file, the best answer we can provide is that make sure all these instructions are followed. E.g., there is no TAB instead of space, there is no '\r\n' instead of '\n', etc. A good way to debug would be to take a few lines and keep adding until you get the error.
head -10 <yourfile> > tmp10
head -20 <yourfile> > tmp20
etc. And see where the error pops up.
My problems were that: you can't use zero as a feature id, and your features need to be sorted.