Monday 15 September 2014

perl - Can we have variable keys defined inside a hash? -



perl - Can we have variable keys defined inside a hash? -

i trying create hash follows :

my $block_name = pm88_60_464 ; %hash_for_tstamp = qw ( layout/${block_name}.proj/tech ap_db_time rpts/sem_check/sem_check_analysis.rpt sem_analysis ); $key; $value; while (($key,$value) = each (%hash_for_tstamp)) { ${$time_summary}{"$value"} = ctime(stat($key)->mtime) ; }

while running above code getting below error.

stat() on unopened filehandle file::stat::layout/${block_name}.proj/tech . can't phone call method "mtime" on undefined value @ audit_automation.pl line 205.

so i've understood error allowed have variable keys within hash.

the issue 1 of variable interpolation, or lack thereof when using qw// construct. there no variable interpolation within of qw//, explained in perlop quote , quote-like operators

you can resolve situation using appropriate quote-like construct; in case, double-quotes variable interpolation desired. here's example:

my $block_name = "pm88_60_464"; %hash_for_tstamp = ( "layout/${block_name}.proj/tech", "ap_db_time", "rpts/sem_check/sem_check_analysis.rpt", "sem_analysis" ); while ( ($key,$value) = each (%hash_for_tstamp) ) { print "($key),($value)\n"; }

perl hash

No comments:

Post a Comment