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

Scaler Length

Status
Not open for further replies.

CoBu

Programmer
Jan 14, 2009
7
Hi all

I have a question regarding scaler lengths. I am opening a file and evaluating it line by line and writing it out to an 'outfile'. I have a problem because it is only taking the first 123 colums and writing it out when in fact it may be 214 colums long.

Can anybody suggest a way around this?
Thanks
Co.
 
Without knowing anything about your data or your code its pretty much impossible to know what the problem is. If you are using the length() function there is no limitation that I am aware of.

Post your code and some sample data if possible.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi
Sorry i found out what the problem was, what i am trying to do is to open a pcl file, find a certain string and replace it with a variable that is defined in the perl script. Some of the characters in the pcl file are treated like end of line or termination characters so it is not writing to the outfile correctly.

So what i would like to achieve is to open the pcl, search for the string replace it and close without altering any format etc.

Any ideas would be great,
Cheers
 
Maybe:

Code:
open (my $FILE, "<", "yourfile") of die "$!";
binmode $FILE; #<-- not sure this is necessary
my $var = do {local $/; <$FILE>};#<-- slurp whole file in scalar
close $FILE; 
$var =~ s/search_pattern/replacement_pattern/; 
open (my $FILE, ">", "yourfile") or die "$!";
print $FILE $var;
close $FILE;





------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi Kevin,

Thanks for that, it still outputs the data incorrectly. When evaluating the pcl file line by line it gets to a certain character and starts a new line. It will not paste here correctly so i can not show you what i mean.

I was searching there to see if there is an option to just find replace & save without having to print it back out to the file, this is where it is going wrong.



 
I have now identified that the line that reads in the file and sets it to a variable is reading the line that contains line breaks in it and is carry this out.

so in stead of having one line with line break characters in it, it now goes onto a second line.

$var = do {local $/; <$FILE>};

Is there any command to ignore the line breaks found and just deal with the file line by line.

Chomp does not achieve this for me either.
Thanks.
 
In the code I posted you can try this:

$var =~ s/search_pattern/replacement_pattern/s;

note the "s" on the end, see if that helps.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Never mind, my provious suggestion will not help. You can try Tie::File to edit the file inplace, not sure it will work with your file though.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
If you do this:

{
local(*INPUT, $/); # undefs the record sep char $/
open (INPUT, $file) || die "can't open $file: $!";
$var = <INPUT>;
}

You'll read the entire contents of the file into the variable $var - and never mind what end-of-line chars are in there.

Now then... When you say "Is there any command to ignore the line breaks found and just deal with the file line by line." What starts a new line or marks the end of the existing line in your file?

Mike

 
By the way... (edited from the Perl Docs)

Perl Documentation said:
$/ --> The input record separator, newline by default. This influences Perl's idea of what a "line" is. You may set it to a multi-character string to match a multi-character terminator, or to undef to read through the end of file. Setting it to "\n\n" means something slightly different than setting to "" , if the file contains consecutive empty lines. Setting to "" will treat two or more consecutive empty lines as a single empty line....

Have a look at
Mike

 
Mike the only thing is that in the pcl file it does not appear as '/n' for a new line, it appears as a couple of boxes.

When the file is opened and asigned to $var it reads it in with the new line chars taken into effect.

Thanks.
Co
 
CoBu said:
it appears as a couple of boxes
How? On screen, in your editor, or what?

Is it possible that your file is created on Unix, or a Mac, and you are trying to read it in Windows (or some other combination)? What constitutes end of line differs on these systems. Perl 'knows' what system it is running on and acts accordingly, but if you supply files created on other systems you have to code around it yourself.

If you can isolate the bytes singly, you can print out the decimal values with ord

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top