Perl Substitution

Discussion in 'Perl Misc' started by kris, Apr 28, 2006.

  1. kris

    kris Guest

    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.
    kris, Apr 28, 2006
    #1
    1. Advertising

  2. kris

    kris Guest

    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);
    kris, May 2, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tyson Marchuk
    Replies:
    7
    Views:
    122
    Brian McCauley
    Feb 24, 2004
  2. ari
    Replies:
    3
    Views:
    80
  3. kp

    Command substitution in perl

    kp, Oct 7, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    787
  4. Replies:
    3
    Views:
    113
    Dr.Ruud
    Nov 22, 2006
  5. substitution in perl

    , Feb 1, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    92
    Mirco Wahab
    Feb 1, 2007
Loading...

Share This Page