Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 28th, 2012, 10:59 PM
fahrial fahrial is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 1 fahrial User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>

Reply With Quote
  #2  
Old September 29th, 2012, 05:03 AM
Laurent_R Laurent_R is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 506 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 5 m 2 sec
Reputation Power: 385
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
  1.  
  2. use strict;
  3. use warnings;
  4.  
  5. open my $INPUT, "<", "filename.txt" or die "could not open filename.txt $! \n";
  6. while (my $line = <$INPUT>) {
  7.      next if $line =~ /<name>/; # remove the header
  8.      my ($name, $class, $score1, $score2, $total) = split /,/, $line;
  9.      $class .= ".SD";
  10.      $total /= 2 ;
  11.      print "$name, $class, $score1, $score2, $total \n";
  12. }

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Concate string and operator from file data

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap