LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-04-2010, 08:51 AM   #1
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Perl coding help!!


Guys,
Please look at the following input files and the code.
cat temp.txt
Code:
aaaaaaaaaaaaaaaaaaaa1.bbbbbbbbbbbbbbbbbbbb2.cccccccccccccccccccccc
3. ddddddddddddddddddddddddd 4. eeeeeeeeeeeeeeeeeeeeeeee 
5. ffffffffffffff6.ggggggggggggggggggg
.........................
what I want is to insert a newline before every no.This is the tried.
Code:
open TEMP,"temp.txt";
open OUT,">out";
@a=<TEMP>;
foreach(@a)
{
$_=~s/[0-9]/[0-9]\n/;
print OUT $_;
}
close TEMP;
But I am getting the desired output which should be like:
Code:
aaaaaaaaaaaaaaaaaaaa
1.bbbbbbbbbbbbbbbbbbbb
2.cccccccccccccccccccccc
3. ddddddddddddddddddddddddd
4. eeeeeeeeeeeeeeeeeeeeeeee 
5. ffffffffffffff
6.ggggggggggggggggggg
Please assist me.
Thanks in advance.
 
Old 02-04-2010, 09:08 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Your grammar really doesn't make any sense to me, but to insert a newline before each number use this:

s/([0-9])/\n\1/g;

And feed it the whole file, not the individual chars on at a time!

If you want to handle multi-digit numbers, use this:

s/([0-9]+)/\n\1/g;
 
Old 02-04-2010, 11:05 PM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Original Poster
Rep: Reputation: 32
Thank you.It's working.
But, may I know where my code went wrong?
I used
Code:
$_=~s/[0-9]/[0-9]\n/;
$_ which you didn't used takes by default. So, I think there is no mistake here.
=~,used to match and assign at the same time.
s///,used for substitution.
1st [0-9]used to match the digit.
2nd [0-9]used to print a digit. I think the interpreter doesn't know which digit to choose. Is the code wrong went wrong here??
Even though I will not get the output as I were expected, this should print any digit between [0-9] instead of [0-9]. Why this is not happening? Could you please explain me???
 
Old 02-04-2010, 11:10 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ashok.g View Post
Thank you.It's working.
But, may I know where my code went wrong?
I used
Code:
$_=~s/[0-9]/[0-9]\n/;
$_ which you didn't used takes by default. So, I think there is no mistake here.
=~,used to match and assign at the same time.
s///,used for substitution.
1st [0-9]used to match the digit.
2nd [0-9]used to print a digit. I think the interpreter doesn't know which digit to choose. Is the code wrong went wrong here??
Even though I will not get the output as I were expected, this should print any digit between [0-9] instead of [0-9]. Why this is not happening? Could you please explain me???
Regarding item in red - you wrongly understand how 's' operator works.

Read

perldoc perlop

- 's' operator is explained there pretty well.

Also, read

perldoc perlretut
perldoc perlre

- both these documents also have explanations on 's' operator.
 
Old 02-05-2010, 07:11 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by ashok.g View Post
2nd [0-9]used to print a digit. I think the interpreter doesn't know which digit to choose. Is the code wrong went wrong here??
Yes. Also, even if it would work, it would put the newline after the digit.

To use the s/// command to replace parts of the original text onto the output, put all the parts you want to retrieve in ()'s. Then use \1, \2 \3, etc. allt the way to \9 to refer to these parenthesized parts.

Example:

$var = 'ab'
$ab =~ s/(a)(b)/\2\1/;

Now $var contains "ba".
 
  


Reply



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
Help with perl coding standard kdelover Programming 10 12-21-2009 08:11 AM
Python/perl coding guides? Michael_aust Linux - Newbie 5 08-29-2006 03:41 PM
Coding a WM? SocialEngineer Programming 1 05-20-2005 08:32 AM
Emacs module for better perl coding rose_bud4201 Linux - Software 3 04-27-2005 02:39 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

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

All times are GMT -5. The time now is 02:02 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