in reply to nth field extraction

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.

Replies are listed 'Best First'.
Re^2: nth field extraction
by lee_crites (Scribe) on Jul 27, 2018 at 14:39 UTC

    Thanks for the (probably obvious) pointer into using a variable in the split command. If I was writing this today, I'd probably have checked to see if that was a possibility. I have a vague memory from back when I wrote the function (15+/- years ago), and couldn't get that construct working.

    I was just thinking about that, and remembered that the reason I wrote this was to deal with migrating data from a PICK system to a *nix system, back in the 90's -- so it is a tad older than 15 years... :O

    Thanks for the help!

    Lee Crites
    lee@critesclan.com
      Thanks for the (probably obvious) pointer into using a variable in the split command. If I was writing this today, I'd probably have checked to see if that was a possibility. I have a vague memory from back when I wrote the function (15+/- years ago), and couldn't get that construct working.

      You might have not recognized the significance of \Q in return (split /\Q$sep/, $source)[ $field-1 ]); The \Q says to ignore any characters in $sep that would otherwise mean something to the regex engine. I often use a \Q...\E pair for this just to highlight this situation. Anyway without \Q, if $sep contains something that matters to the regex engine, you will get confusing results.