Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

perl script for adding information to c file.

by veerubiji (Sexton)
on Oct 10, 2011 at 08:40 UTC ( #930555=perlquestion: print w/replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

I want add some information to c file. I have two hash tables one hash table contains information about some strings and numbers to stings like as shown below.

hash table1 (keys)(string nubers) (values)string names 24 temperature 25 pressure 26 humidity

like this i have more strings and regarding numbers. in this hash table keys are numbers and values are string names. i have another hash table which contains string numbers and information.

hash table 2 (keys)(string nubers) (values)string names 24 information 25 information 26 information.

like that i have second hash table. numbers in the both hash tables are same and each string having unique number.there is no repetion in numbers like 1 to 100 numbers. now in my c file i have the sting names like temperature and humidity and pressure..... i have to search that string using these hash tables and i have to add information after the string if i found any string in the c file. i have written code like this but its notworking.

#!/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 $_ }

if i excute this script it doesnt give any errors and no added information in the c file. it giving original c file as it is.what should i have to change in this code. my c file is lokking like as below.

Replies are listed 'Best First'.
Re: perl script for adding information to c file.
by choroba (Bishop) on Oct 10, 2011 at 08:55 UTC
    1. You are searching for strings in this (or similar) form:
      ":&temperature \n"
      Such a string in not present in your data.
    2. Also, you are reverting %data back and forth. That's probably not what you need.
    3. I tried to clear your code and make it run:
      #!/usr/bin/perl use warnings; use strict; my $code = *DATA; my %data = (24 => 'temperature', 25 => 'pressure', 26 => 'humidity', ); my %nums = ( 24 => ": information1\n", 25 => ": information2 \n", 26 => ": information3 \n", ); %data = reverse %data; my $test_string = '(' . join("|", keys %data) . ')' ; while (<$code>){ if (/$test_string/) { chomp; $_ .= $nums{$data{$1}}; } print; } __DATA__ /*! ********************************************************************** +******* * * $b Description: Pressure Valve high ********************************************************************** +******/ #include "ccode.h" /********************************************************************* +*******/ /* Local function prototypes (static): */ void main_10ms(void) { temperature ( i need to add infomation before string that found) if {..... ...... if{........ ..... }else {...... } } humidity( i need to add infomation before string that found) if {..... ...... if{........ ..... }else {...... } }
    4. Do you really need two hashes? Having just one with the information like temperature => 'information1' would be much easier.
    Update: Restructured.

      Hi choroba, Its working but i have one problem with this script, it adding information how many times sting found in c file. But i need to add only once.for example if temperature string found three times in c file, so it adding same information three times. if string found add information at once is enoghf. no need to add same information so many times(how many times string presents).because in my file i have repeated strings so its aadding same information several times. how can i rectify this.

        how can i rectify this.

        Modify program to use hash "%seen" to keep track of substitutions already made

        See perlfaq6

Re: perl script for adding information to c file.
by Utilitarian (Vicar) on Oct 10, 2011 at 09:06 UTC
    #!/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 $_ }
    I could have sworn I saw this over on last week....?
    Anyway, I hope this resolves your question.
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930555]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-05-27 12:46 GMT
Find Nodes?
    Voting Booth?