Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Help with substitution - - 15 Characters from the left

by IraTarball (Monk)
on Aug 13, 2001 at 22:51 UTC ( #104534=note: print w/replies, xml ) Need Help??


in reply to Help with substitution - - 15 Characters from the left

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://104534]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2019-05-26 01:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (152 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!