Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Need help from the esteemed monks on scoping/hashes by garbage777

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found