imnotverygood has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have created a small program to break up each different part of a syslog file, using regex and then rearranging them and then writing them to an output file. Example syslog line: Note, I know this isn't code.

Jan  4 12:05:21 ubuntu NetworkManager[1034]: <info> (eth0): now unmanaged

I have broken it down into

keyword = NetworkManager, host = ubuntu, datetime = Jan 4 12:05:21, info = <info> (eth0): now unmanaged, portno = NetworkManager[1034]

To write it to the file I have used this code

print OUTPUTINTER "**". $keyword. "**". " ". $host. " ". $datetime. " + ". $info. " ". $portno. "\n"; #The line above prints the following to the file output-inter.txt
However, when I open the file (output-input.txt). The port number is placed on a new line, even though I haven't asked it to. This only happens with the port number. This mucks up my next task as I want to bubble sort each line to put it in alphabetical order. I can't see a problem with my code. I need help, thanks

Replies are listed 'Best First'.
Re: Writing to a file is not being formatted correctly.
by choroba (Cardinal) on Dec 16, 2015 at 14:07 UTC
    That's because you haven't removed the newline from $info. Try
    chomp $info;

    before you output.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Writing to a file is not being formatted correctly.
by Laurent_R (Canon) on Dec 16, 2015 at 23:04 UTC
    choroba has given you the answer to your question (chomping the variable to get rid of the newline character). I would just want to react to something else that you said.
    This mucks up my next task as I want to bubble sort each line to put it in alphabetical order.
    Why do you want to bubble sort your lines? Bubble sort is a very inefficient sort algorithm. You may not care about sort efficiency and this may be OK if the amount of data to be sorted is small.

    But, yet, why would you want to implement yourself a bubble sort (or any other sort algorithm for that matter) when Perl has a sort built-in function, easy to use and implementing a much more efficient sort algorithm?