Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
from lines to dict keys
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Marc Chantreux  
View profile  
 More options Nov 9, 4:52 am
From: Marc Chantreux <kha...@phear.org>
Date: Mon, 9 Nov 2009 10:52:31 +0100
Local: Mon, Nov 9 2009 4:52 am
Subject: from lines to dict keys
hello guys,

i want each lines of the output of a shell command to become a key of a
dict. I wote this code:

let t = {}
for k in split( system("echo foo; echo bar "), '\n' )
    let t[k] = 1
endfor

but i don't like it: as the loop is here to populate the dictionnary, i
would like to use something more appropriate. In perl for exemple, the
map function enables you to write

my %a = map { $_ => 1 } split /\n/, qx< echo foo; echo bar>

vim has the equivalent of $_: it's called v:val, so i tried to use map
or filter with attempts looking like that:

let t = map( split( system("echo foo; echo bar "), '\n' ), { v:val : 1 } )

and yet i just wonder if it's possible.

regards,
marc


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Wokula  
View profile  
 More options Nov 9, 12:33 pm
From: Andy Wokula <anw...@yahoo.de>
Date: Mon, 09 Nov 2009 18:33:24 +0100
Local: Mon, Nov 9 2009 12:33 pm
Subject: Re: from lines to dict keys
Marc Chantreux schrieb:

It is possible, the second argument to map() must be a string:
    :h map()
    :let t = map(["foo", "bar"], '{v:val : 1}')

--
Andy


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Chantreux  
View profile  
 More options Nov 9, 7:30 pm
From: Marc Chantreux <kha...@phear.org>
Date: Tue, 10 Nov 2009 01:30:26 +0100
Local: Mon, Nov 9 2009 7:30 pm
Subject: Re: from lines to dict keys
hello Andy and thanks for reply.

On Mon, Nov 09, 2009 at 06:33:24PM +0100, Andy Wokula wrote:
> It is possible, the second argument to map() must be a string:
>     :h map()
>     :let t = map(["foo", "bar"], '{v:val : 1}')

i tried this solution but the result is

[{'foo': 1}, {'bar': 1}]

when i expect

{'foo': 1, 'bar': 1 }

regards

marc


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Wokula  
View profile  
 More options Nov 10, 6:19 am
From: Andy Wokula <anw...@yahoo.de>
Date: Tue, 10 Nov 2009 12:19:19 +0100
Local: Tues, Nov 10 2009 6:19 am
Subject: Re: from lines to dict keys
Marc Chantreux schrieb:

Sorry, I didn't understand (dunno Perl).
I think you can't do it so nicely in Vim, but you can try the
following:

" helper to keep the list unchanged (not required):
func! KeepVal(_)
    return v:val
endfunc

let in_list = ["foo", "bar"]
let out_dict = {}
call map(in_list, 'KeepVal(extend(out_dict, {v:val : 1}))')

" not so nice:
" - extra command for initialising out_dict
" - return value of map() is useless here

--
Andy


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Brabandt  
View profile  
 More options Nov 10, 6:26 am
From: "Christian Brabandt" <cbli...@256bit.org>
Date: Tue, 10 Nov 2009 12:26:34 +0100 (CET)
Local: Tues, Nov 10 2009 6:26 am
Subject: Re: from lines to dict keys

On Tue, November 10, 2009 12:19 pm, Andy Wokula wrote:
> Sorry, I didn't understand (dunno Perl).
> I think you can't do it so nicely in Vim, but you can try the
> following:

> " helper to keep the list unchanged (not required):
> func! KeepVal(_)
>     return v:val
> endfunc

> let in_list = ["foo", "bar"]
> let out_dict = {}
> call map(in_list, 'KeepVal(extend(out_dict, {v:val : 1}))')

You don't need KeepVal():
:call map(copy(in_list), 'extend(out_dict, {v:val : 1})')

regards,
Christian


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Wokula  
View profile  
 More options Nov 10, 6:41 am
From: Andy Wokula <anw...@yahoo.de>
Date: Tue, 10 Nov 2009 12:41:55 +0100
Local: Tues, Nov 10 2009 6:41 am
Subject: Re: from lines to dict keys
Christian Brabandt schrieb:

I just didn't like getting a list full of dict references.  And the
return value of map() will be less "junky".

--
Andy


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Brabandt  
View profile  
 More options Nov 10, 6:55 am
From: Christian Brabandt <cbli...@256bit.org>
Date: Tue, 10 Nov 2009 12:55:26 +0100
Local: Tues, Nov 10 2009 6:55 am
Subject: Re: from lines to dict keys
Hi Andy!

On Di, 10 Nov 2009, Andy Wokula wrote:

> Christian Brabandt schrieb:
> > On Tue, November 10, 2009 12:19 pm, Andy Wokula wrote:
> >> let in_list = ["foo", "bar"]
> >> let out_dict = {}
> >> call map(in_list, 'KeepVal(extend(out_dict, {v:val : 1}))')

