Hii everybody!! I am a new user of Perl. I have a small problem here:
I have got a tab delimited text file containing a list of chemicals and their properties. Now I would like to create a new column where the program should first scan the whole file and then it will write new properties in a new column.
For example:
File name: chem.txt

Chem name react
Abc 66
cbd 65
gbk 47

and so on..
Now I would like the script to read the whole file and if it finds 'gbk' in file, then it creates a new column here and writes 'red'.

Output:

Chem name react Property
Abc 66
cbd 65
gbk 47 red

Much appreciated if you could help me..(I have attached the example text file also)
Thanks a lot..
Pratul

Attachments
Chem name             react 
Abc                     66
cbd                     65
gbk                     47  


And the output needed:

Chem name      react             Property
Abc              66
cbd              65
gbk              47                red

This works but it doesn't pass "warnings" because of the split. I still have to figure that out, it's strange. But it does what you requested. I had to modify your chem.txt to actually be tab delimited BTW.

my %property;
$property{gbk}="red";
open(IN,"<chem.txt");
open(OUT,">newchem.txt");
my $x=0;
my $chem="";
my $react="";
while(<IN>){
	chomp;
	$x++;
	if($x==1){
		print OUT "Chem name\treact\tProperty\n";
		next;
	}
($chem,$react)=split(/\t/);
print OUT "$chem\t$react\t$property{$chem}\n";
}
close OUT;
close IN;

This works but it doesn't pass "warnings" because of the split. I still have to figure that out, it's strange. But it does what you requested. I had to modify your chem.txt to actually be tab delimited BTW.

my %property;
$property{gbk}="red";
open(IN,"<chem.txt");
open(OUT,">newchem.txt");
my $x=0;
my $chem="";
my $react="";
while(<IN>){
	chomp;
	$x++;
	if($x==1){
		print OUT "Chem name\treact\tProperty\n";
		next;
	}
($chem,$react)=split(/\t/);
print OUT "$chem\t$react\t$property{$chem}\n";
}
close OUT;
close IN;

thnaks a lot..i'll try this but would you please put some comments for me bcoz i am a learner and it would be easier for me to understand..

Sure:

my %property; #this is a keyed hash 
$property{gbk}="red"; #this is a value keyed by the chemical name
#if you want others just put in $property{cbd}="green" and the program will work still
open(IN,"<chem.txt"); #opening your file
open(OUT,">newchem.txt"); #writing to a new file
my $x=0;
my $chem="";
my $react="";
while(<IN>){ #loop through the entire file
	chomp;
	$x++; 
	if($x==1){ #checks to see if we are on the first line
		print OUT "Chem name\treact\tProperty\n"; #if so, write out the header 
		next; #go to next record (the data)
	}
#since we passed the "next" above because $x>1 the other lines are processed with 
#the code below
($chem,$react)=split(/\t/); #pull apart the tab delimited record
#the next line does the heavy lifting. It prints out the original data
#plus the value from the keyed hash. Since I only set one key "gbk" and 
#associated it with one value "red" when the other keys are there
#it prints out nothing "". If you set the other keys above, it will
#print out those
print OUT "$chem\t$react\t$property{$chem}\n"; 
}
close OUT;
close IN;

ghosh22: Make sure your input file is really tab-delimited (see attached file) rather than space-delimited. mitchems already pointed this out, but some text editors make it hard to see the difference so you may not have understood what he meant.

mitchems: I added a line to your script to satisfy the warnings pragma.

...snip...
    ($chem,$react)=split(/\t/);
    #Add the following line to make the warnings pragma happy
    $property{$chem} = '' if not exists $property{$chem};
    print OUT "$chem\t$react\t$property{$chem}\n";
.../snip...
Attachments
Chem name	react
Abc	66
cbd	65
gbk	47

Thanks David. THAT was what it was carping about. It reported the $react variable and I was completely confused. Of course! Because the keys are undefined - which was a feature of the script, but understandably caused warnings to complain.

Thanks a lot guys!!! Got one small question..really got confused with perl conditionals and loop. Would you please elaborate where to use:
if..elsif
while
for
foreach
unless
do
in a program? Which one is best for which purpose?
Many thanks in advance..

This article has been dead for over six months. Start a new discussion instead.