Monday, January 18, 2021

Re: vim9script: dump file content to list of list question !

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                                        


Le lundi 18 janvier 2021 à 08:34:34 UTC+1, jottkaerr@googlemail.com a écrit :

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

--
--
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 vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/b2a289af-1c08-4ff9-8a4a-17ae510cded1n%40googlegroups.com.

No comments:

Post a Comment