Re: s/// question
by bart (Canon) on Oct 02, 2003 at 11:29 UTC
|
Whatever you do, don't use binmode on your textfile handle. You'd prevent conversion of "\n" to the proper line ending for your platform, in this case CR+LF, and which might make notepad fail to see a it, as it'd still be a bare LF.
Oh, one more idea. Could it be that your file contains extra "\r" characters? There shouldn't be any at all. You see, notepad, and anything else using the same textbox control, use CR+CR+LF as a flag for soft wrap. Change anything in your text and they all disappear. That's why it's possible to download a text file from the internet, a HTML file for example, try to edit one tiny thing, and end up with just one huge line for the whole file. Downloaded HTML files often contain two CR characters per LF, and notepad strips them all.
This snippet can delete them all for you, if present:
tr/\r//d;
Like I said, without binmode, Perl will convert the normal "\n" characters to the proper CR+LF combinations. Start with CR+LF, and you'll get CR+CR+LF. | [reply] [d/l] |
Re: s/// question
by dbwiz (Curate) on Oct 02, 2003 at 11:29 UTC
|
$regel = "Username; abcd; Username";
$regel =~ s/Username/\nGebruikersnaam/sgm;
print "(BEGIN)$regel(END)\n";
__END__
output:
(BEGIN) # <-- \n here
Gebruikersnaam; abcd; # <-- and \n here
Gebruikersnaam(END)
Maybe the problem is in your output routine. Are you producing HTML? If so, a "\n" doesn't have any effect, unless inside <pre> </pre> tags.
| [reply] [d/l] |
Re: s/// question
by jonadab (Parson) on Oct 02, 2003 at 11:42 UTC
|
$regel =~ s/Username/\nGebruikersnaam/sgm; </BR>
Do you have a filehandle open named /BR? (Is it even
legal in Perl to start a filehandle name with a slash?
I've *got* to remember that for future obfuscations...)
Also, why are you reading from the filehandle in void
context?
All the "Username" 's are changed in
"Gebruikersnaam" 's but the "\n" has no effect
and is not visible. So it remains one big line.
Where is the output being displayed? In a Windows
command prompt box? Is that Win9x/Me's command.com,
or the cmd.exe that is used in NT? Or is the output
being sent to a web browser, and you forgot to mention
that? (That would *sort-of* explain the
</BR>, though not fully...)
If the output is going to a web browser, then
it is normal for newlines to have no effect. Or maybe
the output is being sent to a file, which you're opening
and looking at in notepad? (That would explain your
(otherwise very odd) remark about Notepad not
recognising the \r.) You need to tell us where it
is that you're looking at this output and seeing it
all on one line.
I have tried "\r", "\r\n" and "\n\r" but the "\r" is not recognized by notepad.
Usually, if you're not using binmode, Perl on Win32 will
normally
automagically insert a proper CRLF pair when you use
\n. However, this isn't fully portable; if someone
later runs the code on Unix, they'll get just linefeeds.
Then again, that is portable, because on Unix linefeeds
are what is normally wanted. Unless you're generating
a file that will be transported across platforms
without undergoing translation. Anyway, it isn't a
very common thing, but if for some reason you do have
the occasion to need to expressly specifiy a CRLF as
such when you'd otherwise get just a linefeed, \r\n is
the way to do it. But I very much doubt that's your
problem.
If you're outputting to a web browser, try it this way:
$regel =~ s/Username/<div>Gebruikersnaam<\/div>/sgm;
Otherwise, you're going to have to tell us more about
where your output is going.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
| [reply] [d/l] [select] |
|
|
Hallo,
I'm not writing HTML. Just plain old ascii :-)
Thanks for the effort :-)
Convex
| [reply] |
Re: s/// question
by inman (Curate) on Oct 02, 2003 at 11:54 UTC
|
The code that you posted appears to work fine on Windows 2000 with Activestate 5.6.1.
If you are losing carriage returns, check that they have not been chomped from string contained in $regel.
If you are generating HTML then you will need to change the substitution to add the <br> tag e.g.
$regel =~ s/Username/<br>\nGebruikersnaam/sgm;
#! /usr/bin/perl -w
use strict;
use warnings;
undef $/;
my $regel = <DATA>;
$regel =~ s/Username/\nGebruikersnaam/sgm;
print "start\n";
print "$regel";
print "end\n";
__DATA__
Username=Joe Smith
Username=Jane Doe
Gives you:
start
Gebruikersnaam=Joe Smith
Gebruikersnaam=Jane Doe
end
Inman | [reply] [d/l] |
Re: s/// question
by matsmats (Monk) on Oct 02, 2003 at 11:27 UTC
|
I can't recreate your problem, I get newlines on both win and linux here. But perhaps you could work around it by using $/ - the input record separator instead of specifying \n. Like this:
$regel =~ s[Username][$/Gebruikersnaam]sgm;
Mats
| [reply] [d/l] |
Re: s/// question
by nylon (Acolyte) on Oct 02, 2003 at 11:59 UTC
|
More information:
1) The HTML tag (break) is not part of the code (typing error, sorry about that)
2) The output is printed to a filehandel (output.txt file)
3) This file is opened in the windows notepad.
Thx,
Nylon
| [reply] |
|
|
Ok, guy's I found it. It was not the regex but an array slip-up.
I send the string to an array and there it was glue together again. Silly me. ;-)
Let this be a lesson to me: The mistakes are not alway's were you assume they are + long live the "perl -d".
Thanks for all the help (I learned something) and sorry for the trouble.
Nylon
| [reply] |
Re: s/// question
by nylon (Acolyte) on Oct 02, 2003 at 11:51 UTC
|
I'm just using ultraedit-32, ActiveState Perl
and windows notepad. Like I said: " I do not get it".
Could the fact that I have to use
undef $/;
$regel = <FH_input>
Have an influence?
Thanks for the support untill now :-) :-)
Nylon | [reply] [d/l] |
|
|
undef $/;
$regel = <FH_input>
All this does is read the entire contents of your input
file into $regel in one fell swoop. It should still
have the newlines in it that it had originally, and
in any case how you do your input shouldn't have any
impact on newlines that you explicitely insert in
your output. I'd say that's not your problem. Maybe
you should post the whole script here.
Maybe Notepad is part of the issue. What happens if
you look at the file in another editor (UltraEdit or
whatever, or even EDIT.COM) or pull up a command prompt and use the
TYPE command to display it?
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}}
split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
| [reply] [d/l] [select] |