in reply to Replace value in the text file

Thank you very much for your fast respond. I understand Marshall and Huck solution, but not sure tybalt89 solution. Tyball89, can you explain in detail what you did, please?

Replies are listed 'Best First'.
Re^2: Replace value in the text file
by afoken (Chancellor) on Jun 29, 2017 at 04:25 UTC
    I understand Marshall and Huck solution, but not sure tybalt89 solution.

    Don't worry, tybalt89 has the nasty habbit of posting completely uncommented, unexplained code that may or may not work; sometimes it works only accidentally.

    Your best bet for handling CSV data is to use Text::CSV_XS, its slower pure-perl implementation Text::CSV, or DBI in combination with DBD::CSV. The latter pair uses Text::CSV_XS behind the scenes to provide an SQL interface to the data in CSV files.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Text::CSV_XS, its slower pure-perl implementation Text::CSV

      From the doc of the latter:

      Text::CSV is a thin wrapper for Text::CSV_XS-compatible modules now. ... Text::CSV uses Text::CSV_XS by default, and when Text::CSV_XS is not available, falls back on Text::CSV_PP, which is bundled in the same distribution as this module.

      Personally, I usually recommend Text::CSV, although I guess I should start telling people to make sure Text::CSV_XS is installed as well :-)

        Text::CSV is a thin wrapper for Text::CSV_XS-compatible modules now.

        Of course, you are right. But one or one and a half decades ago, when I had to handle CSV files from Perl for the first time, Text::CSV was what now is Text::CSV_PP. That's written to my biological persistent storage, and it's damn hard to update. ;-) I know that, because it's not the first time that someone had to remind me.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        I love to use the one you recommend but I am just a beginner so not sure how to apply See if you can help me to edit my code below I like to change the value in column one to a fix value. Any number should reduced by 1 except "1" becomes "P". I am trying to learn how to use map or grep not creating the hash, that the guys showed me. Thanks Macy

        "Header1","Header2",Header3" "A123","1","valueB1" "B234","1","valueB2" "C345","2","valueB3" "D456","3","valueB4" "E567","4","valueB5" "F678","5","valueB6"

        and the output should be

        "Header1","Header2",Header3" "A123","P","valueB1" "B234","P","valueB2" "C345","1","valueB3" "D456","2","valueB4" "E567","3","valueB5" "F678","4","valueB6"

        here is my code

        open my $fh1,'<',"NRoomch.txt"; my @a = <$fh1>; my @arr; for $i (0..$#a){ my @b = split(/,/,$a[$i]); for $j (0..$#b) { $b[$j] =s~/"//g; if ($j==1 && my @arr = grep { $_ =~ /\"1"$/ } @b); @arr = map{$_ = "P"} @arr; else @arr = map {$b[$j] - 1} (1..$#b); print @arr; }; }; close($fh1);
Re^2: Replace value in the text file
by mhoang (Acolyte) on Jun 30, 2017 at 02:53 UTC

    I am just a beginner so please be patient with me,thanks. Macy