tags:

views:

99

answers:

3

My name is abbi

My first perl script run on linux machine

This script read the INI file called (input) and print the values of val , param , name .....

How to create loop that print values of val1-valn OR loop to print values of param1-paramn... etc? (in place the print command's in the script )

  • the loop must have option to match the parameter for example print only param1 until paramn values

n - Is the last number of each param

 #!/usr/bin/perl




 open(IN,"input") or die "Couldn't open input: $!\n"; 
 while(<IN>) { 
 chomp; 
 /^([^=]+)=(.*)$/; 
 $config{$1} = $2; 

 } 
 close(IN);


 print $config{val1};
 print $config{val2};
 print $config{val3};

 print $config{param1};
 print $config{param2};
 print $config{param3}; 

 print $config{name1};
 .
 .
 .
 .

example of the ini file from linux machine

cat input

  val1=1
  val2=2
  val3=3
  param1=a
  param2=b
  param3=c
  name1=abbi
  name2=diana
  name3=elena
A: 

How about this:

use warnings;
use strict;
my %config;

open my $input, "<", "input"
    or die "Couldn't open input: $!\n"; 
while(<$input>) { 
    chomp; 
    if ( /^([^=]+)=(.*)$/) { 
        $config{$1} = $2; 
    }
} 
close($input) or die $!;

for (sort keys %config) {
    if (/param\d+/) {
        print "$config{$_}\n";
    }
}
Kinopiko
hi Kinopiko , can I set argument in place paramfor example in place param I set $param , and $param can be any word ??abbi
abbi
little problem for example if param1A its also match it , in case of param<number> have some char on the right side as param1X then its must not match it , the rule must as for example param<NUMBER> how to solve this problem?
abbi
@abbi: use a $ at the end of the regular expression.
Kinopiko
YES if (/val\d+$/) , this is the right solutionabbi
abbi
abbi
@abbi: could you try to do that yourself as an exercise?
Kinopiko
I try but I not sure what I need to do -:( ?
abbi
abbi
OK, well ask as another question, giving the script. Don't forget to accept an answer for this question too.
Kinopiko
I think I will open anew question in this forum?abbi
abbi
OK I accept your solution , And I will ask anew question soon abbi
abbi
+2  A: 

According to your last comment, this will do what you want:

use strict;
use warnings;

my %config;
my $max_n = 0;
my $input = 'input';
open my $in, '<', $input
    or die "unable to open '$input' for reading: $!";
while (<$in>) {
    chomp;
    if (/^(.*?(\d+))\s*=(.*)$/) { 
        $config{$1} = $3; 
        $max_n = $2 if $2 > $max_n;
    }
}
close $in or die "unable to close '$input': $!";

for my $n(1..$max_n) {
    for my $param (qw/val param/) {
        print "$param.$n = $config{$param.$n}\n" if exists $config{$param.$n};
    }
}
M42
hi M42 the same question for you:, can I set argument in place param for example in place param I set $param , and $param can be any word ?? abbi –
abbi
@abbi: Yes of course
M42
abbi
see updated answer
M42
hi M42 see I get ERRORGlobal symbol "$max_n" requires explicit package name at ./new_ini line 22.
abbi
did you see the problem?
abbi
Did you really copy paste the code ? I think you forgot my $max_n on the 5th line.
M42
No the copy is OK (the $max_n) in the line I am sure about this-:(abbi
abbi
This message is typicaly of a non-declaration. May be you put this declaration inside another bloc of code ?
M42
sorry my fault I forget the my $max_n = 0;its seems OK I now check it with some parameters please wait
abbi
abbi
I edited the answer. But you could find it by yourself. please upvote this answer if it is usefull !!
M42
did you mean to remark the "V" its to vote?abbi
abbi
No, but you can increase the number on the top left of the answer. Not just to get some more points, but to show to ones who search for an answer to a similar question retrieve it more easily.
M42
I see but I need the open ID this aprocedure that I need to perfrom?
abbi
+2  A: 

You can use Config::Tiny to read your .ini file. Then you can use the returned hash to filter what you want.

sebthebert
+1 for not reinventing the wheel.
Jonas