With just one problematic field that contains spaces you can still split the whole line on spaces without first modifying it by using the third argument to limit the number of fields. Work first from the left leaving the field with spaces along with the rest of the line in the "remainder" part of the string. Then, by using reverse, work in from the right, again with a limit, on the reversed "remainder" to get the rest of the fields; the field with the spaces is the last field and is not disturbed because of the limit to the split.

use strict; use warnings; use 5.010; my @dataLines = ( q{>cds:AEA30293 A/Netherlands/2223b/2009 2009/11/18 HA}, q{>cds:ADD23250 A/District of Columbia/INS17/2009 2009/10/26 HA}, q{>cds:ADX98640 A/San Diego/INS13/2009 2009/10/19 HA}, q{>cds:ADD97035 A/Wisconsin/629-D00036/2009 2009/09/15 HA}, ); say q{=} x 60; foreach my $dataLine ( @dataLines ) { say $dataLine; my @elems; ( $elems[ 0 ], my $remainder ) = split m{\s+}, $dataLine, 2; @elems[ 3, 2, 1 ] = map { scalar reverse } split m{\s+}, reverse( $remainder ), 3; say for @elems; say q{=} x 60; }

The output.

============================================================ >cds:AEA30293 A/Netherlands/2223b/2009 2009/11/18 HA >cds:AEA30293 A/Netherlands/2223b/2009 2009/11/18 HA ============================================================ >cds:ADD23250 A/District of Columbia/INS17/2009 2009/10/26 HA >cds:ADD23250 A/District of Columbia/INS17/2009 2009/10/26 HA ============================================================ >cds:ADX98640 A/San Diego/INS13/2009 2009/10/19 HA >cds:ADX98640 A/San Diego/INS13/2009 2009/10/19 HA ============================================================ >cds:ADD97035 A/Wisconsin/629-D00036/2009 2009/09/15 HA >cds:ADD97035 A/Wisconsin/629-D00036/2009 2009/09/15 HA ============================================================

I hope this is of interest.

Update: Modified code to change order of array slice and thereby eliminated the final reverse

Cheers,

JohnGG


In reply to Re: How to substitute something from only between two specified charecters by johngg
in thread How to substitute something from only between two specified charecters by ZWcarp

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.