Wednesday, October 16, 2013

Re: Insert lines in vim based on parsing a comma-separated line of code

On 2013-10-16 17:44, DwigtArmyOfChampions wrote:
> Suppose I have the following Perl code:
>
> defined or $_ = "" for my ($alabama, $alaska, $arizona, $arkansas,
> $california, $colorado, $connecticut, $delaware, $florida,
> $georgia, $hawaii, $idaho, $illinois, $indiana, $iowa, $kansas,
> $kentucky, $louisiana, $maine, $maryland, $massachusetts,
> $michigan, $minnesota, $mississippi, $missouri, $montana,
> $nebraska, $nevada, $new_hampshire, $new_jersey, $new_mexico,
> $new_york, $north_carolina, $north_carolina, $north_dakota, $ohio,
> $oklahoma, $oregon, $pennsylvania, $rhode_island, $south_carolina,
> $south_dakota, $tennessee, $utah, $vermont, $virginia, $washington,
> $west_virginia, $wisconsin, $wyoming) = split(/\n/, $results);
>
>
> I want to insert the following lines into my code:
>
> $data{"alabama"} = $alabama;
> $data{"alaska"} = $alaska;
> $data{"arizona"} = $arizona;
>
> ...
>
> $data{"wisconsin"} = $wisconsin;
> $data{"wyoming"} = $wyoming;
>
> Is there a way in Vim to parse that line of for loop code to create
> all fifty of those lines?

(ignoring the wrapping introduced by my mailer)

I'd do a couple passes:

:t. " copy the one line to a new line
0df( " delete through the opening paren
f)D " delete after the closing paren
" at this point, we should have just the variables
:s/,\s*/\r/g " put each on its own line, remove commas
" highlight the resulting lines with "V"
:'<,'>s/\$\(.*\)/$data{"\1"} = &;
" transform the lines into your desired format


-tim



--
--
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.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: