Adding information to c file using perl


 
Thread Tools Search this Thread
Top Forums Programming Adding information to c file using perl
# 8  
Old 10-06-2011
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

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

# 9  
Old 10-07-2011
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.
Code:
#!/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.

Last edited by veerubiji; 10-10-2011 at 05:14 AM..
# 10  
Old 10-10-2011
Hi skrynesaver,
please can you reply me . what mistakes i did in the above code.
# 11  
Old 10-10-2011
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 Smilie )
Code:
#!/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 $_
}

# 12  
Old 10-10-2011
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.
# 13  
Old 10-10-2011
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.
Code:
while(<$code>){
     if (/$test_string/o){
         $_=$nums{$data{$1}}.$_;
         $nums($data{$1}=""; 
 }

# 14  
Old 10-11-2011
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
Code:
#include<stdio.h>
void main()
{
 /*test name*/  ( add information here)
if {
    ........
    .........
     (string found here)
.........
}...............

sorry for asking question once agin .
regards,
veerubiji
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding to an array in an external file, and adding elements to it.

I have an array in an external file, "array.txt", which contains: char *testarray={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};I want to be able to add an element to this array, and have that element display, whenever I call it, without having to recompile... (29 Replies)
Discussion started by: ignatius
29 Replies

2. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Web Development

Tapatalk Modification for vB3 - Adding User for System Information

Referring back to this thread: Tapatalk Modification for vB3 - Issue with Avatar Icons I mentioned that we had some "system bot" code: In this post, I describe that code and how easy it is to create a "system bot" user for Tapatalk (TT): Basically, its quite easy. We add a hook to... (0 Replies)
Discussion started by: Neo
0 Replies

4. UNIX for Dummies Questions & Answers

Perl - adding columns to file

I have a file in which I need to add more columns to based on a key in the first file: File1 key1,abc,123, key2,def,456, key3,ghi,789, File2 key2,zyx,111,qqq, key3,yuu,222,www, key1,pui,333,eee, key4,xxx,999,rrr, I would like to create the following output: Output (1 Reply)
Discussion started by: WongSifu
1 Replies

5. Shell Programming and Scripting

Parsing information in perl

So i'm trying to write a perl script that logins into a network switch via ssh: #sh ip traffic IP statistics: Rcvd: 1460119147 total, 563943377 local destination 0 format errors, 0 checksum errors, 48401998 bad hop count 0 unknown protocol, 8379279 not a gateway ... (2 Replies)
Discussion started by: streetfighter2
2 Replies

6. Shell Programming and Scripting

perl script to print file information - newbie

Hi I have a perl script that prints all the video and audio file information(playing duration). It works fine in one of my friends linux laptop. But it doesn't work in my both windows and linux. My friend told me I have to do install some module ( ppm instal ...... ) but I have no... (1 Reply)
Discussion started by: srijith
1 Replies

7. Shell Programming and Scripting

Print file information using ffmpeg in perl

I am trying to print file information using ffmpeg tool in perl Here is my code use strict; use warnings; use IPC::Open3; # example my $filename = $ARGV; my %videoInfo = videoInfo($filename); print "duration: " . $videoInfo{'duration'} . "\n"; print "durationsecs: " .... (0 Replies)
Discussion started by: srijith
0 Replies

8. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

9. Shell Programming and Scripting

cant get perl to pull information right

Hello, I cant get the perl script to pull the information from Sark DNS 4.X Options ACL Templates= and other= Can someone look at the script to see why and fix it please. FYI..Under Sark DNS 4.x ACL Templates= and other= has an indent/tab, not sure if thats the reason my the script... (24 Replies)
Discussion started by: richsark
24 Replies

10. Shell Programming and Scripting

Perl: adding columns in CSV file with information in each

Hi Wise UNIX Crew, I want to add 3 different columns to the file in which: 1. The first new column pulls in today's date and time 2. Second column one has a '0' 3. Third column has the word 'ANY' going down the column If my file content is as follows: "7","a","abc",123"... (1 Reply)
Discussion started by: dolo21taf
1 Replies
Login or Register to Ask a Question