vim9script: dump file content to list of list question !

26 views
Skip to first unread message

Ni Va

unread,
Jan 15, 2021, 8:38:23 AM1/15/21
to vim_use
Hi,

I got this kind of classical logfile :

2021/01/14 07:42:22.588 Information Foo dbg 
2021/01/14 07:42:22.588 Information Bar dbg 
2021/01/14 07:42:22.588  Information    Foobar dbg 
2021/01/14 07:42:22.588  Information    Barbar dbg 
..
.

and would like to add all lines' informations split by space into dict or array in vim9script.
['2021/01/14', '07:42:22.588', 'Information  Foo dbg   ']

trying map(getline(1, '$'), ' v:val->split() ') it returns list<list<string>> but impossible to declare var foo: list<list<string>>

How can I fix it ?

Best Wishes 2021
NiVa

c.wil...@btinternet.com

unread,
Jan 15, 2021, 9:36:24 AM1/15/21
to vim...@googlegroups.com
Hi

not quite clear on what you want to achieve. If it's lines like the square bracketed example (rather than internal Vim arrays), then you cd use 4 :%s constructs:

:%s/^/['/
:%s/$/']/
:%s/ /','/
:%s/ /','/

which cd of course be canned in a single command. Note not to use a global instead of the two single replacements.

regards - Chris

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unsub...@googlegroups.com">vim_use+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/051f67c9-a061-46a7-9c3b-794b563ea565n%40googlegroups.com.

Jürgen Krämer

unread,
Jan 18, 2021, 2:34:34 AM1/18/21
to vim...@googlegroups.com

Hi,

Ni Va schrieb am 15.01.2021 um 14:38:
>
> I got this kind of classical logfile :
>
> 2021/01/14 07:42:22.588 InformationFoo dbg 
> 2021/01/14 07:42:22.588 InformationBar dbg 
> 2021/01/14 07:42:22.588 Information    Foobar dbg 
> 2021/01/14 07:42:22.588 Information    Barbar dbg 
> ..
> .
>
> and would like to add all lines' informations split by space into dict or array in vim9script.
> ['2021/01/14', '07:42:22.588', 'Information  Foo dbg   ']
>
> trying map(getline(1, '$'), ' v:val->split() ') it returns list<list<string>> but impossible to declare var foo: list<list<string>>
>
> How can I fix it ?

you need to use mapnew() instead of map(), because the item type changes.

getline() returns a list of strings. The supplied expression would replace
every item of type string by an item of type list<string>. In Vim9 script
this is not allowed. Instead you need to use mapnew() which creates a new
list which is then filled with the results of the mapping expression.

Regards,
Jürgen

Ni Va

unread,
Jan 18, 2021, 1:39:18 PM1/18/21
to vim_use
Hi Jürgen and thank you.

  •  this seems to work well !
             var bigData_records = mapnew(getline(1, '$'), '' v:val->split() ' )

  • Considering this kind of records:
2021/01/14 07:42:22.588 InformationFoo dbg 
2021/01/14 07:42:22.588 InformationBar dbg  
 

I wonder if I can build big dict in one line
  • first key level is day
  • second key level would be hour stamped  
  • value is list of rest of infos
It should look like that inline:

mapnew(getline(1, '$'),     ' g:bigData[v:val->split()[0][v:val->split()[1]] = v:val->split()[2 : -1]  ')

Actually I have a more developped vimscript that ensure existence of keys ...

var lines: list<string> = getline(1, '$')     
var l: list<string> = []                      
var day: string = ''                          
var hour: string = ''                         
var infos: string = ''                        
var hourDict:  dict<any> = {}                 
var infosList: list<any> = []                 
                                              
for line in lines[0 : g:dataRows]             
                                              
  l     = line->split()                       
  day   = l[0]                                
  hour  = l[1]                                
  infos = l[2 : ]->join()                     
  #echomsg day .. ' ' .. hour .. ' ' .. infos 
                                              
  # Hours' dict                               
  if !has_key(hourDict, hour)                 
    hourDict[hour] = [infos]                  
  else                                        
    hourDict[hour]->add(infos)                
  endif                                       
                                              
  # Days' dict                                
  if !has_key(g:dataD, day)                   
    g:dataD[day] = hourDict                   
  endif                                       
                                              
endfor                                        
Reply all
Reply to author
Forward
0 new messages