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

In reply to Re^6: Accessing value from a hash table in perl by DAN0207
in thread Accessing value from a hash table in perl by DAN0207

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.