in reply to Re^2: Accessing value from a hash table in perl
in thread Accessing value from a hash table in perl

I have a few input files

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

poj
  • Comment on Re^3: Accessing value from a hash table in perl

Replies are listed 'Best First'.
Re^4: Accessing value from a hash table in perl
by DAN0207 (Acolyte) on Sep 17, 2018 at 15:08 UTC
    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.

      Post the contents of file2 along with the code you have so far (in code brackets please)

      poj
        I am trying to process the data in a single file now. i have to read the file and create a hash structure,get the value of fruitname append it to fruitCount and fruitValue and delete the line fruitName and write the entire output after the change is done.Given below is the content of file.
        # 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 tried with the following code.
        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash_table; my $name; my $file = '/tmp/fruitdir/fruit1.txt'; open my $fh, "<", $file or die "Can't open $file: $!"; while (<$fh>) { chomp; if (/^\s*fruitName/) { ($name) = /(\".+\")/; next; } s/(fruitCount|fruitValue)/$name\.$1/; my ($key, $value) = split /\s+/, $_, 2; $hash_table{$key} = $value; } print Dumper(\%hash_table);
        This is not working and i am getting the following output.
        $VAR1 = { '' => undef, 'time' => '11:15 ', 'date' => '14/07/2016', '{' => undef, '#' => 'this is a new file', 'total' => '30 ', 'end' => '11:20 ', 'No' => '"FRUITS"', 'Fruit_class' => undef, '}' => undef };