473,614 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New Lines in an Output File

3 New Member
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 2414
miller
1,089 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
beginnerperl
3 New Member
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
beginnerperl
3 New Member
By the way,
Can you explain your code:

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

Please?

Thanks
May 3 '07 #5
miller
1,089 Recognized Expert Top Contributor
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
3056
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
23350
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 has something to do with the column width switch (-w). But enlarging the column width to 800 characters max still doesn't help. The following is a stored procedure that is essentially doing what my stored procedure is doing: create procedure...
26
11756
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 into these ruts each time I try to deal with regular expressions. All I'm trying to do is delete the lines which don't contain a particular string. Actually a filter to edit a log file. I can find and replace a thing with null, but can't...
19
5685
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 news://news.gmane.org/gmane.comp.lang.c++.perfometer =====================================
6
24144
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 like to split it and create a new file at every lets say 20000 lines... so, the directory output would have to be something like this:
7
2094
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 lines. and after that save both of those files... since that is a big block of text (15000 lines) i thint that it is a big job to look for a keyword... so my question is this exactly: how do i do this:
8
2208
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 what do you think is the fastest method to delete specific lines in a text file ? Thanks
2
19740
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: print line f.close()
3
3589
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. ------------------------------------------------------------------------------ #!/usr/bin/python import fileinput # cycle through files for line in fileinput.input(): if (fileinput.isfirstline()): if (fileinput.lineno 1):
4
7232
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 header that has been created on a Windows XP machine: My goal is to get rid of the header and the empty lines to finally have an output file with only the number entries.
0
8642
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8294
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8444
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6093
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5549
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2575
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.