Loading ...
Sorry, an error occurred while loading the content.
 

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

Expand Messages
  • DwigtArmyOfChampions
    Suppose I have the following Perl code: defined or $_ = for my ($alabama, $alaska, $arizona, $arkansas, $california, $colorado, $connecticut, $delaware,
    Message 1 of 3 , Oct 16, 2013
      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?

      --
      --
      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@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Tim Chase
      ... (ignoring the wrapping introduced by my mailer) ... 0df( delete through the opening paren f)D delete after the closing paren
      Message 2 of 3 , Oct 16, 2013
        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@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • DwigtArmyOfChampions
        That worked perfectly, you re a genius, thank you very much. -- -- You received this message from the vim_use maillist. Do not top-post! Type your reply
        Message 3 of 3 , Oct 17, 2013
          That worked perfectly, you're a genius, thank you very much.

          --
          --
          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@....
          For more options, visit https://groups.google.com/groups/opt_out.
        Your message has been successfully submitted and would be delivered to recipients shortly.