I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code.
my $current_value=12345;
my @users=("bob","ben","tom","harry");
open DBLIST,"<","/var/tmp/DBinfo";
my @input = <DBLIST>;
foreach (@users)
{
my $req_user_info=grep(/$_/,@input);
my ($user,$blah,$value)=split(/:/,$req_user_info);
my $diff=$current_value-$value;
print "$diff";
}}
Can some please help me understand what i am missing and how to edit this code to get the desired result.
It would have been better had your provided a sample input file of yours. At hindsight, this is one issue:
foreach my $x (@users)
{
my $req_user_info=grep(/$x/,@input);
my ($user,$blah,$value)=split(/:/,$req_user_info);
my $diff=$current_value-$value;
print "$diff";
}
@chidori : Did you try the above solution? I understand you tried to use $, but it should not be used when using grep. This is because grep internally uses $ to process each element of the array.
the output of grep looks like "0" and "1" so the variable $req_user_info is holding numbers. but i would require something like search for the pattern and when match is found store that line in variable
Looking at your code, it makes sense to use the first subroutine from the List::Util core module, instead of grep . With that, you may not use the set of parentheses.
Thanks for the suggestion. I am a beginner in perl so i am trying with what i have learnt. Will have check on that module. Also i dont like one part of my code
my ($user,$blah,$value)=split(/:/,$req_user_info);
if you see this. all i want is $value that is present in 3rd column. Well since split will start spliting from the start ( thats my understanding) i have to use 2 extra variables which i like to avoid. Any suggestions in doing that ?