473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

using grep

Hi I want to is this right if I want to search an array and evaluate as true for IF statement...
Expand|Select|Wrap|Line Numbers
  1. if(grep{ $_ == $p }@p1)
  2. {
  3.  
  4. }
Any Suggestions...
Oct 16 '07 #1
4 1702
eWish
971 Expert 512MB
They way you are trying I don't believe will return anything.
Expand|Select|Wrap|Line Numbers
  1. my @p1 = (qw{22 33 43 56 77 34});
  2. my @p2;
  3. my $n = 34;
  4.  
  5. if (@p2 = grep {/^$n$/} @p1) {
  6.     print @p2,"\n";
  7. } else {
  8.     print "No Match's were found\n";
  9. }
Oct 17 '07 #2
KevinADC
4,059 Expert 2GB
They way you are trying I don't believe will return anything.
It will return 1 (true) or 0 (false). But it is not very efficient because it has to check every element of the array. A for/foreach loop should be more efficient and could be way more efficient in some circumstances. For example:

Expand|Select|Wrap|Line Numbers
  1. @array = 0..1000000000;
  2. $p = 0;
  3.  
  4. for (@array) {
  5.    if ($_ == $p) {
  6.       print "true";
  7.       last;
  8.    }
that will be much faster than:

Expand|Select|Wrap|Line Numbers
  1. if (grep {$_ == $p} @array) {
  2.    print "true";
  3. }
Oct 17 '07 #3
It will return 1 (true) or 0 (false). But it is not very efficient because it has to check every element of the array. A for/foreach loop should be more efficient and could be way more efficient in some circumstances. For example:

Expand|Select|Wrap|Line Numbers
  1. @array = 0..1000000000;
  2. $p = 0;
  3.  
  4. for (@array) {
  5.    if ($_ == $p) {
  6.       print "true";
  7.       last;
  8.    }
that will be much faster than:

Expand|Select|Wrap|Line Numbers
  1. if (grep {$_ == $p} @array) {
  2.    print "true";
  3. }

thanks for the suggestion...I asked for that as it was not working for me with the code I mentioned....
Anyway I will be using the for loop....
Oct 17 '07 #4
KevinADC
4,059 Expert 2GB
It will work:

Expand|Select|Wrap|Line Numbers
  1. @p1 = 1..5;
  2. $p = 3;
  3. if (grep{ $_ == $p } @p1) {
  4.    print "true";
  5. }
  6. else {
  7.    print "false";
  8. }    
Oct 17 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: championsleeper | last post by:
i am trying to write a script that will: - check an integer value to see if it has a particular pattern (regular expression match) - tell me what the particular pattern is. I am searching...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
3
by: fox | last post by:
I need to extract patterns from a line in a web page and these patterns sometimes show up twice in the same line so using grep with the pattern only grabs one. Exaple is I need <td width="30%"...
9
by: niteck07 | last post by:
I am using following shell script to ftp files to another server but this is failing as the shell script changes the user name for the ftp login the correct user name is 'ag\invprint' which the...
1
by: seforo | last post by:
Hi Guys, I am using using grep to look for certain patterns in files and it can't give me the output I want. I know that the pattern, TMatrixT<double>::Determinant() Const is there...
3
by: Manogna | last post by:
hi ! I have two big files like A anb B. A contains nearly 1000 lines. and B contains 1500000 lines.
1
by: tapmas | last post by:
Newbie question -- any help very much appreciated: I want to be able to get grep (or whatever else would work) to return not only matching lines, but also the original input string: An example may...
2
by: powerfulperl | last post by:
I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumption) from the beginning of the file out of 5000 lines. The 100th line start with the...
1
by: rameshjumgam | last post by:
Hi in my file im having value like cpu speed is 1000.000 but i wants to pick only before special character .(dot) please help me someone ? thanks in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.