print "Enter the Name: ";
my $Name = <>;
print "Word is $Name";
open (FH,"AIDNameList.txt");
while (<FH>)
{
my $line;
print "Word is $Name";
for $line(<FH>)
{
for ($line =~ /$NameAid/g ) {
print $line;
}
}
}
close FH;
I need to search for the user entered value in Variable $Name and search in each line of the file "AIDNameList.txt". I tried many ways but cudnot find how to give the word in the for loop.
print "Enter the Name: ";
my $Name = <>;
print "Word is $Name";
open my $FH, '<', 'AIDNameList.txt' or die "Can't read AIDNameList: $!";
print grep { /$Name/ } <$FH>;
close $FH;
Sorry, my bad, you'll have to chomp the input, otherwise it'll include the newline character in the search:
print "Enter the Name: ";
my $Name = <>;
chomp $Name;
print "Word is $Name";
open my $FH, '<', 'AIDNameList.txt' or die "Can't read AIDNameList: $!";
print grep { /$Name/ } <$FH>;
close $FH;
sorry jisha for stealing your thread, i did 1 line change making chomp when in accept the input. i use this frequently but i have not seen in actual coding. can someone tell me reason ? if there is any reason behind it.
I used to get unexpected output before chomp was used. chomp removes the trailing strings usually the new line character. it should be used because a string with a newline character and same string with out the new line charecter are different.
eg: $Name = "Alina"; and $Name = "Alina " are different .
Regular expression search will not return the intended value if chomp is not used in these cases .