I have another tack on this problem, it might not be better though. If the number of fields is consistent and the only field that contains commas is the company name you could rebuild the company name after splitting. The following seems to work.

use strict; use warnings; my $max = 4; #Largest expected index. while (<DATA>) { s/#.*$//; #Clear out the comments chomp; my @foo = split /,/; my $company = $foo[1]; #prime a variable with a company name if ($#foo > $max) { #If there are more elements than the ma +x... my $range = (scalar @foo) - $max; #Get the top of the range $company = join '+', @foo[1..$range]; #The company name is in +a slice } print "$company\n"; #do whatever with the data. } __END__ client,ABC Company, jwilkens,jdoe,mdeart #this one is fine client,Cornell,Thayil,Cameron & Sheppard, LLC, klivingston,amarison,ps +erton #thi s one is not client,Doe,Smith & Randall, Inc, jwaters,pfloyd,jjoplin #this one is n +ot
I didn't bench this against substr and index, that might be more efficient, and I now this code could be compressed a bit but I thought this was a fairly clear way to write a sample.

Ira,

"So... What do all these little arrows mean?"
~unknown


In reply to Re: Help with substitution - - 15 Characters from the left by IraTarball
in thread Help with substitution - - 15 Characters from the left by La12

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.