in reply to Accessing value from a hash table in perl

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

Replies are listed 'Best First'.
Re^2: Accessing value from a hash table in perl
by DAN0207 (Acolyte) on Sep 17, 2018 at 05:58 UTC
    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
        As of now ,i have two files in the ditectory.File 1 has Fruit_class at the start of {}.File 2 has Avocado_class at the start of {}.These class names should repeat at the next {} also.
      You have cross posted this question on perlguru. We do not mind, but you should tell us to help us avoid duplicate effort.
      Bill
        Apologies.deleted the duplicate thread.Any help on perlmonks would be appreciated.I am desperately in need of help with perl code.thanks in advance
      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