Perl Substitution

K

kris

Hi,
Iam a research scientist trainee working on reaction kinectics, I just
started to learn perl, I had asked a few doubts previously and got them
cleared , thanks to all the people who helped, i require your help
again.
I have an input file which has the following form :
....
....
Species1.Concentration moles;
Species2.Concentration moles;
.....
Species3.variable;
Species2.Concentration=Species1.Concentration^Species3.varaible;
....
Species3.variable=4;

(^- raised to the power of)
I need to substitute the variable value in place of the variable and
change the units of Species2.Concentration to moles^4.
I do not know the name of the Species so i don't know how to use
regular expression and also the variable might be more than 1.
Please help me out. Thanks in advance.
 
K

kris

hi,
thank you for your help, i read up regular expressions and have written
some code, but it substitutes the value at all places, i want it to
substitute only if it is raised to the power or if it occurs after '^'
..
Code :
#!usr/bin/perl
# testing for ^ substitution.

use strict;
use warnings;

my $iname = "/home/user/test1.txt";
my $oname = "/home/user/out.txt";
my (@temp,@newline,@newtmp);
open(INF,'<',$iname) or die ("Cannot open input file \n");
@newline=<INF>;
close (INF);
open(OUTF,'>',$oname) or die ("Cannot open output file\n");
my ($newlines,$i);
my @tmp;
foreach $newlines(@newline)
{
if ($newlines =~ /(.*)(\.)(.*)( = )(.*)(;)/) // if it matches
Species4.variable
{
@temp = split (/=/,$newlines); // split at '=' & store
values in
@temp
}
if ($newlines =~ /(\^)/) // search for ^
{
@tmp = split(/ /,$newlines); // if
found split at '^'
@newtmp = split (/;/,$tmp[1]); //
store Species4.variable after
split

}
}

if ($temp[0] eq $newlines[1])
{
foreach $newlines(@newline)
{
$newlines =~ s/$tmp[1]/$temp[1]/;
print $newlines;
}
}

close (OUTF);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top