Adding information to c file using perl

Hi,
I have c file which contains more number of tests. two or more tests in one c file. I have the XMl data regarding tests. i need to add this xml data to c file at before the test. I know which file having which test and i created hash table for that. so the problem is i have to add information to the c file at rhight place before the test. my c file look like this

/*!
*****************************************************************************
* * $b Description: Pressure Valve high
****************************************************************************/
#include "ccode.h"
/****************************************************************************/
/* Local function prototypes (static): */
void main_10ms(void)
{
test1 ( i need to add infomation before every test)
if {.....
......
if{........
.....
}else
{......
}
}
test2 
if {.....
......
if{........
.....
}else
{......
}
}

like this . i need to add information to c file using perl script. suggest me how to do this task. thanks in advance.

I assume that the testnames are the keys of your hash and that you need to add the value?
So read the c file and if you match any of the test names

#hint
$test_expression="(" . join("|", keys %tests).")" ;

Then print the description after printing the line
Otherwise just print the line.
Now copy the output back over the original.

1 Like

Thanks for your reply, i will try like this and i will ask if any errors is occured.

regards,
veerubiji

---------- Post updated at 02:15 PM ---------- Previous update was at 08:43 AM ----------

Hi, i am trying like this but i am not getting result can explian little bit more. i have string names in my file like "temperature value test" i need to add information after this sting. so i read the file and i matched this string using regex but i am confusing how to add information after this.

#!/usr/bin/perl
use warnings;
use strict;
open(File, "code.c") or die "unable to open file: $!\n";
while(<File>){
if (my $string =~ /\/\temperature value test\\//)
push (@string,"information");
print "$_";

but i didnt get result may be there is a wrong in my code can help me how should i add information.
thanks in advance.

Seriously, try using code tags when you post code, it makes everyone's life easier.

Now to address your question...

if (my $string =~ /\/\*temperature value test\*\//)

Examine that line for a moment... what does the =~ operator do? and how could $string match anything at that point?

Take a look at the following

~/tmp$ cat code.c 
#include code.h

//first test
if (test_passes()){
...
//second test
if (this_test_passes()){
..
//third test
if (this_test_passes()){

~/tmp$ cat test.pl
#!/usr/bin/perl

use strict;

my %test_description = ('first test'=>": This is the first test we undertake\n",
                        'second test'=>": This is the second test we undertake\n",
                        'third test'=>": This is the third test we undertake\n",
                       );
open (my $code , "<", 'code.c');
my $test_finder="(" . join("|", keys %test_description).")" ;
while(<$code>){
    if (/$test_finder/){
        chomp;
        $_.=$test_description{$1};
    }
    print $_
}
    
~/tmp$ ./test.pl
#include code.h

//first test: This is the first test we undertake
if (test_passes()){
...
//second test: This is the second test we undertake
if (this_test_passes()){
..
//third test: This is the third test we undertake
if (this_test_passes()){

Is that close to what you wish to do?

Hi,
thanks for reply, next time i cant repeat the mistake sorry for that.
your code is very much useful for me because my output also look like but i have hash table that contains testnumber and information to add regarding testnumber. i am trying to use that hash table.
thanks in advance.

regards,
veerubiji

Well, what's in this hash table, and what output would you like it to produce?

Hi,
Actuvally i am trying like this, I have two hash tables.

Hash table1 contains strings and their numbers like as shown below
(value) (key)
pressure 2034
temperature 2035
humidity 2036 like that i have 100 stings and and numbers.

second hash table contains numbers and information like as shown below
(key) (value)
2034 information
2035 information
2036 information
.......................... like this, i have 100 strings and 100 unique numbers to the strings.

In my c file i have the strings names what i have in hash table1 like pressure, temperature, humidity ........

now i have to test if string matches in the c file then add according number and information before the string. this is my hole problem, i have to implement perl script for this task.

I am very beginner to perl, i am trying like this.
thanks in advance.

regards,
veerubiji.

If the values in the first table are unique you can simply reverse the hash and if they are not you cannot look up the second table on the basis of the first as you won't have a unique key to use.
so the look up above becomes

%hsah_1 = reverse %hash_1;
$_.=$hash_2{$hsah_1{1}};

Hi,
you mean in the above two line code you are combining two hash tables into one. I think its ok, because i have 100 strings each string contains different numbers but numbers are unique, not repeated even one. for examples 1 to 100 and i have information for each number. numbers in both hash tables are same.
thanks for your suggestion.
best regards,
veerubiji

---------- Post updated 10-07-11 at 09:19 AM ---------- Previous update was 10-06-11 at 07:50 PM ----------

Hi, If in my c file sometimes i have same string two or three times then its adding two or three times the same information into c file. what should i do if i need to add frist appearence of the string.

regards,
veerubiji.

---------- Post updated at 04:30 PM ---------- Previous update was at 09:19 AM ----------

Hi,
i tried like this, there is no errors i am not getting output, i am getting original c file there is no added information.

#!/usr/bin/perl
use warnings;
use strict;
 
open (my $code , "<", 'ccode.c');
 
my %data = ('24'=>":&temperature \n",
'25'=>": &pressure \n",
'26'=>": &humidity\n",
);
 
my %nums = ( '24'=>": information1\n",
'25'=>": information2 \n", 
'26'=>": information3 \n",
); 
 
 
my $test_string="(" .join("|", keys %data).")" ;
 
while(<$code>){
 
if (/$test_string/){
 
chomp;
 
%data= reverse %data;
 
$_.=$nums{$data{1}}; 
}
print $_
}

it excuted without errors but not giving resultant output.

Hi skrynesaver,
please can you reply me . what mistakes i did in the above code.

I left out the dollar sign in the look up in the line I posted and also didn't explain the use of the reverse hash, you're changing it on each iteration (here's the code as also posted on Perl Monks this morning :wink: )

#!/usr/bin/perl
use warnings;
use strict;

open (my $code , "<", 'ccode.c')||die "Could not open ccode.c\n\t$!";

my %data = ('24'=>"temperature",
            '25'=>"pressure",
            '26'=>"humidity",
);

my %nums = ('24'=>"information1: ",
            '25'=>"information2: ",
            '26'=>"information3: ",
);

%data = reverse %data;

my $test_string="(" .join("|", keys %data).")" ;

while(<$code>){
    if (/$test_string/){
        $_=$nums{$data{$1}}.$_;
    }
    print $_
}

Hi,
Thanks for your reply, its working.
If i have the same string two or three times in a c file, this script adding two or three times the same information. I need to add information on frist time string found, no need to add two or three times the same information. like if temperature string found three times i need to add on frist found no need on second and third. can you help how can i modify the script.

thanks in advance.

Regards,
veerubiji.

then change the while block by adding a line to remove the additional text, thus next time we modify the string by prepending an empty string.

while(<$code>){
     if (/$test_string/o){
         $_=$nums{$data{$1}}.$_;
         $nums($data{$1}=""; 
 }

Hi,
thank you very much.

regards,
veerubiji

---------- Post updated at 12:18 PM ---------- Previous update was at 09:38 AM ----------

Hi,
If i am adding information before the string that i found in c file, in that case i found string some where in the middle of c code it look likes badly for a programmer. so tried like this if string found any where in the c file add information before c file starts, its ok it working. is there any way to add information at start of the test if i found string any where of the test.

for example

#include<stdio.h>
void main()
{
 /*test name*/  ( add information here)
if {
    ........
    .........
     (string found here)
.........
}...............

sorry for asking question once agin .
regards,
veerubiji