473,411 Members | 1,880 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,411 software developers and data experts.

Using the GREP Function in PERL 5.8

I've found a program and interpreter for PERL 5.8 so to everyone who tried to help me with that thank you.

Now my attention turns to the GREP function in PERL. From what I understand GREP is a simple pattern matching function that searches for either the full element in a string or a first letter of an element, but if anyone can clarify this that would be great :)

Expand|Select|Wrap|Line Numbers
  1. @lastNames = ('Sutherland','Summers','Sunley','Duran','Grieve')
  2. @JustSNames = grep(/^S/, @lastNames);
  3.  
For example in the above code I have a list and a regular expression that is trying to search for all the elements in this list that start with a letter S and return the full element.
Sep 6 '07 #1
4 2754
numberwhun
3,509 Expert Mod 2GB
Ok, so what is the problem? I ran what you have there and it works fine. If it is the fact that it does not print out the names, then it is because you haven't told it to do so. All you did was put them into an array. If you want them to print, try something like this:

Expand|Select|Wrap|Line Numbers
  1. my @lastNames = ('Sutherland','Summers','Sunley','Duran','Grieve')
  2. my @JustSNames = grep(/^S/, @lastNames);
  3.  
  4. foreach my $line (@JustSNames)
  5. {
  6.     print("$line \n");
  7. }
  8.  
That prints out the elements in the array @JustSNames.

Regards,

Jeff
Sep 6 '07 #2
Hello,

Well I compared our 2 bits of code and I believe I found where the problem was
Expand|Select|Wrap|Line Numbers
  1. @lastNames = ('Sutherland','Summers','Sunley','Duran','Grieve')
In the above line there is a semi colon missing.

Expand|Select|Wrap|Line Numbers
  1. @lastNames = ('Sutherland','Summers','Sunley','Duran','Grieve');
By simply adding the semi colon to the line, both code blocks seemed to work. The code on GREP that I worked from was very sketchy.

Thanks. :D
Sep 6 '07 #3
numberwhun
3,509 Expert Mod 2GB
You are completely correct. In fact, if this were in a script with warnings were used, you would have been given an error when executing. When I did this in a perl one liner, I had put in the semicolon after that line and didn't catch it. So, when I pasted the foreach statement in my posting, I had used your two lines. Must have wake-up juice before posting, I swear.

Regards,

Jeff
Sep 6 '07 #4
KevinADC
4,059 Expert 2GB
From what I understand GREP is a simple pattern matching function that searches for either the full element in a string or a first letter of an element, but if anyone can clarify this that would be great :)
That is not quite correct. grep() uses an argument to find things in lists, the argument can be a regular expression but it can be other things as well. For example:

@files = grep { (-d) } readdir DIR;

same as:

@files = grep( -d $_, readdir DIR);
Sep 6 '07 #5

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

Similar topics

2
by: John E. Jardine | last post by:
Hi, Problem: Executing 's///' has a side effect on grep null string matching. If line 62, the substitution, is executed the last two values returned by grep and printed on lines 68, 69 are...
1
by: bhavya sg | last post by:
Hi, I saw in PEP4 of python 2.5 that grep module has gone obsolete in perl 2.5. But I am not able to find an alternative for that.My doubt is "are the other forms of grep like egrep and ggrep...
4
by: js | last post by:
Just my curiosity. Can python beats perl at speed of grep-like processing? $ wget http://www.gutenberg.org/files/7999/7999-h.zip $ unzip 7999-h.zip $ cd 7999-h $ cat *.htm bigfile $ du -h...
6
by: None | last post by:
Hi, I was trying to use SWIG to expose some C++ functions to perl. But, I ran into some issues here. I don't know why, but I see that a macro in SWIG replaces "NORMAL" with "PL_op->op_next"... ...
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: gyandwivedi | last post by:
what should I use in perl /in place of grep -v in unix.
3
by: soniamadtha | last post by:
hello everyone, i have the following values : 98000345 and i would like to search this value in all of the sql files in the directory /temp/*sql normally on unix its grep( ' 98000345 '...
4
by: hassan470 | last post by:
I work in an investment bank. We have a huge log file which contains FIX messages. I have a problem to grep FIX messages within a time rage (i.e. between 9:30 am to 11 pm, those were sent by CITIBANK...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.