Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substitution 1

Status
Not open for further replies.

altaratz

ISP
Apr 15, 2001
73
US
I know this is so simple, but I'm still a real newbie at Perl. I'm trying to search through a space delimited text file, and replace the spaces with comma delimits. The lines of data is as such:

AAA BBB CCC DDD EEE FFF
XXX YYY ZZZ RRR GGG YYY

and I want

AAA,BBB,CCC,DDD,EEE,FFF
XXX,YYY,ZZZ,RRR,GGG,YYY


so some elements are separated by 3 spaces, some by 2 spaces, and some by 1 space.

My code looks like this


$counter = 1;

#############open test file#####################

open (TESTFILE, ">>test.txt");

@element=<TESTFILE>;

$lengtharray = @element;

if ($counter<$lengtharray) {

$element[$counter]=$a;

$a=~s/ /,/;
$a=~s/ /,/;
$a=~s/ /,/;



$counter++;



}

close (TESTFILE);



nothing is happending - can someone kindly help. Thanks!

Ethan
 
altaraz,

Try this:

$element[$cnt] =~ s/\s+/,/g;

This will replace any whitespace with a ','. The + sign will match all of the whitespace and the g substitutes globally. If you have a comma on the end of each line you could use the chop command to take off the comma.

Brandon
 
You could also try the following line of code. It's not as fast as luckydexte's, but it's useful with other things (like removing spaces from filenames and such)

$a = join ',', (split / /, $element[$cnt]);
 
some of your problems are structural and some are in your syntax.
- Using >> in your open statement opens the file for appending, not reading
- Your 'if' needs to be a 'while'. As it is, it will only run once.
I think you want to run once for each line.
- you can do the space replacement with on regex.

$counter = 1;
open (TESTFILE, &quot;[red]>>[/red]test.txt&quot;);
@element=<TESTFILE>;
$lengtharray = @element;
[red]if[/red] ($counter<$lengtharray) {
$element[$counter]=$a;
$a=~s/ /,/;
$a=~s/ /,/;
$a=~s/ /,/;
$counter++;
}
close (TESTFILE);

a slightly different approach, do the entire file in one shot
Code:
# open test.txt for reading
open(TESTFILE, &quot;<test.txt&quot;) or die &quot;open test.txt, $!\n&quot;;
while (<TESTFILE>) { $buffer .= $_; }
close TESTFILE;

# replace one or more spaces with a comma
$buffer =~ s/ +/,/gs;

# open test.txt for writing
open(OPF,&quot;>text.txt&quot;) or die &quot;open test.txt, $!\n&quot;;
print OPF &quot;$buffer&quot;;
close OPF;

HTH If you are new to Tek-Tips, please use descriptive titles, check the FAQs,
and beware the evil typo.
 
I like doing things in a more straight forward way. Like this:

#!/usr/local/bin/perl
open(IN, &quot;input.dat&quot;) or die &quot;Cannot open datafile. $!&quot;;
open(OUT, &quot;>output.dat&quot;) or die &quot;Cannot open output file. $!&quot;;

while (<IN>) { # Read a line from the input file
chomp; # Remove CR/LF
s/^\s+//; # Remove any whitespace at the beginning
s/\s+$//; # Remove ant whitespace at the end
s/\s+/,/g; # Replace space(s) with a comma
print OUT &quot;$_\n&quot;; # Print the line to the output file
}
close(IN);
close(OUT);
 
I have to say I agree with esserc. In my book, straightforward is the best way, even if it makes for longer code - it's the easiest to understand and maintain.
Hardy Merrill
Mission Critical Linux, Inc.
 
Thanks to all very much for the help - I ended up donig it esserc's way - thanks!

Ethan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top