I'm not sure why you can't use plain split for that?

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'; sub field_split { my ($sep, $field, $source) = @_; return (split /\Q$sep/, $source)[ $field-1 ] }; 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);

But really, I would look at using Text::CSV_XS to read in the incoming (major) data and split it up into an array, and then split up the minor fields from that.


In reply to Re: nth field extraction by Corion
in thread 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.