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

Not appending but instead replacing 2

Status
Not open for further replies.

Zac5

Programmer
Jan 30, 2003
75
US
Hi,

simple experiment where array stores a name of a colour in each element. I want to print colours to error log as blackbluewhiteredyellow on the same line, but this doesnt work correctly, i only see the last colour in the array, it does seem to be writing the other items too but instead of appending it overwrites, any ideas as to what i am doing wrong please?

foreach $c (@Colours)
{
$conc .= $c;
$c = "";
}
print STDERR $conc;

many thanks,

Zac
 
Code:
@Colours = qw(black blue white red yellow);
print STDERR join ("", @Colours);

outputs:-

Code:
blackbluewhiteredyellow


Kind Regards
Duncan
 
or even simpler:-

Code:
@Colours = qw(black blue white red yellow);
print STDERR @Colours;


Kind Regards
Duncan
 
Zac --

Your original solution works when I try it, though it isn't necessary to set $c = "" each time through the loop, as it takes on a new value on each iteration anyway. Perhaps the error is somewhere else in your code. Are you sure @Colours really contains what you think it does?
 
thanks for your replies, but unfortunately this has not helped, perhaps more information from me might help:

I am reading a field from a database, which contains a list of colours, into an array. Because the list contains line breaks I am doing the following:

my @Colours=split(/\n/,$A{'collist'});

I have checked each element by printing seperately to error_log to see what it contains eg:

print STDERR $Colours[0];

and it shows the correct colour.

However, if I do:

foreach $c (@Colours)
{
$conc .= $c;
}
print STDERR $conc;

although the contents of each element is printed quickly to the error log I only see the contents of the last element, with the last few letters of the longest colour that was shown, for example, supposing the contents of the last element is White and some previous element is Chrome, the error_log will display Whitee. This only happens when I print to error log or append to a field. If I print directly to screen it works as one would expect i.e ChromeWhite......

I hope this is clearer. Please help thank you.
 
Ok, I think I see what is happening. The file you are reading from is a DOS-formatted file - it contains both linefeeds AND carriage returns at the end of each line. You are stripping out the linefeeds by splitting on them - but the carriage returns remain. As a result, after your script prints each color, it prints the trailing carriage return, which returns the cursor to the beginning of the line, and the next color overwrites the previous one.

To correct, just split on carriage return/linefeed pairs:

my @Colours=split(/\r\n/,$A{'collist'});

Alternatively, you could strip out the carriage returns later, before you concatenate them:

foreach $c (@Colours)
{
$c=~s/\r//g;
$conc .= $c;
}
print STDERR $conc;



--G
 
can you post the output of the following:-

Code:
my @Colours=split(/\n/,$A{'collist'});
print STDERR @Colours;

or the string value of $A{'collist'} before it reaches the split


Kind Regards
Duncan
 
Ok thank you all very much, all your help was much appreciated. I used Geekatron solution and it worked. Once again thank you all.

Zac.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top