Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting and storing the the values in key value pair from a text in file in perl

Tags:

hash

perl

I have a text file like which contains information like this:

name=A
class=B
RollNo=C

I want to extract the values in perl script

key(name) = value(A)
key(class) = value(B)
key(RollNo) = value(C)

the keys should be exported as the variables which will have values. Whenever we type

print $name

the output should be 'A'

I have tried:

open my $fh, '<', $file_name
  or die "Could not open sample.txt: $!";
my @lines = <$fh>;

my %hash;
while (<@lines>) {
    chomp;
    my ($key, $value) = split /=/;
    next unless defined $value;
    $hash{$key} = $value;
}
print %hash;
like image 909
sam Avatar asked Aug 27 '20 15:08

sam


People also ask

Which object is used to store key value pair?

A bucket is used to store multiple key value pairs . In hash map, bucket used simple linked list to store objects. Each bucket has a unique number , that's what identifies the bucket.

Which object can store data in key value form?

The dictionary stores objects as key-value pairs and can be used to represent complex real-world data.

How do I print a hash key and value in Perl?

print "$ perl_print_hash_variable{'-hash_key2'} \n"; Description: The Perl print hash can used $ symbol for a single hash key and its value. The Perl print hash can use the % symbol for multiple hash keys and their values.

How do you access the elements of a hash in Perl?

To access the value of a key value pair, Perl requires the key encased in curly brackets. Note that strings do not need to be quoted when placed between the curly brackets for hash keys and that the scalar sigil ('$') is used when accessing a single scalar value instead of ('%').


Video Answer


3 Answers

Your code looks pretty good and most of what you've done so far works.

At the end, you run print %hash and that doesn't give you what you expect. That will "unroll" the keys and values from the hash into a list and print that list. So you get all of the keys and values printed out.

If you just want one value (for example, the value associated with the "name" key), then just print that.

print $hash{name};

Is that what you were looking for?

like image 81
Dave Cross Avatar answered Oct 08 '22 14:10

Dave Cross


You could try using one of the configuration modules that are available. Config::Tiny seems to fit your data:

use strict;
use warnings;
use Data::Dumper;
use Config::Tiny;
 
my $Config = Config::Tiny->new;
 
$Config = Config::Tiny->read( 'a.txt' );   # your text file name goes here

print $Config->{_}{name};                  # print the name value
print Dumper $Config;                      # print all the values in perl variable format
like image 35
TLP Avatar answered Oct 08 '22 15:10

TLP


You can store data in hash and can retrieve from their.

use strict;
use warnings;

use Data::Dumper;

my %hash = (
    name    => 'A',
    class   => 'B',
    RollNo  => 'C'
);

print Dumper(\%hash);

print $hash{'name'};
like image 3
vkk05 Avatar answered Oct 08 '22 14:10

vkk05