vim how to transform text in this way - vim

I want to transform this following text in a single line from
[12.2,3.3 ,8.1 ,9., 12.4]
to
5
12.2
3.3
8.1
9.
12.4
or from
[def, abc , ghi ]
into
3
def
abc
ghi
So, essentially, removing [ and ], breaking elements based on , and remove leading or trailing spaces, and then put the number of elements as the first line (5 in the first example and 3 in the second) and each element in the following lines.
How to write a command to do this fast in vim?

Steps
1.Strip spaces in the list:
:s/ //g
2.Add number of elements to the start:
:s/\v\[(([^,]+)*[^\]]+)\]/\=len(split(submatch(1), ',')).','.submatch(1)/
3.Replace , with \r:
:s/,/\r/g
Results
After first step:
[12.2,3.3,8.1,9.,12.4]
After second step:
5,12.2,3.3,8.1,9.,12.4
After last step:
5
12.2
3.3
8.1
9.
12.4

If you are ok with python(installed in your os and activated in vim) you can:
edit the text to:
a=[your list];print(len(a));
for x in a:print(x)
enter visual mode and select all the text
just type :!python and it should transform your list to the desired shape, replacing the original
P. S.: the string need to be quoted in order for this to work

Related

How to find and replace the first occurrence of any single text character (Excel)

I need to replace or substitute the first instance of a single text character in an excel row.
current: B01 TEST TEST TEST A W B 0 A
expected result where first "A" that is on its own is replaced with "|": B01 TEST TEST TEST | W B 0 A
The issue is, each row has a character that is segmented on its own, but they are all different (some A, some W, some R, etc). Which function can I use to look for the first instance of a single text character surrounded by spaces?
In Office 365 you could use =AGGREGATE(15,6,FIND(" "&CHAR(SEQUENCE(26,,65))&" ",A19),1)
Older version: =AGGREGATE(15,6,FIND(" "&{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}&" ",A19),1)
Edit: better version suggested by Mayukh
=AGGREGATE (15,6,FIND(" "&CHAR(ROW($65:$90))&" ",A19),1)
This uses Office 365 LET and SEQUENCE and is not dependent on it being a capital character. It will replace the first character whether alpha, numeric or special that has a space on either side of it with the |:
=LET(rng,A1,
sq,SEQUENCE(LEN(rng)-3),
md,MID(rng,sq,3),
lft,LEFT(md),
rt,RIGHT(md),
REPLACE(rng,MIN(IF((lft=" ")*(rt=" "),sq+1)),1,"|"))

6502 BASIC removing a character

How do I delete a character in 6502 basic? Like reverse print...
Would there be a way to channel the del key code into a program? I have tried looking at the reference but there is nothing there for a command.
As OldBoyCoder suggests, it can be done - but the character depends on the platform.
10 PRINT "AB";
20 PRINT "C"
Output on Commodore PET or Apple II:
ABC
For Commodore PET add:
15 PRINT CHR$(20);
Output:
AC
On the Apple II you'd need to use 8 instead of 20 because the I/O firmware is different.
You can also use the string manipulation commands, such as LEFT$, RIGHT$ and MID$, here's an untested example from memory (for the Commodore PET and other Commodore 8-bit machines, probably also works on other variants of BASIC):
0 A$="ABC"
1 PRINT LEFT$(A$,2): REM PRINTS AB
2 PRINT RIGHT$(A$,2): REM PRINTS BC
3 PRINT MID$(A$,2,1): REM PRINTS B
Otherwise, you can over-write characters as long as you know where they are located on the screen.
0 D$=CHR$(17): REM CURSOR DOWN
1 U$=CHR$(145): REM CURSOR UP
2 R$=CHR$(29): REM CURSOR RIGHT
3 L$=CHR$(157): REM CURSOR LEFT
4 PRINT "ABC";L$;" "
I'm using CHR$ codes here for clarity. If you open a string with a double-quote, you can press the UP/DOWN and LEFT/RIGHT keys which will show inversed characters. Printing these inversed characters will do the same as moving the cursor around with the cursor keys.

