Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl Substitution

Reply
Thread Tools

Perl Substitution

 
 
kris
Guest
Posts: n/a
 
      04-28-2006
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^Spec ies3.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.

 
Reply With Quote
 
 
 
 
kris
Guest
Posts: n/a
 
      05-02-2006
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);

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
substitution in perl raj.somla@gmail.com Perl Misc 1 02-01-2007 08:28 AM
Perl substitution working on some machines but not others? therocket79@yahoo.co.uk Perl Misc 3 11-22-2006 10:54 AM
Command substitution in perl kp Perl Misc 5 10-09-2006 06:42 AM
parametrical substitution in perl ari Perl Misc 3 11-01-2005 09:59 AM
[NEWBIE] Expanding a perl variable twice inside a substitution Tyson Marchuk Perl Misc 7 02-24-2004 05:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57