Nth Field Extraction

I am hoping for some ideas from the wonderful collective on how to do something better than I am currently doing it. Here is the issue: I need a function (it will eventually replace the one in an package I have) that will be passed a delimiter string, the count, and the source string, and return the string in the count'th field.

I cannot use split() because the delimiter might be multiple characters, and it comes in via a variable. I currently use a while loop with index, building an array with the starting positions of each field, and then use substr to grab out what I am looking for. Here is an example:

my $major_div = '!!'; my $user_div = ';'; my $var_div = ','; my $full_string = 'abcd-efgh-ijkl-mnop;key1=data1,key2=data2;key1=data +3,key2=data4!!qwer-asdf-zxcv-tyui;key1=data3;key3=data6!!trew-hgfd-yt +re-bvcx;key1=data7,key2=data8;key1=data9,key2=data10!!erty-dfgh-cvbn- +hjkl;key2=data5;key3=data6'; my $major_field = &field_split($major_div, 3, $full_string); my $user_key = &field_split($user_div, 1, $major_field); my $user_vars1 = &field_split($user_div, 2, $major_field); my $user_vars2 = &field_split($user_div, 3, $major_field); my $key_data = &field_split($var_div, 1, $user_vars1);

The example is rather ludicrous, I admit, but it shows what we are doing. Sometimes what we are doing is iterating over the string (the major fields), and processing them all, one at a time.

I have something that works, but the code is a dozen years old, and I have wanted to update it, but just haven't done it -- if it ain't broke, don't fix it, right? But this new task will be using the code a lot, so I'm hoping one of you perl masters have already tuned a function that does this, and would be willing to share it.

Thanks muchly!

David Lee Crites
lee@critesclan.com

In reply to nth field extraction by lee_crites

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.