The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Concate string and operator from file data
Discuss Concate string and operator from file data in the Perl Programming forum on Dev Shed. Concate string and operator from file data Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 28th, 2012, 10:59 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 1
Time spent in forums: 46 m 25 sec
Reputation 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>
|

September 29th, 2012, 05:03 AM
|
|
|
There are many ways to do this. One of the simplest is to split you input with the commas.
Something like this:
Perl Code:
Original
- 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"; }
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|