DAN0207 has asked for the wisdom of the Perl Monks concerning the following question:

Hi , My requirement is to use a hash table to store the key value pair from the file as given below.
# this is a new file { date 14/07/2016 time 11:15 end 11:20 total 3 No "FRUITS" Fruit_class { Name "fruit 1" fruitName "apple_fru" fruitId "0" fruitCount 5 fruitValue 6 }
I have tried to create a hash and push data into it. But then i am stuck on how to get the value of fruitname "apple.fru" and append it to fruitCount and fruitValue like apple_fru.fruitCount and apple_fru.fruitValue and delete the line fruitName . Any help would be appreciated.

Replies are listed 'Best First'.
Re: Accessing value from a hash table in perl
by BillKSmith (Monsignor) on Sep 15, 2018 at 16:06 UTC
    Is This what you want???
    ?type DAN0207.pl use strict; use warnings; use Data::Dumper; my %hash_table; my $name; while (<DATA>) { chomp; s/^\s*//; next unless /Fruit_class/ .. /\}/; next if /Fruit_class|[{}]/; if (/^\s*fruitName/) { ($name) = /(\".+\")/; next; } s/(fruitCount|fruitValue)/$name\.$1/; my ($key, $value) = split /\s+/, $_, 2; $hash_table{$key} = $value; } print Dumper(\%hash_table); __DATA__ # this is a new file { date 14/07/2016 time 11:15 end 11:20 total 3 No "FRUITS" Fruit_class { Name "fruit 1" fruitName "apple_fru" fruitId "0" fruitCount 5 fruitValue 6 } ?perl DAN0207.pl $VAR1 = { 'Name' => '"fruit 1"', 'fruitId' => '"0" ', '"apple_fru".fruitValue' => '6 ', '"apple_fru".fruitCount' => '5' }; ?
    Bill
      Thank you so much. It really helped me. Now i have a few additional requirements to be implemented.I am trying to write but i am not getting result as expected. The requirements are: 1) I have a few input files in directory /tmp/fruits with the content of the files as given below:
      # this is a new file { date 14/07/2016 time 11:15 end 11:20 total 30 No "FRUITS" Fruit_Class { Name "fruit 1" fruitName "apple.fru" fruitId "0" fruitCount 5 fruitValue 6 } { Name "fruit 2" fruitName "orange.fru" fruitId "1" fruitCount 10 fruitValue 20 } }
      I have to read each file from the directory and process the data and write the result to separate output files which looks like
      # this is a new file {date 14/07/2016 time 11:15 end 11:20 total 30 No FRUITS Fruit_Class { Name fruit_1 fruitId 0 apple_fru.fruitCount 5 fruitValue 6 } Fruit_Class { Name fruit_2 fruitId 1 orange_fru.fruitCount 10 fruitValue 20 } }
      The fruit_Class has to be added at the beginning of each {}.How can i each achieve this.I have to use hash(key,value) concept. Any help would be much appreciated.
        I have a few input files

        Are all the files 'Fruit_Class' ? If not what other classes are there.

        poj
        You have cross posted this question on perlguru. We do not mind, but you should tell us to help us avoid duplicate effort.
        Bill
        I have to use hash(key,value) concept.

        TIMTOWTDI and here is a counter-example.

        use strict; use warnings; use Test::More tests => 1; my $in = <<EOIN; # this is a new file { date 14/07/2016 time 11:15 end 11:20 total 30 No "FRUITS" Fruit_Class { Name "fruit 1" fruitName "apple.fru" fruitId "0" fruitCount 5 fruitValue 6 } { Name "fruit 2" fruitName "orange.fru" fruitId "1" fruitCount 10 fruitValue 20 } } EOIN my $want = <<EOWANT; # this is a new file {date 14/07/2016 time 11:15 end 11:20 total 30 No FRUITS Fruit_Class { Name fruit_1 fruitId 0 apple_fru.fruitCount 5 fruitValue 6 } Fruit_Class { Name fruit_2 fruitId 1 orange_fru.fruitCount 10 fruitValue 20 } } EOWANT my $name = ''; my $out = ''; my $nr = 0; for (split "\n", $in) { tr /"//d; s/ (\d)/_$1/ if $nr++ > 5; if (/^\s+fruitName\s+(.*?)$/) { ($name = $1) =~ s/\./_/; next; } s/fruitCount/$name.fruitCount/; s/^$/Fruit_Class/ if $nr > 10; if (/^{/) { $out .= $_; next; } $out .= "$_\n"; } is ($out, $want);

        Note that I've removed the arbitrary trailing tabs from both files where they existed before importing. If you need those then it's up to you to implement that part as it is genuinely arbitrary.

        Your 'requirement' to 'use hash(key,value) concept' sounds very much like a homework assignment. We are glad to help, but not to do your assignments. (I may have made that mistake on your first question.) Do as much as you can yourself. Show us where you get stuck. Be sure to give us enough code (and data) that we can duplicate your problem.
        Bill