CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
text formatting problem


Jerry In Maine

Oct 29, 2009, 10:25 AM

Post #1 of 3 (198 views)
text formatting problem Can't Post

This is for a web display of data from a solar energy project...

I'm reading a text file that has two lines and contains this text:



This file will eventually be generated by a piece of hardware the monitors power consumpotion of devices connected to it.

I want to take these and format them into what will eventually an .xml file. This file will be referenced by a javascript in the HTML and will display a bar graph. I had hoped to see this in the output:

<data unit= "heater" value= "1"/>

<data unit= "refrigerator" value= "2"/>

<data unit= "oven" value= "3"/>

<data unit= "outlets" value= "4"/>

I wind up with a file that has the first 3 lines correct, but the last line always appears like this:

<data unit= "outlets" value= "4

Note that the 4th line seems to always have a newline between "4" and "/" and I'm not sure why.

I've not done anything in PERL for a long time...The trouble might be staring at me right in the face but somehow I'm missing it. Any help?

Thanks -

Here's the script


#open file - read into an array called @bar_data

open(BAR_DATA_FILE, $bar_data_raw) ;

@bar_data = <BAR_DATA_FILE>;
close (BAR_DATA_FILE);

#take the first two .csv lines and split into separate elements

@points = split(',',$bar_data[0]);
@items = split(',',$bar_data[1]);

#added chomp get the newline off the end of the last element of the first line

#I had thought that this might be my problem...

chomp($items[@points - 1]);


#check to see that there are an equal number of devices and data points

if(@points != @items){
print ("Error in bar data file.\nNumber of devices and data points do not match.\n");


#assemble the elements of the two arrays into an xml file entry

#Example of a properly formatted entry if <data unit= "heater" value= "1"/>


while ($i < @items){

push(@file_entry, "\<data unit\= \"$items[$i]\" value\= \"$points[$i]\"\/\>\n");
$i = $i + 1;


#open a file and write the array to it.

print @file_entry;

open FILE, ">temp.txt";
print FILE @file_entry;
close FILE;


Oct 29, 2009, 11:28 AM

Post #2 of 3 (197 views)
Re: [Jerry In Maine] text formatting problem [In reply to] Can't Post


open(BAR_DATA_FILE, $bar_data_raw) ; 

@bar_data = <BAR_DATA_FILE>;
close (BAR_DATA_FILE);


open my $bar_data_fh, '<', $bar_data_raw or die "could not open $bar_data_raw: $!\n"; 
chomp( @bar_data = <$bar_data_fh> );
close $bar_data_fh or die "could not close $bar_data_raw: $!\n";


I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

Jerry In Maine

Oct 29, 2009, 11:36 AM

Post #3 of 3 (195 views)
Re: [shawnhcorey] text formatting problem [In reply to] Can't Post

yep- that did it...thanks!

Can you give me a quick explanation of what the difference is?


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives