LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-02-2005, 10:14 AM   #1
CyberJedi
LQ Newbie
 
Registered: Jun 2004
Location: New England
Distribution: Slackware v9.1
Posts: 5

Rep: Reputation: 0
Question Perl regular expressions


I'm somewhat new to Perl and just started getting into Perl's powerful Regular Expressions. On a tutorial that I'm using (with no solutions to critique my learning), I came across an exercise that asks to write a Perlscript to read a file, number each found line, and find any occurrences of words with double-letters and to put them in parentheses when it finds them.

Here's my script that's supposed to match a double-letter (and number each line upon finding them) but this is all I came up with. Only for matching single letters at a time. I can't get it to match various double-letters in words.


$" = "0";
$file = '/etc/electricity.txt'; # Name the file
open(INFO, $file); # Open the file
@lines = <INFO>; # Read it into an array
close(INFO);
foreach $line (@lines)
{
$_ = $line;
if (s/(pp)/(\1)/g)
{
print $"."$_\n";
++$";
}

The output should look like this......

01 I was ru(nn)ing in the field and tri(pp)ed over a rock. Thereby, hi(tt)ing my head on the ground.

I used a miscellaneous text file to extract the double-letters from.

Can anyone help? I can't figure it out.

Thanks in advanced.
 
Old 10-02-2005, 10:54 AM   #2
puffinman
Member
 
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 31
I'm not sure if you wanted to number each instance, or just put the line number on each line where you had a double. Anyway, I did the latter. Here is a very short script which does it. I urge you to learn certain idioms, like the while(<>){} loop and the fact that a for loop where you don't specify a variable defaults to $_ (so you can just do for (@lines) and the line will automatically be in $_). I also here used the special variable $. which is the current line number (actually current input record number, which is normally a line). Feed this script the name of the file on the command line.

Learn the features of perl that make it beat the crap out of C for text processing!! Cheers!

Code:
#!/usr/bin/perl

while (<>) {
  s/((.)\2)/(\1)/g ? print "$. $_": print;
}

Last edited by puffinman; 10-02-2005 at 11:00 AM.
 
Old 10-02-2005, 11:17 AM   #3
CyberJedi
LQ Newbie
 
Registered: Jun 2004
Location: New England
Distribution: Slackware v9.1
Posts: 5

Original Poster
Rep: Reputation: 0
Cool!

I understand your use of the regular expression in your script to find double-letters.

Thanks for the help and advice.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl regular expressions daYz Programming 6 08-11-2005 07:27 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
Perl and Regular Expressions Fonk Programming 1 06-27-2004 11:32 AM
Perl Regular Expressions true_atlantis Programming 1 11-15-2003 05:16 PM
regular expressions? alaios Linux - General 2 06-11-2003 03:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration