Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Accessing the hash name in perl

by 1nickt (Canon)
on Mar 22, 2017 at 12:58 UTC ( [id://1185445]=note: print w/replies, xml ) Need Help??


in reply to Accessing the hash name in perl

As hippo said, your code goes about your task in an over-complicated way. Just load the library normally and call a function in it. ( See the monastery's Tutorials on modules. )

MyLib.pm :

package MyLib; use strict; use warnings; sub get_data { my %hash = ( A => { a => 1, b => 2, }, B => { a => 3, b => 4, }, ); return \%hash; } 1;
my_script.pl :
use strict; use warnings; use MyLib; my $hashref = MyLib::get_data(); my %hash = %{ $hashref }; for my $key ( keys %hash ) { printf( 'Value of `a` in %s is %s', $key, $hash{ $key }->{'a'} ); } __END__

Or, use a data format suited to hashes:

my_data.yaml :

--- A: a: 1 b: 2 B: a: 3 b: 4

my_script2.pl :

use strict; use warnings; use YAML qw/ LoadFile /; my $hashref = LoadFile('my_data.yaml'); my %hash = %{ $hashref }; for my $key ( keys %hash ) { printf( "Value of `a` in %s is %s', $key, $hash{ $key }->{'a'} ); } __END__

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Accessing the hash name in perl
by Sonali (Novice) on Mar 23, 2017 at 10:40 UTC

    The hash structure is generated, so I can't change the structure of it, just extract it and modify it

      The hash structure is generated, so I can't change the structure of it, just extract it

      Sometimes you just need to take out the chainsaw.

      This assumes your file is just as you put it, parts always in quotes called hash1, hash2, hash3, there always are keys in quotes called paramA and paramB, and the values of those keys are always in quotes.

      #!/usr/local/bin/perl use strict; use warnings; Generate(); sub Generate { Process_File('hash.pm'); } sub Process_File { my $filename = shift; open(my $fh, '<:encoding(UTF-8)', $filename) or die "Could not open file '$filename' $!"; my $file=''; while (my $line=<$fh>) { $file.=$line; } for my $n (1..3) { if ($file =~ m/\'hash$n\'.+?\{(.*?)\}/ms) { my $part=$1; my ($typ_s) = $part=~/\'paramA\'.*?\=\>.*?\'(.*?)\'/ms; my ($paramb)= $part=~/\'paramB\'.*?\=\>.*?\'(.*?)\'/ms; print "n $n typ_s $typ_s paramb $paramb\n"; } # part } # n }
      Result
      D:\goodies\pdhuck\down1\perl\monks>perl 1185438.pl n 1 typ_s 00 paramb FF n 2 typ_s 01 paramb 02 n 3 typ_s 00 paramb 03
      Im sure others can improve on this too.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1185445]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found