populate PERL Hash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting populate PERL Hash
# 1  
Old 07-27-2009
populate PERL Hash

Hello

I am new to perl and learning ...can you please explain why am i getting this error...as myHash is defined right above the code and I think it should be avilable in the sub function.

Here is my code:

<code>
#!/usr/bin/perl
use warnings;
use strict;
%myHash=();
sub populateHash
{
my $MY_FILE="file.properties";
open my $PFILE, '<', $MY_FILE or die "Cannot open '$MY_FILE' $!";
LINE: while (<$PFILE>)
{
# now read the line and remove all white spaces, tab and new line chars or any trailing spaces
$_ =~ s/\s+$//;
next LINE if (/^\s*#/); # ignore commented lines
next LINE if (/^$/); # ignore null lines
my $n=substr($_,0,index($_,'='));
my $v=substr($_,index($_,'=')+1);
$myHash->{$n}=$v;
}
close $PFILE;
}
&populateHash();
while ( my ($key, $value) = each(%myHash) ) {
print "From myHash --> $key => $value\n";
}

</code>

I get the below error:

<code>
Global symbol "%envPropertiesHash" requires explicit package name at first.pl line 5.
Global symbol "$envPropertiesHash" requires explicit package name at first.pl line 22.
Global symbol "%envPropertiesHash" requires explicit package name at first.pl line 31.
Execution of first.pl aborted due to compilation errors.

</code>
# 2  
Old 07-27-2009
Please CODE tags
Code:
<CODE>
</CODE>

Need to explicitly mention the variable as
Code:
my %envPropertiesHash;

# 3  
Old 07-27-2009
when you use the strict pragma, and you should, all lexical variables have to be declared with 'my' within their intended scope. If you don't understand what that means maybe some reading will help:

perlsub - perldoc.perl.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

4. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

5. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

6. Shell Programming and Scripting

perl DBI: populate a scalar from a select statement

hi every resource i see regarding DBI refers to retrieving data from a database into and array or a hash, but i havent seen anything on how to pull out a single value to a scalar in my database i have a field called "forcewrite" with a value of "6". I am trying to connect to the database,... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

7. Shell Programming and Scripting

perl hash

This is my data 1 0 1 0 1 1 1 2 1 6 1 7 Assume that first field is key and 2nd field is value I want to create a hash in perl, on this data. My hash should having uniq key and all values by , separated. 1,0,0,1,2,6,7 1 will be my key and rest of are should be values. (3 Replies)
Discussion started by: pritish.sas
3 Replies

8. Shell Programming and Scripting

perl help on hash

I have line which is read from xml doc. I want to put this line into hash(perl variable). find line below and how i want to put this in hash <font size="10" type="int" name="ABC" > hash key should be size, type and name with corresponding value I doing as below:- $line =~ s/\s*.*?\s//;... (3 Replies)
Discussion started by: aju_kup
3 Replies

9. Shell Programming and Scripting

Perl Hash

HI I have a hash like this $hashname->{$filesystem}->{'fsname'}=$filesystem; How to get the values from this multilevel hash. Thanks in advance... :) (1 Reply)
Discussion started by: Harikrishna
1 Replies

10. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question