What do you mean exactly by "arbitrary"? If it means a constant, known at run-time, number of spaces, then it's easy, just do a split on that number of spaces. If you mean random, unknown and different from line to line, then the answer is easy: with the information you give, you can't. In your example, there is no way to know where to split Genesis Chamber Mark Tedin in 2 strings.

Now all is not lost, you can try various heuristics to figure out what to do, but remember # 11953 Of course, this is a heuristic, which is a fancy way of saying that it doesn't work (from MJD):

#!/usr/bin/perl -w use strict; while( <DATA>) { if( m{^\d+ # initial digits (1rst field) \s+ # separating space(s) (.*?) # the 2cd and 3rd field \s+ # separating space(s) \S # 1 (non-space) character (4th field) \s+ # separating space(s) \S # 1 (non-space) character (5th field) \s* # you might or might not want to allow extra spaces + a the end of the line $}x ) { my $fields= $1; my @fields; # @fields= heuristic1( $fields) || heuristic2( $fields); # does not work as the || seems to put the first function call + in scalar mode, weird @fields= heuristic1( $fields); unless( @fields) { @fields= heuristic2( $fields); } if( @fields) { print "field 2: '$fields[0]' - field 3: '$fields[1]'\n"; } else { warn "cannot extract field 2/3 of line $. Fields are: '$fi +elds'\n"; } } else { warn "can't parse line $."; } } # only 2 words, that's easy sub heuristic1 { my $fields= shift; my @fields= split /\s+/, $fields; if( @fields == 2) { return @fields } else { return; } } # more than one space separates the 2 fields sub heuristic2 { my $fields= shift; my @fields= split /\s\s+/, $fields; if( @fields == 2) { return @fields } else { return; } } __DATA__ 122 Genesis Chamber Mark Tedin A U 123 f2w f3w 4 5 123 f2w1 f2w2 f3w 4 5 123 f2w1 f2w2 f3w1 f3w2 4 5 123 f2w f3w 4 99 123 f2w1 f2w2 f3w1 f3w2 4 5

In reply to Re: Abritrary multiple spaces as delimiter by mirod
in thread Abritrary multiple spaces as delimiter by Weisshaupt

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.