#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Concate string and operator from file data


    need help please

    i have data.txt like this

    <name>,<class>,<score1>,<score2>,<total>
    fahrial,1A,9,8,17
    novi,1B,9,7,16
    arka,1C,9,10,19
    <name>,<class>,<score1>,<score2>,<total>
    arfa,2A,8,8,16
    ari,2B,8,7,15
    dira,2C,7,10,17


    I want to add a string ".SD" in the second column (<class>) and value in the last column divided into 2, so the result will be like this

    <name>,<class>,<score1>,<score2>,<total>
    fahrial,1A.SD,9,8,8.5
    novi,1B.SD,9,7,8
    arka,1C.SD,9,10,9.5
    <name>,<class>,<score1>,<score2>,<total>
    arfa,2A.SD,8,8,8
    ari,2B.SD,8,7,7.5
    dira,2C.SD,7,10,8.5

    and also how to if I want to delete the header column
    <name>,<class>,<score1>,<score2>,<total>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    There are many ways to do this. One of the simplest is to split you input with the commas.

    Something like this:

    Perl Code:
     
    use strict;
    use warnings;
     
    open my $INPUT, "<", "filename.txt" or die "could not open filename.txt $! \n";
    while (my $line = <$INPUT>) {
         next if $line =~ /<name>/; # remove the header
         my ($name, $class, $score1, $score2, $total) = split /,/, $line;
         $class .= ".SD";
         $total /= 2 ;
         print "$name, $class, $score1, $score2, $total \n";
    }

IMN logo majestic logo threadwatch logo seochat tools logo