Hi Monks,
This is my first posting and I am relatively new to Perl. Hence please excuse if I failed to follow any posting rules.
I have a text file called "top.spef" that has the following lines -
*NAME_MAP
*1 ab
*2 abc
*3 abcd
*4 def
*5 ghi
*6 klm
*7 mno
*8 kji
*9 ips
*10 dlm
*PORTS
*D_NET *2 25
*D_NET *3 16
*D_NET *5 8
*D_NET *9 3
Here is what I want to do -
1. I want to remove "*" wherever it occurs,
2. I want to store the lines in between "NAME_MAP" and "PORTS" into a hash.
3. I want to replace numbers 2, 3, 5 and 9 in second column in lines that contain "D_NET" with the corresponding values of keys 2, 3, 5 and 9 from hash in step#2 above .
In order to do this, I wrote a small script as below -
#!/usr/bin/perl
#use strict;
use warnings;
open(IHF, "<", "top.spef");
while( <IHF> ) {
my @keys;
my ($n, $k1);
my %nets;
my @net;
my $item;
next if (/^\n/);
s/\*//g;
s/^\s//g;
s/\//:/g;
### Here we store the lines between the lines
### NAME_MAP and PORT into a hash called %mapping"
### and print the key,value pairs
if (/NAME_MAP/ .. /PORTS/) {
next if (/NAME_MAP/);
next if (/PORTS/);
%mapping = split;
@keys = sort keys %mapping;
foreach $k1 (sort keys %mapping) {
print "$k1 = $mapping{$k1}\n";
}
}
if (/D_NET/) {
my @anet = split;
%nets = ($anet[1] , $anet[0]);
for $n (sort keys %nets) {
print "$mapping{$n}\n";
}
}
}
When I execute this code I get the following error -
Use of uninitialized value in concatenation (.) or string at ./mapping
+.pl line 39, <IHF> line 23
I am assuming this error is issued by Perl because probably the hash %mapping scope may be not visible in the below part of the code -
if (/D_NET/) {
my @anet = split;
%nets = ($anet[1] , $anet[0]);
for $n (sort keys %nets) {
print "$mapping{$n}\n";
}
}
Can you kindly suggest me what I am doing wrong and how to fix this problem ?
I very much appreciate all your help in advance.
Best Regards,
Dan
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.