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 .

%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);
commented: Helped very much +3

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/;
Member Avatar for onaclov2000

$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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.