i have 10 data notepad files (with column x= 1 to 5 and column y = some random number)
x y
1 12
2 13
3 8
4 6
5 2
i want to multiply the 1st file's second column with 3, and save it as new data file
Increase the data file no. and muliply the data file no 2nd to 7th with 8 and keep 8th to 10th file same. I want to save these data files differently as new notepad files and i require a perl program for it. Can someone help?

Recommended Answers

All 9 Replies

What are the names of your input files? Before you run your program you have 10 files. After you run it you will have 20 (10 input plus the 10 new output files). If you run your program more than once, the program will need to specify the names of the files it needs to read, since you won't want to read the output files as well.

I recommend you name your input files according to an easily recognisable pattern, such as, "in_001.txt", "in_002.txt" etc. Then your program can make an array of file names to read, and as it opens each file it can open a new output file named something like "out_001.txt", "out_002.txt" etc.

i am new to perl and i do not know to write a program. My files are ls_0001_1.dat to ls_0010_10.dat

Try this:

use strict;
use warnings;

my $x=0;
for ($x=1;$x<11;$x++){
	
	my $pad="000";
	$pad="00" if($x==10);
	open (INF,"<","./ls_$pad$x\_$x\.dat");
	open (OUT, ">", "./New_ls_$pad$x\_$x\.dat");
	my $y=0;
	while(<INF>){
		chomp;
		$y++;
		if($y==1){
			print OUT "X Y\n";
			next;
		}
		my ($line,$value)=split(/ /);
		my $mult=1;
		if($x==1){
			$mult=3;
		}elsif($x<8){
			$mult=8;	
		}
		my $newval=$value*$mult;
		print OUT "$line $newval\n";
	}
}

Thankyou very much for your kind reply, it is the same what i require.
Actually, i made a mistake in mentoning my input file name, it is ls_00045_mythen_1.dat to ls_00045_mythen_10.dat, and it is present in a folder in drive C: Doc and sett\mona\Desktop\etc.

Additionally what should i add to make changes in the above program. I would be happy if you indicate to me what does each line do, i am new to perl.

Thanks again

The changes are marked below:

use strict;
use warnings;
my $x=0;
for ($x=1;$x<11;$x++){
 	#my $pad="000"; #remove not necessary any more
	# $pad="00" if($x==10); #remove
	open (INF,"<","./ ls_00045_mythen_\$x.dat"); #change file spec
	open (OUT, ">", "./new__00045_mythen_\$x.dat"); #change file spec
	my $y=0;
	while(<INF>){
		chomp;
		$y++;
		if($y==1){
			print OUT "X Y\n";
			next;
		}
		my ($line,$value)=split(/ /);
		my $mult=1;
		if($x==1){
			$mult=3;
		}elsif($x<8){
			$mult=8;	
		}
		my $newval=$value*$mult;
		print OUT "$line $newval\n";
	}
}

"readline() on filehandle INF at trial.pl line 10"
I am gettig this error, what is its meaning? Am i doing some mistake?

That means that the filespec is wrong - perhaps a typo in the open statement. Try adding:

print "./ ls_00045_mythen_\$x.dat";

after line #7 and see what comes out. If the filename is not correct you will have to fix it.

Whoops, looks like I put in an extra space before the filename.

"./ ls_00045_mythen_\$x.dat";

should be

"./ls_00045_mythen_\$x.dat";

I did that because I cut and pasted your filename from your message. Take out the space and it should work fine.

thankyou very much for your reply, my problem is solved, thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.