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. -
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.
-
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. -
#!/usr/bin/perl
-
-
open (FH, "text");
-
open (out, ">", "out3");
-
-
while (<FH>) {
-
# chomp;
-
@ar = split (' ', $_);
-
for ($i=0; $i < @ar; $i++){
-
if ((@ar[$i] eq 'Database.') || (@ar[$i] eq 'Database')) {
-
$arr = @ar[$i];
-
$arrr = reverse ($arr);
-
@ar[$i] = $arrr;
-
} else {
-
-
}
-
print out "@ar[$i] ";
-
}
-
}
-
thanks in advance for the help.
5 2414 miller 1,089
Recognized Expert Top Contributor
Hi beg,
Just add a print return line after your for loop:
- 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.
miller 1,089
Recognized Expert Top Contributor
An expert method for doing what you described: -
#!/usr/bin/perl
-
-
open(IN, "text") or die "Can't open text: $!";
-
open(OUT, "> out3") or die "Can't open out3: $!";
-
-
while (<IN>) {
-
s/(Database\.?)/reverse $1/eig;
-
print OUT;
-
}
-
-
close IN;
-
close OUT;
-
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
By the way,
Can you explain your code:
s/(Database\.?)/reverse $1/eig;
Please?
Thanks
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: -
#!/usr/bin/perl
-
-
use strict;
-
-
open(IN, "text") or die "Can't open text: $!";
-
open(OUT, "> out3") or die "Can't open out3: $!";
-
-
while (<FH>) {
-
chomp;
-
my @array = split ' ', $_;
-
for (my $i=0; $i<@array; $i++){
-
if (($array[$i] eq 'Database.') || ($array[$i] eq 'Database')) {
-
$array[$i] = reverse $array[$i];
-
}
-
}
-
print OUT join(' ', @array) . "\n";
-
}
-
-
close IN;
-
close OUT;
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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...
|
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...
|
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
=====================================
|
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:
| |
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:
|
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
|
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()
|
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):
|
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.
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |