Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^6: Accessing value from a hash table in perl

by DAN0207 (Acolyte)
on Sep 18, 2018 at 13:07 UTC ( [id://1222578]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^7: Accessing value from a hash table in perl
by poj (Abbot) on Sep 18, 2018 at 13:32 UTC
    This is not working

    You have omitted these lines after chomp from Bill's script

    s/^\s*//; next unless /Fruit_class/ .. /\}/; next if /Fruit_class|[{}]/;
    poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1222578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found