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

Help with a perl problem

Hi everyone,

I am trying to teach myself Perl with the aid of some books I have boughten. I'm doing some exercises with regular expressions but am having some trouble, specifically with this problem:

Write a program that prints only words (not the entire line) in the file part3.txt (attached) on a word boundary that have the letter "p" or "P" in them.

The Unix command line should look like:

$ proj2_3.pl part3.txt

The output should look like:

Perl
pattern
pattern
Perl
help


The text file given is this:

CPAN stands for comprehensive Perl Archive Network.
^ and $ are used as anchors in a regular expression.
/pattern/ is a pattern match operator.
Perl is very easy to learn.
Enter 'H' or 'h' for help.


So far what I have is this:

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/env perl
  2.  
  3. open  (test, "@ARGV");
  4.  
  5. while (<test>) {    
  6.     m/p/gi;
  7.     print;
  8. }
  9.  
  10. close (test);
Someone please lend a helping hand :)
Mar 18 '08 #1
4 1561
/bump

No one has any idea about this? Is there some regular expression that can find a character and print out the word it's in?
Mar 18 '08 #2
KevinADC
4,059 Expert 2GB
The obvious way is to use the split() function and split the lines into words then check each word for /[Pp]/ instead of each line, like you are doing. But I have no idea what the book you are reading has covered so far so making specific suggestions may not be relevant to where you are in the lessons/book.

Kevin(R) ;)
Mar 18 '08 #3
eWish
971 Expert 512MB
Check out perlre and perlrequick. Look for "Match a word boundary"

--Kevin
Mar 18 '08 #4
nithinpes
410 Expert 256MB
You should also look out for '/' or '.' occuring near word boundaries. The following script will do the job.

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/env perl
  2. while (<>) {    
  3.          while(/\b[.\/]?(\w+)[.\/]?\b/g) {
  4.              $word = $1;
  5.              print "$word\n" if($word=~/^p|p$/i);
  6.        }
  7. }
  8.  
But, inorder to understand this regex better or to extend this for some other scenario, kindly go through the links posted by Kevin.
Mar 19 '08 #5

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

Similar topics

2
by: Tomislav Lepusic | last post by:
Hello, I don't know if this is the right group (I'm more in Perl, know nothing about Python), so if you can help me thanks, if not, sorry to bother you. I'm working on my student project and...
7
by: codeslayer | last post by:
Greetings to everyone in ‘forum-land': I have a problem that has plaguing me to no end. It is a CSS-related question, and I have not seen this question posted anywhere in forums or through...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
0
by: gedsta | last post by:
Hi all I am a total noob and its my 2nd post, first of all thank you for letting me use these forums as a source of information, and hopefully once i get my head round perl i maybe able to help...
10
by: stylecomputers | last post by:
Hey guys, I am absolutely new to Linux programming, with no w######s programming experience except a small amount of C++ console apps. Reasonably new to Linux, BSD etc, got good sound networking...
6
by: Henry Law | last post by:
I'm trying to debug a Perl problem whose symptoms are that a running Perl process either doesn't receive, or fails inexplicably to handle, a SIGTERM sent from the OS during shutdown (Linux Fedora...
5
by: enkidu72 | last post by:
Hello everybody , I have a problem with arrays ... I need to get some data from files in some directories . The names of the dirs are something like 12.12344 or 23.34441. Some contains one ,...
2
by: geniuskanwal | last post by:
Before I begin to explain my problem, I just want to say that I can do the following two things: 1. Using Perl, connect to a MS Access Databse Table and perform the required operations.(Database...
1
rajiv07
by: rajiv07 | last post by:
Hi to all I have some problem to use DBIx::Class package My::Schema; use base qw/DBIx::Class::Schema::Loader/; __PACKAGE__->loader_options( debug => 1, );
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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...

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.