> > You don't need KeepVal():
> > :call map(copy(in_list), 'extend(out_dict, {v:val : 1})')

> I just didn't like getting a list full of dict references.  And the
> return value of map() will be less "junky".

Okay, how about

:let @_=string(map(copy(in_list), 'extend(out_dict, {v:val : 1})'))

regards,
Christian
--
  • EFI is this other Intel brain-damage (the first one being ACPI).
       Torvalds, Linus (2006-07-24).


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Link  
View profile  
 More options Nov 10, 7:24 am
From: Tom Link <micat...@gmail.com>
Date: Tue, 10 Nov 2009 04:24:04 -0800 (PST)
Local: Tues, Nov 10 2009 7:24 am
Subject: Re: from lines to dict keys

> :let @_=string(map(copy(in_list), 'extend(out_dict, {v:val : 1})'))

I personally don't think it is a good idea to use map for iterating
over a list since it manipulates the list it is working on and returns
that transformed list.

Since vimscripts provides no high-order function to iterate over a
list without modifying it, what's wrong with a dull looking for-loop:

for k in list
 let dict[k] = 1
endfor


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Link  
View profile  
 More options Nov 10, 7:30 am
From: Tom Link <micat...@gmail.com>
Date: Tue, 10 Nov 2009 04:30:54 -0800 (PST)
Local: Tues, Nov 10 2009 7:30 am
Subject: Re: from lines to dict keys

> what's wrong with a dull looking for-loop:

Okay, that was the original solution anyway. I'd say stick to it.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andy Wokula  
View profile  
 More options Nov 10, 7:38 am
From: Andy Wokula <anw...@yahoo.de>
Date: Tue, 10 Nov 2009 13:38:36 +0100
Local: Tues, Nov 10 2009 7:38 am
Subject: Re: from lines to dict keys
Tom Link schrieb:

>> :let @_=string(map(copy(in_list), 'extend(out_dict, {v:val : 1})'))

> I personally don't think it is a good idea to use map for iterating
> over a list since it manipulates the list it is working on and returns
> that transformed list.

> Since vimscripts provides no high-order function to iterate over a
> list without modifying it, what's wrong with a dull looking for-loop:

> for k in list
>  let dict[k] = 1
> endfor

Nothing, it's the best option ;)  But the OP already used that.

--
Andy


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Link  
View profile  
 More options Nov 10, 7:39 am
From: Tom Link <micat...@gmail.com>
Date: Tue, 10 Nov 2009 04:39:29 -0800 (PST)
Local: Tues, Nov 10 2009 7:39 am
Subject: Re: from lines to dict keys
On 10 Nov., 13:30, Tom Link <micat...@gmail.com> wrote:

> > what's wrong with a dull looking for-loop:

> Okay, that was the original solution anyway. I'd say stick to it.

Sorry for the reply to self. If you really want to use map, you could
use:

exec 'let dict = {'. join(map(split(lines, "\n"), 'string(v:val) .":
1"'), ",") .'}'

You have to make sure the lines are unique.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Chantreux  
View profile  
 More options Nov 10, 11:06 am
From: Marc Chantreux <kha...@phear.org>
Date: Tue, 10 Nov 2009 17:06:14 +0100
Local: Tues, Nov 10 2009 11:06 am
Subject: Re: from lines to dict keys
hello all

many thanks for you tries and replies. all of them where very
instructive.

i'll stick on the for-loop solution as it seems that functionnal
solutions are harder to read/debug in viml.

regards
marc


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hari Krishna Dara  
View profile  
 More options Nov 11, 1:11 am
From: Hari Krishna Dara <hari....@gmail.com>
Date: Tue, 10 Nov 2009 22:11:46 -0800
Local: Wed, Nov 11 2009 1:11 am
Subject: Re: from lines to dict keys

On Tue, Nov 10, 2009 at 8:06 AM, Marc Chantreux <kha...@phear.org> wrote:

> hello all

> many thanks for you tries and replies. all of them where very
> instructive.

> i'll stick on the for-loop solution as it seems that functionnal
> solutions are harder to read/debug in viml.

Since you seem to want a one liner, here is one:

let t=eval('{'.join(map(split(system("echo foo; echo bar"), "\n" ),
'"''".v:val."'': 1"'), ',').'}')

--
Hari


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marc Chantreux  
View profile  
 More options Nov 11, 5:41 am
From: Marc Chantreux <kha...@phear.org>
Date: Wed, 11 Nov 2009 11:41:51 +0100
Local: Wed, Nov 11 2009 5:41 am
Subject: Re: from lines to dict keys
hello Hari

On Tue, Nov 10, 2009 at 10:11:46PM -0800, Hari Krishna Dara wrote:
> Since you seem to want a one liner, here is one:

> let t=eval('{'.join(map(split(system("echo foo; echo bar"), "\n" ),
> '"''".v:val."'': 1"'), ',').'}')

I want my code to be readable then short. it seems that viml isn't the
good langage for it. I stick on the for-loop :)

regards,
marc


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google