On 11/21/05, Danny Fang <[EMAIL PROTECTED]> wrote: > Hi, > I'm new to PERL and would like to seek help for the task mentioned below: > > I'm attempting to read the contents of a file containing rows with the > format shown below: > > version|exchange|area|date|time|callmod|callid|callno1|callno2|part2|start_date|start_time|spare|dur|flag_ini|indicator|length|ni|calling_nai|screening|address_i|num_plan_ind|publicservice_user|spare1|spare2|calling_no|line_no|spare3|spare4|called_nai|called_num_plan_ind|spare5|called_num|spare6|di_nai|di_num_plan_ind|spare7|dest_num|dest_num_type|spare8|doc|dccc_type|spare9|dt_update|spare10|subs_type|cug_code|spare11|teleservices|spare12|channel_isdn|calling_terminal|pulses|in_port|out_port|in_pop|out_pop|spare13|out_mod|spare14|link2bb_type|out_cic|in_cic|anomaly_code|anomaly_ind|spare15|spare16|location|cause_value|spare17|final_status|ingress_ip|egress_ip|inter_dur|bid_time|term_call_setup_delay|del_call_setup_delay|term_ccd|int_call_clear_delay|trans_delay|inter_jitter|send_packets|send_octets|rec_packets|rec_octets|lost_packets|lost_packets_out|packet_period|code_alg|spare20|silence_suppr|spare21|coi|spare22| > > E1440000100TT|006030766|0521|051018|191554|2|1529238851|63271|0|0|051018|184414|0|965|0|0|171|1|3|1|0|1|2|0|0|882030|255|0|0|3|1|0|052539028|0|3|1|0|052539028|1|0|1|0|0|0|0|SNO|0|0|0|0|0|000521882030|1|24086|27368|0|0|0|0|0|0|2|2|0|0|0|0|0|16|0|1|85.38.245.146|83.175.46.150|1|1129653851|142|0|0|0|17093|140|48227|1542980|47896|1532672|258|0||13|0|0|0||| > > I'm interested in modifying the values at the 3rd and 26th column of 1 > particular row in this file and duplicating that row values to populate it to > 4000 rows. There are 2123 rows in the this file currently. > > Below is the script which I've written in order to modify the values at the > column mentioned. > > However, I'm not sure how I could rewrite the newly modified column values > of that particular row back into the file - I want to use the particular row > which had its columns modified to be duplicated and appended to the end of > the current file for a specific number of time (adding more rows with the > duplicated rows). > > > version|exchange|area|date|time|callmod|callid|callno1|callno2|part2|start_date|start_time|spare|dur|flag_ini|indicator|length|ni|calling_nai|screening|address_i|num_plan_ind|publicservice_user|spare1|spare2|calling_no|line_no|spare3|spare4|called_nai|called_num_plan_ind|spare5|called_num|spare6|di_nai|di_num_plan_ind|spare7|dest_num|dest_num_type|spare8|doc|dccc_type|spare9|dt_update|spare10|subs_type|cug_code|spare11|teleservices|spare12|channel_isdn|calling_terminal|pulses|in_port|out_port|in_pop|out_pop|spare13|out_mod|spare14|link2bb_type|out_cic|in_cic|anomaly_code|anomaly_ind|spare15|spare16|location|cause_value|spare17|final_status|ingress_ip|egress_ip|inter_dur|bid_time|term_call_setup_delay|del_call_setup_delay|term_ccd|int_call_clear_delay|trans_delay|inter_jitter|send_packets|send_octets|rec_packets|rec_octets|lost_packets|lost_packets_out|packet_period|code_alg|spare20|silence_suppr|spare21|coi|spare22| > > E1440000100TT|006030766|AAA|BBBB|191554|2|1529238851|63271|0|0|051018|184414|0|965|0|0|171|1|3|1|0|1|2|0|0|882030|255|0|0|3|1|0|052539028|0|3|1|0|052539028|1|0|1|0|0|0|0|SNO|0|0|0|0|0|000521882030|1|24086|27368|0|0|0|0|0|0|2|2|0|0|0|0|0|16|0|1|85.38.245.146|83.175.46.150|1|1129653851|142|0|0|0|17093|140|48227|1542980|47896|1532672|258|0||13|0|0|0||| > > E1440000100TT|006030766|AAA|BBBB|191554|2|1529238851|63271|0|0|051018|184414|0|965|0|0|171|1|3|1|0|1|2|0|0|882030|255|0|0|3|1|0|052539028|0|3|1|0|052539028|1|0|1|0|0|0|0|SNO|0|0|0|0|0|000521882030|1|24086|27368|0|0|0|0|0|0|2|2|0|0|0|0|0|16|0|1|85.38.245.146|83.175.46.150|1|1129653851|142|0|0|0|17093|140|48227|1542980|47896|1532672|258|0||13|0|0|0||| > > Could anyone help me out? > > Thanks > Danny > > > ##open file for reading > open(INPUTFILE, $inputFile) || die "Cannot open $inputFile \n"; > @fileRecs = <INPUTFILE>; > $totalRecs = scalar(@fileRecs)-1; > print "Total records in $inputFile is $totalRecs \n"; > ##open file for writting output >
I'm not sure where you're headed here: the number of lines in the file is @fileRecs, not @fileRecs-1. > open(OUTFILE, ">$inputFile.tmp") || die "Cannot open $inputFile.tmp \n"; > $secondRow = $fileRecs[2]; > print "BEGINNING -- secondRow = $secondRow \n"; > This is actually the thrid row. Perl indexes arrays beginning with zero (although it keep couunt of the number of elements counting from 1). > @secondRowRec = split("|", $secondRow); > > $secondRowRec[2]="AAAA"; > $secondRowRec[3]="BBBBB"; > > print "\$secondRowRec[2] = $secondRowRec[2] and > \$secondRowRec[3]=$secondRowRec[3] \n"; > > print "\$secondRow is now $secondRow \n"; > > $diffOfNewRec = 4000 - $totalRecs; > > print "Need to produce additional $diffOfNewRec \n"; > > #making a backup copy > `cp $inputFile $inputFile.tmp`; 1) Dont' use backticks unless you're capturing output; use system(). 2) You already created $inputFile.tmp, now you're clobbering it, why? 3) if all you want to do is copy a text file, either read and print the lines yourself, or use File::Copy, etc.. Don't waste resources on an external system call when Perl offers about 300 native options. 4) always check the return value of system calls to mke surethey succeed. > ## I'm need help here !! Not sure how I could re-join the elements modified > back into the array containing that particular row and append it to the end > of the file > ## here's one way: #!/usr/bin/perl use strict; use warnings; my $inputFile = "myfile"; open(INPUTFILE, $inputFile) || die "Cannot open $inputFile \n"; open(OUTFILE, ">$inputFile.tmp") || die "Cannot open $inputFile.tmp \n"; my $secondRow; while (<INPUTFILE>) { if ( $. == 2 ) { print "BEGINNING -- secondRow = $_ \n"; @secondRowRec = split /|/; $secondRowRec[2]="AAAA"; $secondRowRec[3]="BBBBB"; print "\$secondRowRec[2] = $secondRowRec[2] and \$secondRowRec[3]=$secondRowRec[3] \n"; $secondRow = join("|", @secondRowRec); print "Second Row is now $secondRow \n"; print OUTFILE join("|", $secondRow); } else { print OUTFILE; } } for (1..4000-$.) { print OUTFILE $secondRow; } close OUTFILE; close INPUTFILE; rename("$inputFile.tmp", $inputFile) or die "couldn't rename tmp file"; __END__ (this code is intented as an example; don't go out and clobber your data with that rename() at the end without testing it for your environment and data) HTH, --jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!