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 2382
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.
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
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
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.
------------------------------------------------------------------------------...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |