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

New Lines in an Output File

Hello All,
I am writing a perl program that pars through a text file and find a particular word and reverse the word. I can do that fine, but when creating the output file, the output files gives me that text file in one line. I want the progream to insert a new line anytime it sees the new line in the text file.
for example, my text file looks like this.

Expand|Select|Wrap|Line Numbers
  1. 1-database one was in here. where have you been these day.
  2. 2- I am not sure what do to with eht database.
  3. 3- perl is a good for parsing eht database data.
  4.  
but the output gives me:
1-database one was in here. where have you been these day.2- I am not sure what do to with eht database.3- perl is a good for parsing eht database data.


The program is.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open (FH, "text");
  4. open (out, ">", "out3");
  5.  
  6. while (<FH>) {
  7.     # chomp;
  8.     @ar = split (' ', $_);
  9.     for ($i=0; $i < @ar; $i++){
  10.         if ((@ar[$i] eq 'Database.') || (@ar[$i] eq 'Database')) {
  11.             $arr = @ar[$i];
  12.             $arrr = reverse ($arr);
  13.             @ar[$i] = $arrr;
  14.         } else {
  15.  
  16.         }
  17.         print out "@ar[$i] ";
  18.     }
  19. }
  20.  
thanks in advance for the help.
May 3 '07 #1
5 2382
miller
1,089 Expert 1GB
Hi beg,

Just add a print return line after your for loop:

Expand|Select|Wrap|Line Numbers
  1.     print out "\n";
  2. }
  3.  
- Miller

PS, there are lots of ways that your code could and probably should be improved. Namely the use of "array slices" is a bad habit to get into. And not declaring any of your variables with my is also unwise. Nevertheless, I leave this to you to discover in time.
May 3 '07 #2
miller
1,089 Expert 1GB
An expert method for doing what you described:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open(IN, "text") or die "Can't open text: $!";
  4. open(OUT, "> out3") or die "Can't open out3: $!";
  5.  
  6. while (<IN>) {
  7.     s/(Database\.?)/reverse $1/eig;
  8.     print OUT;
  9. }
  10.  
  11. close IN;
  12. close OUT;
  13.  
May 3 '07 #3
Hi Miller,
Thanks for your reply. I have used your code and it works the way I want it. But I just started to learn perl and wanted to use Array and manipulate the array content. The only problem I am having, how to detect a new line in my array and insert it in the outpurt file. for example, here my TEXT file that I am using and parsing:


Fhaw was in here
Deda was Database.
here we go
there we go


Database. j1-king senior technologists.
Database. across HP is an important step in strenghening our technology communit
y. To that end, I am pleased to announce the new technical Caree Path (TCP) Mas
ters
0- Database.
1- Database.
2- Database
3- Database
4- Database

And here is the output of my prgramm after I ran my program:

Fhaw was in here Deda was esabataD in here here we go there we go
test text. .esabataD j1-king senior technologists. .esabataD across HP
is an important step in strenghening our technology community. To th
at end, I am pleased to announce the new technical Caree Path (TCP)
Masters 0- .esabataD 1- .esabataD 2- esabataD 3- esabataD 4- esabataD.



I ran your code and it gave me what I want, but I need my program to gives me the same output that your code gave me.


By the way, here is what your code gave me after running it which is what I want


Fhaw was in here
Deda was esabataD in here
here we go there we go
test text.
.esabataD j1-king senior technologists.
.esabataD across HP
is an important step in strenghening our technology community. To th
at end, I am pleased to announce the new technical Caree Path (TCP)
Masters
0- .esabataD
1- .esabataD
2- esabataD
3- esabataD
4- esabataD
May 3 '07 #4
By the way,
Can you explain your code:

s/(Database\.?)/reverse $1/eig;

Please?

Thanks
May 3 '07 #5
miller
1,089 Expert 1GB
You can read about regular expressions here:

http://perldoc.perl.org/perlrequick.html

As for attempting your problem in a more verbose method. Here:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. open(IN, "text") or die "Can't open text: $!";
  6. open(OUT, "> out3") or die "Can't open out3: $!";
  7.  
  8. while (<FH>) {
  9.     chomp;
  10.     my @array = split ' ', $_;
  11.     for (my $i=0; $i<@array; $i++){
  12.         if (($array[$i] eq 'Database.') || ($array[$i] eq 'Database')) {
  13.             $array[$i] = reverse $array[$i];
  14.         }
  15.     }
  16.     print OUT join(' ', @array) . "\n";
  17. }
  18.  
  19. close IN;
  20. close OUT;
  21.  
As is almost always the case with perl, there are many different ways to make the above code more efficient in both a performance and a lines required sort of way. Nevertheless, this maybe will enlighten you concerning the problems you were having.

- Miller
May 4 '07 #6

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

Similar topics

18
by: Piotr Wolski | last post by:
i read the filr into an array using file(). some of the lines are empty. i would like to output the file but ignoring the emnpty lines. any ideas?
5
by: Jay Chan | last post by:
I am trying to use a command line program to run a stored procedure that generates output in a comma-delimitted format. Somehow, ISQL or OSQL always wrap the lines at 256 characters. I believe this...
26
by: Shannon Jacobs | last post by:
Sorry to ask what is surely a trivial question. Also sorry that I don't have my current code version on hand, but... Anyway, must be some problem with trying to do the negative. It seems like I get...
19
by: Alex Vinokur | last post by:
Is there any tool to count C-program lines except comments? Thanks, ===================================== Alex Vinokur mailto:alexvn@connect.to http://mathforum.org/library/view/10978.html...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
7
by: peraklo | last post by:
Hello, there is another problem i am facing. i have a text file which is about 15000 lines big. i have to cut the last 27 lines from that file and create a new text file that contans those 27...
8
by: Horacius ReX | last post by:
Hi, I need to write a program which reads an external text file. Each time it reads, then it needs to delete some lines, for instance from second line to 55th line. The file is really big, so...
2
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line:...
3
by: Robert | last post by:
I would like to count lines in a file using the fileinput module and I am getting an unusual output. ------------------------------------------------------------------------------...
4
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.