basically i am trying to search for similar strings in an array which is hardcoded into the script and compare them to and find the line they exist on the text file
here is my code so far

#!/usr/bin/perl
%fruit=(banana=>1,apple=>2,orange=>5);
$target = %fruit;

open(INPUT, "<fruits.txt");

while (<INPUT>) {
     if (/$target/) {
         print "Found $target on line $.";
     }
}
close(INPUT);

I have attached the file which i am to search .

Attachments
hello this is just random 
text that i am just typing 
for no reason while at the 
same time hoping that someone
on daniweb will help me find 
bananas , uhm i mean the solution 
to this problem. i will reward with an
apple or maybe
if i am not bored and have the money 
to drive to your house i might even give 
you an orange
%fruit=(banana=>1,apple=>2,orange=>5);
#$target = %fruit; 
#why u have used above line?what it means?in perl it is of no use.



open(INPUT, "<fruits.txt");

while (<INPUT>) 
{
        foreach $target(keys %fruit)
        {
                if (/$target/) 
                {
                        print "Found $target on line $.\n";
                }
        }
}
close(INPUT);

Edited 7 Years Ago by vbharathi: n/a

Comments
Helped very much

Hey thank you very much, i am new to perl and as you can see im trying to learn on my own. Been reading some online tutorials and so far i like perl but still a novice.

Hey that worked but i have another question and i dont want to start another thread ( will if i have to). How would i be able to delete a line that started with a certain string, for example in this file "you". Is this easy to approach? Is there a delete function used with

$target =~ m/you/;

$target =~ m/^[yY]ou/; # this will grab lines starting with you or You (not sure if the character class is right, but it looks to be).
just do an "if" on that above search, and then don't print if it contains it, now if it's a multiline situation, you'll have to do some more adjusting on what lines to not print.

So basically what you are saying onaclov2000 is that the only way to delete a line is to write the whole thing in a different file omitting the line you do not want that begins with the specified string?

So basically what you are saying onaclov2000 is that the only way to delete a line is to write the whole thing in a different file omitting the line you do not want that begins with the specified string?

Pardon my jumping in. I'm new to Perl but I have read there is such a thing as doing an in place edit on a file, which I think is what you are asking about. If you Google perl "in place edit" $^I you will see a lot has been written about it.

This article has been dead for over six months. Start a new discussion instead.