Comma separated combination of LTR/RTL/Digit characters reorder issue

I have a comma separated list of values generated from an excel sheet. (Numbers and RTL Characters)
Having these values in columns: 1 | 2 | 3 | 4 | 5
would yield me the output of 1,2,3,4,5
But the issue arises when I have RTL characters (Persian/Arabic) in my columns: 1 2 ب الف and a 5 in the end.
Now the output becomes 1,2الف, ب, 5
Since my columns can have multiple sets of RTL characters it can really mess up the output to the point that it's no more trivial to fix it by substituting several inputs.
What are my options to produce a csv file with the right order?
Tools I used where javascript and excel and both had the same issue.
If your purpose is to only display the CSV for human eye, you can add ‏RIGHT-TO-LEFT MARK (‏) before each number:
‏1, ‏2, ب, الف, ‏5
‏1, ‏2, ب, الف, ‏5
Note that these characters may drive crazy any tool you use to parse the CSV.
I think your CSV file already has the right order. In the text you pasted in the question:
1,2الف, ب, 5
The "1" is the first character in the string, and the "5" is the last. It just doesn't seem that way to you because the first half of the string (1,2) is rendering LTR whereas the second half of the string (الف, ب, 5) is rendering RTL.

=IF(ISNUMBER(SEARCH.... the difference between 1 and 11

I am trying to convert a single column of numbers in excel to multiple depending on the content.
e.g. Table 1 contains 1 column that contains 1 or more numbers between 1 and 11 separated with a comma. Table 2 should contain 11 columns with a 1 or a 0 depending on the numbers found in Table 1.
I am using the following formula at present:
=IF(ISNUMBER(SEARCH("1",A2)),1,0)
The next column contains the following:
=IF(ISNUMBER(SEARCH("2",A2)),1,0)
All the way to 11
=IF(ISNUMBER(SEARCH("11",A2)),1,0)
The problem with this however is that the code for finding references to 1 also find the references to 11. Is it possible to write a formula that can tell the difference so that if I have the following in Table 1:
2, 5, 11
It doesn't put a 1 in column 1 of Table 2?
Thanks.
Use, for list with just comma:
=IF(ISNUMBER(SEARCH(",1,", ","&A2&",")),1,0)
If list is separated with , (comma+space):
=IF(ISNUMBER(SEARCH(", 1,", ", "&A2&",")),1,0)
A version of LS_dev's answer that will cope with 0...n spaces before or after each comma is:
=IF(ISNUMBER(SEARCH(", 1 ,",", "&TRIM(SUBSTITUTE(A2,","," , "))&" ,")),1,0)
The SUBSTITUTE makes sure there's always at least one space before and after each comma and the TRIM replaces multiple spaces with one space, so the result of the TRIM function will have exactly one space before and after each comma.
How about using the SUBSTITUTE function to change all "11" to Roman numeral "XI" prior to doing your search:
=IF(ISNUMBER(SEARCH("1",SUBSTITUTE(A2, "11", "XI"))),1,0)
If you want to eliminate "11" case, but this is all based on hardcoded values, there should be a smarter solution.
=IF(ISNUMBER(SEARCH(AND("1",NOT("11")),A2)),1,0)

Align at longest word

I have the following code:
a = 123
p.value 0.123
p.long.name = "abc"
How can I align each line like shown below in vim?
a = 123
p.value = 0.123
p.long.name = "abc"
Thanks for any hints.
Without plugin:
:%s/=/ &/
:%s/\%13c\s\+=/=
First command will insert spaces before first equal signs on all lines, second one will remove all spaces before an equal sign at 13th column. You could also use Visual block selection and <..... to shift left as many times as necessary.
However this is really unclean. With the tabular plugin you just type :Tab /=/ and this will do the work and the range will be calculated automatically (greatest range around the cursor in which all lines match the pattern).

Resources