search an array for strings

Reply

Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training

search an array for strings

 
0
  #1
32 Days Ago
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
  1. #!/usr/bin/perl
  2. %fruit=(banana=>1,apple=>2,orange=>5);
  3. $target = %fruit;
  4.  
  5. open(INPUT, "<fruits.txt");
  6.  
  7. while (<INPUT>) {
  8. if (/$target/) {
  9. print "Found $target on line $.";
  10. }
  11. }
  12. close(INPUT);
I have attached the file which i am to search .
Attached Files
File Type: txt fruits.txt (327 Bytes, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: vbharathi is an unknown quantity at this point 
Solved Threads: 1
vbharathi vbharathi is offline Offline
Newbie Poster
 
1
  #2
32 Days Ago
  1. %fruit=(banana=>1,apple=>2,orange=>5);
  2. #$target = %fruit;
  3. #why u have used above line?what it means?in perl it is of no use.
  4.  
  5.  
  6.  
  7. open(INPUT, "<fruits.txt");
  8.  
  9. while (<INPUT>)
  10. {
  11. foreach $target(keys %fruit)
  12. {
  13. if (/$target/)
  14. {
  15. print "Found $target on line $.\n";
  16. }
  17. }
  18. }
  19. close(INPUT);
Last edited by vbharathi; 32 Days Ago at 5:35 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training
 
0
  #3
32 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training
 
0
  #4
32 Days Ago
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
  1. $target =~ m/you/;
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 49
Reputation: onaclov2000 is an unknown quantity at this point 
Solved Threads: 5
onaclov2000 onaclov2000 is offline Offline
Light Poster
 
0
  #5
32 Days Ago
$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.
My Blog:
Onablog
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 74
Reputation: notuserfriendly is an unknown quantity at this point 
Solved Threads: 5
notuserfriendly notuserfriendly is offline Offline
Junior Poster in Training
 
0
  #6
32 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster
 
0
  #7
29 Days Ago
Originally Posted by notuserfriendly View Post
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.
Reply With Quote Quick reply to this message  
Reply

Tags
array, perl, search, string

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC