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

Changing an array into a string

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

I am currently using a rich text area to edit HTML pages. The idea is to open a HTML file using...

Code:
open (LOG, "$initial_path$path_carry") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
@edit_page = <LOG>;
close (LOG) || Error ('close', 'file');

My first question: Is this the best way to open a file? Because I have never understood why the file should be opened into an array? Why not into a scaler?

I then take the contents of the HTML file (@edit_page) and use this as the rich test areas value...

However, it doesn't like me using an array (@ symbol) therefore it doesn't display properly. If I use a scaler variable then it works fine.

If I could turn the array into a scaler (string), then this should work fine.

My second question: How can I print the contents of the file into the rich text area.

If you would like to see this part of the script let me know.

Thank you,

Chris
 
Okay stupid question really...

I used:
Code:
$scaler = "@array";

But its still not working... I think its possibly something to do with the <html> <body> etc tags
 
Right I think I have norrowed down my problem. In the source code of the webpage, at the end of the @array that has been printed into the variable, it leaves a space like this (I have labelled the array contents as ARRAY VALUE:

Code:
var oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
oFCKeditor.BasePath	= 'fckeditor/' ;
oFCKeditor.Height	= 400 ;
oFCKeditor.Value	= 'ARRAY VALUE
' ;
oFCKeditor.Create() ;

I think this is messing with the javascript and it should be like this:

Code:
var oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
oFCKeditor.BasePath	= 'fckeditor/' ;
oFCKeditor.Height	= 400 ;
oFCKeditor.Value	= 'ARRAY VALUE' ;
oFCKeditor.Create() ;

Any ideas on removing the line space...

I thought about maybe removing the ' ; and adding ' ; to the end of the array...

Ill try it
 
Nothings working :s ill post my code anyway:

For a working demo:

For the page I am working on:
1) go to 2) Click Open
3) Locate a .htm page
4) Click Edit Page

Code:
################################
## -Begin- Edit Page Template ##
################################

##### if Edit Page button pressed
if ($edit_pageb) {

##### Open page into variable
open (LOG, "$initial_path$path_carry") || Error('open', 'file');
flock (LOG, 2) || Error('lock', 'file');
@edit_page = <LOG>;
close (LOG) || Error ('close', 'file');

$edit_page2 = "@edit_page";

print "Content-type: text/html\n\n";
print <<"HTML code";
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<!--
 * FCKeditor - The text editor for Internet - [URL unfurl="true"]http://www.fckeditor.net[/URL]
 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    [URL unfurl="true"]http://www.gnu.org/licenses/gpl.html[/URL]
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    [URL unfurl="true"]http://www.gnu.org/licenses/lgpl.html[/URL]
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    [URL unfurl="true"]http://www.mozilla.org/MPL/MPL-1.1.html[/URL]
 *
 * == END LICENSE ==
 *
 * Sample page.
-->
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
	<title>Edit Page</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="robots" content="noindex, nofollow" />
	<link href="$initial_url_fckeditor_css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="$initial_url_fckeditor_js"></script>
</head>
<body>
	<h1>
	<input type="button" value=" - " onClick="history.go(-1);return true;" style="font-family: Arial; font-size: 10pt; font-weight: bold">&nbsp;<input type="text" name="create_pageb_path" size="40" value="$path_carry" readonly="true" style="font-family: Arial; font-size: 10pt; font-weight: bold; border-style: solid">
	</h1>
	<div>
		
	</div>
	<hr />
	<form action="$script_name" method="post">
		<script type="text/javascript">
<!--
// Automatically calculates the editor base path based on the _samples directory.
// This is usefull only for these samples. A real application should use something like this:
// oFCKeditor.BasePath = '/fckeditor/' ;	// '/fckeditor/' is the default value.
var sBasePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('_samples')) ;

var oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
oFCKeditor.BasePath	= 'fckeditor/' ;
oFCKeditor.Height	= 400 ;
oFCKeditor.Value	= '$edit_page2' ;
oFCKeditor.Create() ;
//-->
		</script>
		<br />
		<input type="submit" name="edit_pageb_execute" value="Change" style="font-size: 10pt; font-family: Arial; font-weight: bold" />
        <input type="hidden" name="drive_carry" value="$drive_label">
	</form>
</body>
</html>
HTML code
##### end if Edit Page button pressed
}

##############################
## -End- Edit Page Template ##
##############################

Cheers
 
$var = @array
will give you a count of elements in the array where as $#array will give you the last index of the array
if you want to treat an array as a variable you can do ${@array} but it is rare that you will probably need that.

if you printing you can just print @array;

your getting an extra new line because your not chomping your array;
chomp @array;
I always chomp my file's that are pulled into array's as soon as I do it so I don't run into that later.
after you chomp you should be able to do
oFCKeditor.Value = '@edit_page';
I think anyway :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I think I lied about the ${@array} :) but you don't need it anyway.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
read file into a scalar:

Code:
$edit_page = do {local $/; <LOG>};

then:

Code:
oFCKeditor.Value    = '$edit_page';

but the value of $edit_page might mess with the javascript, especially if there are any single quotes in it. You will have to manually escape any characters that mess up the javascript.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Okay thanks for your responses...

I tried the different methods and a line was still left :s.

What I have done now is use a normal Text Area box, and on page load the javascript recognizes it and converts it to the rich text area...

Works nicely

Thanks,

Chris
 
travs,

this will work to stringify the array in the scalar

$scalar = "@array";

this will not:

$scalar = @array;


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
for some reason I was thinking you could do ${@array} but that doesn't seem to work :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Chris if you really want to know what is going on then loop through the array and print each line and (maybe prepend it with a counter so you can see each line's index) then you can see if you are getting a stray new line or two. If your not chomping data normally then there is a chance that you are getting cascading new lines. If you read/write this file often you could just be making it longer and longer and that will come back to haunt you.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
He really does not want to chomp the array since he is ultimately editing a file. If the array were chomped all the newlines will be removed and the formatting of the file will be lost. He should read the file into a scalar and chomp the scalar, that way only the last newline is removed.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
hmm.. you could be right.. :D

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
travs and Kevin,

I'm going to have a go at looping the array to see where the problem arises. And also try chomping the scaler.

Thank you
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top