Perl language - Arrays problem

MrBig55

Distinguished
Jun 27, 2011
62
0
18,590
Hi all,

I've been coding PERL for a moment but I've come to a wall. My program is supposed to read an array, and report every element on it's line, one for each element. The thing is that the same coding acts differently in the terminal and in the logged file. Here's a sample:

Code:
#!/usr/bin/perl -W
use strict;
require 5.001;

my @errors;
open (LogFile, ">>LocationOfLogFile")

push (@errors,"Program error 1:\n\t$!\n\n");
push (@errors,"Program error 2:\n\t$!\n\n");
push (@errors,"Program error 3:\n\t$!\n\n");
print ("\nErrors Encountered in \@errors:\n\n@errors");
print LogFile ("\nErrors Encountered in \@errors:\n\n@errors");
print ("\nEnd of program");

In the console it's writting every error stored and it respects the escape sequences as intented:
Errors Encountered in @errors:

Program error 1:
File cannot be found....blablabla <- this is the $! variable

Program error 2:
Another error reported on it's line...

Program error 3:
And one more...

End of program
but in the logged file:
Errors Encountered in @errors:

Program error 1:File cannot be found....blablablaProgram error 2:Another error reported on it's line...Program error 3:And one more...

End of program

See the way it is all messed up. Tried different things but I just can't figure what is going on. Please anyone got an idea how to do that ? I ate my fingers already and going for the whole arm...
 

MrBig55

Distinguished
Jun 27, 2011
62
0
18,590
No one can help me?? Please I need that. :-(

Is there a way to take every single element from that array and add a newline character before logging every one of it?? Said I tried different ways so here are those solutions that didn't work so far:

Code:
foreach $errorsLOG (@errors) {
	print LogFile ("$errorsLOG\n");
}
Code:
@errorsLOG = map( { $_."\n" } @errors );
Code:
@errorsLOG = map( { $_."\\n" } @errors );
Code:
@errorsLOG = map( { $_.'\n' } @errors );
Code:
print LogFile $errors[0..25];
Code:
print LogFile "$erreurs[0]\n$erreurs[1]\n$erreurs[2]\n$erreurs[3]\n$erreurs[4]\n";
Actually looking to print automagically every element in one command since there is no way for me to know how many elements there is in the array @errors.

EDIT: Was tired of that being all fussy so I temporarily removed the "BINMODE" command, Of course I needed it at first since my program is compatible with all Windows (XP32, XP64, Vista32, Vista64, Win7-32, Win7-64) and all ubuntu (since I use my hand coded installer to install everything, including PERL 5.14). Here's the command I used:
Code:
binmode (LogFile) if $os eq "windows";
Months ago I used BINMODE cause in Windows the logged file was all written on a single line, and that corrected the log bug... but now it works without it... And I don't understand why it works now (without binmode) and it didn't months ago with the same program (with a few less functions but still it was the same program and the same log). All I know is that in the past months, I had to remove the binmode too for the log to be functionnal, and a few days later I had to put it back cause the log was AGAIN written on a single line... These strange behaviors are pretty frustrating.

Well for the time being I'll just log everything without BINMODE, but since it interfers with my read from file attempts, I have to find a way to fix this.

Oh well thanks for not answering at all.

BTW I just can't put this thread as SOLVED since I know my program will act as before and log everything on a single line AGAIN in a few days... I'm now used to it... Feel free to give me your opinions on that. And don't ask me to take a look at my actual program since it is WAY TOO COMPLEX to be analyzed by people not experienced in PERL coding...
 

MrBig55

Distinguished
Jun 27, 2011
62
0
18,590
sorry it's been a long time. Yes Everything I've posted here has been tried before posting. My problem is still not solved yet but I guess I will always have a problem cause I run it in every Windows and Linux possible with the same program.

Meanwhile I'm still looking for solutions to this problem but also for a few other problems I have for about a year already. Oh well I guess not much member knows much about PERL. But I like that language since it comes from C# and can do about anything and still is easy to implement. Thanks though for your time ^-^
 

Zenthar

Distinguished
Dec 31, 2007
250
0
18,960
I can try a few things tomorrow, I'm by no mean a Perl developer. I think it's more of a very advanced scripting language than a programming language anyway (more a judgement of what the language should be used for than the quality).

As for Perl coming from C#, you got it backward as Perl is barely younger that C++ istelf (1987 vs 1983).