http://www.perlmonks.org?node_id=930559


in reply to perl script for adding information to c file.

  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.