I am trying to process the data in a single file . 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 . I need to append the value of fruitname and print the the entire file content as output. Any help will be appreciated.Given below is the output that i got.

$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
        };

Dear Jag_1,

I have seen the effort you put into having the result you wanted. But you are not doing it rightly.

You can, like you are doing use regex to pick what you need and modify the file to want you need.

Since, this is your first post on this platform. I will show you one to to achieve what you want to do.

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $hash = {};
my $class_name;

while (<DATA>) {

    # remove all the spaces at the begin and the end of each line
    s/^\s+|\s+$//g;

    if ( /Name.+/ ... /}/ ) {
        my $line = [ split /\s+/ => $_, 2 ];    # split each line
        if ( $line->[0] eq "fruitName" ) {      # at every instance of fruitName
            $class_name = $line->[1];           # get a new fruit clas
        }
        else {
            next if /Id|Name|fruitName|\}/;     # don't include this in our hash
                 # modify to remove the word fruit from each line
            ( my $sub_class = $line->[0] ) =~ s/fruit(.+)$/$1/ig;

            # get the wanted values
            push @{ $hash->{$class_name} } => { $sub_class => $line->[1] };
        }
    }
}

print Dumper $hash;

# to print out. Not in Dumper style.
sub {
    my $table = shift;
    for my $fruit ( keys %$table ) {
        print $fruit, $/;
        map { print "\t", join( "\t" => %$_ ), $/ } @{ $table->{$fruit} };
    }
  }
  ->($hash);

__DATA__
{
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
        }
}

Take a clue from the above.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.