You can take one of two approaches:

1/ edit the string, probably using a regex

2/ split the string into an array and edit the elements

We can't tell you what will work best because you don't tell us enough about the overall process. You asked in essence "how do I edit elements of an array" and got good answers. Now you show us a process involving reading a line, spliting it into an array, editing the array, ... . Then you tell us you don't want to do that but instead want to do something else! What do you want to do?

I suggest you try several approaches on a small subset of your data and benchmark them to see what works best. The following may be a good starting point:

use strict; use warnings; use Benchmark qw(cmpthese); my $line = join (',', 1 .. 1000) . "\n"; my $data = $line x 100; cmpthese ( -1, { array => \&asArray, str => \&asStr, } ); sub asArray { my $outStr; open my $in, '<', \$data; open my $out, '>', \$outStr; print $out join ',', map {s/$/a/; $_} split ',' while <$in>; return $outStr; } sub asStr { my $outStr; open my $in, '<', \$data; open my $out, '>', \$outStr; while (<$in>) { s/(?=,)|\n/a/g if /\S/; print $out $_; } return $outStr; }

Prints:

Rate array str array 6.23/s -- -28% str 8.61/s 38% --

which indicates that the differences between the two techniques are small enough that they would probably be completely lost in the I/O time.

True laziness is hard work

In reply to Re^3: Concatenation to elements in an array ! by GrandFather
in thread Concatenation to elements in an array ! by pspillai

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.