Bioinfocoder has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perlmonks! I am trying to read a file with user information categorized under a location, I want to fill in the some of the fields using user input and output the file while keeping the fields under each location intact for eg - file
[California] ;;Name $;FIrst_Name = ;;Last_name $;Last_Name= ;;Age of member $;Age = [NewYork] ;;Name $;FIrst_Name = ;;Last_name $;Last_Name= ;;Age of member $;Age = [Washington] ;;Name $;FIrst_Name = ;;Last_name $;Last_Name= ;;Age of member $;Age =
Once user provides input from command line it should look it
[California] ;;Name $;FIrst_Name = Jack ;;Last_name $;Last_Name= Daner ;;Age of member $;Age = 27 [NewYork] ;;Name $;FIrst_Name = Jill ;;Last_name $;Last_Name= XYZ ;;Age of member $;Age = 30 [Washington] ;;Name $;FIrst_Name = Kim ;;Last_name $;Last_Name= ABC ;;Age of member $;Age = 25
The order of First_Name, Last_Name and Age within each location can change and even order of locations can change, but each location section should remain separate and intact. I wrote following code so far and some of my code works for taking whole file in one hash, but i am not able to preserve each location section within it! I tried using two hashes - can someone please help me this complex problem! Thanks a lot.
open($input.file, "<$InputFile") or die "Error! Cannot open $InputFile + for reading: $!"; while (<$input.file>) { $_ =~ s/\s+$//; next if ($_ =~ /^#/); next if ($_ =~ /^$/); if ($_ =~ m/^\[(\S+)\]$/) { $sComponent = $1; next; } elsif ($_ =~ m/^;;\s*(.*)/) { $sDesc .= "$1."; next; } elsif ($_ =~ m/\$;(\S+)\$;\s*=\s*(.*)/) { $sParam = $1; $sValue = $2; } $sParam = $sValue = $sDesc = ""; next; } }

Replies are listed 'Best First'.
Re: Read an input file in two hashes to retain order
by 1nickt (Canon) on Mar 18, 2016 at 15:13 UTC

    Hi Bioinfocoder,

    It looks like your file is basically an ini-style file with sections, keys and values, with the addition of the "nice name" subsection labels.

    I would remove the "subsection" labels, which are really part of your presentation, and put them in a hash keyed by the real config names for lookup and use when needed. Then I would use Config::Tiny::Ordered to read from and write to the file.

    Edit: Since you say "The order of First_Name, Last_Name and Age within each location can change and even order of locations can change, but each location section should remain separate and intact" you could just use Config::Tiny which is a little easier to work with since you use named hash keys rather than array indices to work with the config settings.

    Hope this helps!


    The way forward always starts with a minimal test.
      thanks 1nickt for the response, I was trying hashes as the input file might end up having many variables and different user input options at later stages, for eg just calling one location etc, so I thought to start with hashes. I will try the Config::Tiny as well. I remember using Template module earlier, but it was messing up with the location+it's variables orders.