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

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.

Replies are listed 'Best First'.
Re^3: Accessing value from a hash table in perl
by poj (Abbot) on Sep 17, 2018 at 10:48 UTC
    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.

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

        poj
Re^3: Accessing value from a hash table in perl
by BillKSmith (Monsignor) on Sep 17, 2018 at 13:33 UTC
    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

        Be at peace. There was no need to delete anything anywhere. The monks (and probably the folks who attend perlguru) just like to know about cross-postings (ideally with a nice link) for the reason stated here.


        Give a man a fish:  <%-{-{-{-<

Re^3: Accessing value from a hash table in perl
by hippo (Archbishop) on Sep 18, 2018 at 14:00 UTC
    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.

Re^3: Accessing value from a hash table in perl
by BillKSmith (Monsignor) on Sep 17, 2018 at 22:09 UTC
    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