Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi hyans.milis,

When parsing a CSV file use tested modules like Text::CSV_XS or Text::CSV, it's a lot better than re-inventing the wheel.
However, using your method, you can achieve your aim by using non-greedy regex in your if/elsif statement blocks and putting your variables $sum2 and $sum3, in the proper place like so:

... if ( $sum > 310 ) { my $sum2 = "volemd"; #chomp($sum2); not needed $line =~ s/(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)/$1\,$2\,$3 +\,$sum2,$5,$6/g; } elsif ( $sum == 70 ) { my $sum3 = "volemd1"; #chomp($sum3); not needed $line =~ s/(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)\,(.*?)/$1\,$2\,$3 +\,$sum3,$5,$6/g; } ...
That should give you your expected result:
623192729079,510993192729079,19,volemd,0,133,282051608, 623192728769,510993192728769,19,310,0,118,84950715, 623192729901,510993192729901,19,volemd1,0, 623192609007,510993192609007,19,22,0, 623416771429,510993416771429,19,volemd1,0, 622319309157,510992319309157,19,22,0, 623192724581,510993192724581,19,volemd1,0, 622319381619,510992319381619,19,volemd1,0, 622198575655,510992198575655,19,1,0, 623192724589,510993192724589,19,volemd1,0, 622743581281,510992743581281,19,71,0,
Update:
Always close your open filehandles like so: close $fh or die "can't close file: $!";

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

In reply to Re: replace/substituion 4th field by 2teez
in thread replace/substituion 4th field by hyans.milis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 11:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found