PERL, search and replace inside foreach loop


 
Thread Tools Search this Thread
Top Forums Programming PERL, search and replace inside foreach loop
# 1  
Old 06-09-2010
PERL, search and replace inside foreach loop

Hello All,

Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location.

CODE:
Code:
..
..
...
foreach $key(keys %arr_hash) {
	my ($loc,$ind,$add) = split /,/, $arr_hash{$key}[0];
	&create_verilog($key, $loc, $ind ,$add);
}

sub create_verilog{
     $key= shift;
     $loc = shift;
     $ind = shift;
     $add = shift;

	foreach (@tplate1) {
	  s/NAME/$key/g;
	  s/INDEX/$ind/g;
	  s/LOCATION/$loc/g;
	  s/ADDR/$add/g;
	  push @arr_f, "$key\n";
	  push arr_f, $_;
	}
}


In the above code, for every $key value, I'm calling the create_verilog() function for replacing some of the strings in the array. But it repalces only with the first key. Even though $key is passing properly to the subroutine.

Any urgent help??


Thanks,
rIZ

Last edited by Franklin52; 06-09-2010 at 08:44 AM.. Reason: Please use code tags!
# 2  
Old 06-09-2010
Quote:
Originally Posted by riyasnr007
...
CODE:
Code:
..
..
...
foreach $key(keys %arr_hash) {
    my ($loc,$ind,$add) = split /,/, $arr_hash{$key}[0];
    &create_verilog($key, $loc, $ind ,$add);
}
 
sub create_verilog{
     $key= shift;
     $loc = shift;
     $ind = shift;
     $add = shift;
 
    foreach (@tplate1) {
      s/NAME/$key/g;
      s/INDEX/$ind/g;
      s/LOCATION/$loc/g;
      s/ADDR/$add/g;
      push @arr_f, "$key\n";
      push arr_f, $_;
    }
}


In the above code, for every $key value, I'm calling the create_verilog() function for replacing some of the strings in the array. But it repalces only with the first key. Even though $key is passing properly to the subroutine.
...
Not sure if this solves your problem, but the syntax of the push function is incorrect.

Maybe you intended to write the foreach loop like so ?

Code:
...
foreach (@tplate1) {
      s/NAME/$key/g;
      s/INDEX/$ind/g;
      s/LOCATION/$loc/g;
      s/ADDR/$add/g;
      push @arr_f, $_;
    }
...

tyler_durden
# 3  
Old 06-14-2010
the correct idiom for reading a hash is each

something like...
Code:
while (($key, $value) = each %hash) {
    create_verilog($key, split ',', $value);
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print perl hash value in foreach loop

Experts - Any advice on how to get a hash value in a foreach loop? Values print correctly on standalone print statements, but I can't access value in foreach loop. See sample code below and thanks in advance. foreach my $z (sort keys %hash) { for $y (@{$hash{$z}}) { print "$z... (6 Replies)
Discussion started by: timj123
6 Replies

2. Shell Programming and Scripting

Search inside a file, PERL

HEllo!! I need a script in perl that see inside a file(file.txt) that has only one row. The row contains only this text: logs_yearly = 20120628113345 I want that the script does this: "Search in the file, if the date 20120628 is actual date" Thank you!! ---------- Post... (4 Replies)
Discussion started by: super_sun
4 Replies

3. Shell Programming and Scripting

Speeding up search and replace in a for loop

Hello, I am using sed in a for loop to replace text in a 100MB file. I have about 55,000 entries to convert in a csv file with two entries per line. The following script works to search file.txt for the first field from conversion.csv and then replace it with the second field. While it works fine,... (15 Replies)
Discussion started by: pbluescript
15 Replies

4. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

5. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

6. Shell Programming and Scripting

loop to replace txt and add command inside of colon (:)

in one of my script..I have some thing like john:christina : one:: and i want to make it like john:chritina:two:(date command):jackey: basically change 'one' to 'two' and run date :command and add other name: (30 Replies)
Discussion started by: Learnerabc
30 Replies

7. UNIX for Dummies Questions & Answers

Foreach loop to run a perl script on multiple files

Hi, I have thousands of files in a directory that have the following 2 formats: 289620178.aln 289620179.aln 289620180.aln 289620183.aln 289620184.aln 289620185.aln 289620186.aln 289620187.aln 289620188.aln 289620189.aln 289620190.aln 289620192.aln.... and: alnCDS_1.fasta (1 Reply)
Discussion started by: greptastic
1 Replies

8. Shell Programming and Scripting

Replace aword in a.The replaced word should not be overwitten in perl(details inside)

Hi i am trying to write a perl program where i have to open a 1)directory "unit" 2) rename the files in the dir say file1.txt;file2.txt...file5.txt to file1_a.txt;file2_a.txt,....file5_a.txt ;file1_x.txt ;file2_x.txt 3) open these renamed files and replace the words lets say file1_a.txt... (7 Replies)
Discussion started by: madhul2002
7 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question