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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Troubles appending arrays

Status
Not open for further replies.

Perlwannabe

Technical User
Mar 7, 2006
57
IT
Hello,

I made a simple script that picks up words from a text file A and save them in text file B each time I call the script.
I would that it appends the new array just under the existing one but the first word stands on the side:

Array:

bla
bla
bla
bla

After I add a new array should be:

bla
bla
bla
bla
bla
bla
bla
bla

Instead is:

bla
bla
bla
blabla (the last and the first coincide)
bla
bla
bla

Why this? What can I do?
Here is the very short script (didaptic):

Code:
#!perl

# reading data from file A

# open file A to read
open(A,"filea.txt")||die("could not open file!");

# store them in an array
@a=<A>;

# close
close(A);

# writing data on file B

# open file B to write (append)
open(B,">>fileb.txt")||die("could not open file!");

# print data
print A;

# close
close(B);

# open file B to print a check
open(B,"fileb.txt")||die("error");
@b=<B>;
close(B);

# print results as html
print "Content-type: text/html\n\n";
foreach $line(@b)
{
print $line;print"<br>\n";
}
exit;

Thanks for a kind help

Sincerely
 
Excuse me:

I made a mistake writing the function under this comment:

# print data

It is:

# print data
print B(@a);

Thanks!
 
Note 1: "print B(@a)" should be "print B (@a);" with a space, otherwise an undefined function error will be thrown.

All that is your problem is that the final line of your filea.txt data file does not contain a return character. You can edit the data file yourself and add such a character, or you can add the following code to protect against that condition.

Code:
# close
close(A);

# Fix final return character.
chomp($a[-1]); $a[-1] .= "\n";
 
And as a minor tidbit, you could clean up your code with some use of the File::Slurp module.

Code:
#!perl

use File::Slurp qw(read_file append_file);

# reading data from file A
my $data = read_file('filea.txt');
chomp($data); $data .= "\n";

# writing data on file B (append)
append_file('fileb.txt', $data);

# open file B to print a check
my @b = read_file('fileb.txt');

# print results as html
print "Content-type: text/html\n\n";
foreach $line(@b)
{
print $line . "<br>\n";
}
exit;
 
Thanks very much MillerH!

I checked your code to fix the final return character
but now it "skips" the appending:

bla
bla
bla
bla

after the appending:

bla
bla
bla

bla
bla
bla

Seems like if it deletes last and first array elements...
 
It works fine on my end. In order to help you debug whatever you're doing wrong, I'll need to see your current code. It would also be a good idea if you would attach your data filea.txt. Or is it still just "bla\n" x 4?
 
Very grateful for your support.

Here is my current code:
Code:
#!perl

# reading data from file A

# open file A to read
open(A,"filea.txt")||die("could not open file!");

# store them in an array
@a=<A>;

# close
close(A);

# prepare to append
$a[-1] = "\n";
chomp($a[-1]);

# writing data on file B

# open file B to write (append)
open(B,">>fileb.txt")||die("could not open file!");

# print data
print B (@a);

# close
close(B);

# open file B to print a check
open(B,"fileb.txt")||die("error");
@b=<B>;
close(B);

# print results as html
print "Content-type: text/html\n\n";
foreach $line(@b)
{
print $line;print"<br>\n";
}
exit;

I just inverted $a and chomp function but now it seems to "eat" the last array element...

As regard my filea.txt it is just "bla"x4, nothing complex.

Thanks again
 
As I thought, you did not paste my code in correctly. Look again at what I gave you and paste it correctly. Instead of chomping an optinal return character and then forcing the additional of one, you are setting the last element to a return character and then chomping it. Along with the wrong order, you removed the concatenation operator.

If you really want it in two lines then just use this:

Code:
# Fix final line's return character
chomp($a[-1]);
$a[-1] .= "\n";
 
OK!!!
It works fine!

yes, I missed the "." operator in pasting...

Thank you!

I see you the next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top