[29913] in Perl-Users-Digest
Perl-Users Digest, Issue: 1156 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 3 03:55:23 2008
Date: Thu, 3 Jan 2008 00:55:15 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 3 Jan 2008 Volume: 11 Number: 1156
Today's topics:
split file <sarma.nedunuri@gmail.com>
Re: split file <jurgenex@hotmail.com>
Re: split file <someone@example.com>
sprintf doesnt work in s///e syntax. raksha34@gmail.com
Re: sprintf doesnt work in s///e syntax. <thepoet_nospam@arcor.de>
Re: sprintf doesnt work in s///e syntax. raksha34@gmail.com
Re: sprintf doesnt work in s///e syntax. <thepoet_nospam@arcor.de>
Re: sprintf doesnt work in s///e syntax. <rvtol+news@isolution.nl>
Substitute <br> for newlines <user@example.net>
Re: Substitute <br> for newlines <chris@hyperspace.org.uk>
Re: Substitute <br> for newlines <jurgenex@hotmail.com>
Re: Substitute <br> for newlines <noreply@gunnar.cc>
Re: Substitute <br> for newlines <wheeledBobNOSPAM@yahoo.com>
Re: Substitute <br> for newlines <abigail@abigail.be>
Re: Substitute <br> for newlines <abigail@abigail.be>
Re: Testing File Existence - best way? <Juha.Laiho@iki.fi>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 28 Dec 2007 21:01:31 -0800 (PST)
From: Babu <sarma.nedunuri@gmail.com>
Subject: split file
Message-Id: <f4e42183-415e-4dc1-b9f1-d9f8874ce71d@y5g2000hsf.googlegroups.com>
Hi,
I am new to perl.I have basic question about splitting a file into 2.
the input file= temp.hex
contents
4A
5F
C3
3D
is there a one liner to split this file into 2 files temp1.hex
containing
4A
5F
and temp2.hex having
C3
3D
Thanks in advance
SN
------------------------------
Date: Sat, 29 Dec 2007 05:30:50 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: split file
Message-Id: <phmbn3lsckbmohnvtt0rvf6fa4gnf20gso@4ax.com>
On Babu <sarma.nedunuri@gmail.com> wrote:
>I am new to perl.I have basic question about splitting a file into 2.
>
>the input file= temp.hex
>
>contents
>
>4A
>5F
>C3
>3D
>
>is there a one liner to split this file into 2 files temp1.hex
>containing
>
>4A
>5F
>
>
>and temp2.hex having
>
>C3
>3D
Probably there is. But unless you tell us the criteria for _where_ to split
it's everyone's guess as what the algorithm is supposed to do.
- split after a given value 5F?
- split before a given value C3?
- split after 2 lines?
- split between given values 5F and C3?
- split one line after a given value 4A?
- ....
In any case, did you check the standard program split? Maybe it already does
what you are looking for.
jue
------------------------------
Date: Sat, 29 Dec 2007 06:37:27 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: split file
Message-Id: <HWldj.42757$UZ4.33084@edtnps89>
Babu wrote:
>
> I am new to perl.I have basic question about splitting a file into 2.
>
> the input file= temp.hex
>
> contents
>
> 4A
> 5F
> C3
> 3D
>
> is there a one liner to split this file into 2 files temp1.hex
> containing
>
> 4A
> 5F
>
>
> and temp2.hex having
>
> C3
> 3D
perl -e'$h = ( @x = <> ) / 2 + .5; open _ and print _ splice @x, 0, $h
for map ">$_", qw/temp1.hex temp2.hex/'
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
------------------------------
Date: Thu, 27 Dec 2007 00:22:11 -0800 (PST)
From: raksha34@gmail.com
Subject: sprintf doesnt work in s///e syntax.
Message-Id: <857a0556-a604-488d-b11a-db5adf28a680@x29g2000prg.googlegroups.com>
Hi,
perl -le '
### this part works
$var=sprintf "%d", 0x11;
print $var; ### and we get 17 output
$_ = "{0x11}";
#s/(0x\d+)/hex($1)/e; #### this part works ok and we see
a {17} output
s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
see a {0} output
print;
'
Is this some bug in the sprintf or s///e flag?
Thanks,
Rakesh
------------------------------
Date: Thu, 27 Dec 2007 11:09:04 +0100
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: sprintf doesnt work in s///e syntax.
Message-Id: <477379c0$0$17529$9b4e6d93@newsspool4.arcor-online.net>
raksha34@gmail.com wrote:
> Hi,
>
> perl -le '
> ### this part works
> $var=sprintf "%d", 0x11;
> print $var; ### and we get 17 output
>
> $_ = "{0x11}";
>
> #s/(0x\d+)/hex($1)/e; #### this part works ok and we see
> a {17} output
>
> s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
> see a {0} output
Try
s/0x(\d+)/sprintf("%d",oct($1))/e;
There is a difference between an unquoted mention of 0x11 and a
scalar containing a string of 0x11.
> print;
> '
>
> Is this some bug in the sprintf or s///e flag?
It's expected behaviour.
-Chris
------------------------------
Date: Thu, 27 Dec 2007 03:04:11 -0800 (PST)
From: raksha34@gmail.com
Subject: Re: sprintf doesnt work in s///e syntax.
Message-Id: <0e540610-b234-4c4a-b3f6-61e206f85973@i12g2000prf.googlegroups.com>
On Dec 27, 3:09 pm, Christian Winter <thepoet_nos...@arcor.de> wrote:
> raksh...@gmail.com wrote:
> > Hi,
>
> > perl -le '
> > ### this part works
> > $var=sprintf "%d", 0x11;
> > print $var; ### and we get 17 output
>
> > $_ = "{0x11}";
>
> > #s/(0x\d+)/hex($1)/e; #### this part works ok and we see
> > a {17} output
>
> > s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
> > see a {0} output
>
> Try
> s/0x(\d+)/sprintf("%d",oct($1))/e;
>
> There is a difference between an unquoted mention of 0x11 and a
> scalar containing a string of 0x11.
>
> > print;
> > '
>
> > Is this some bug in the sprintf or s///e flag?
>
> It's expected behaviour.
>
> -Chris
Thanks for the explanation Chris.
This quoted/unquoted interpretation is unique to hexadecimal/octal
numbers looks like.
Because, we dont see any problems if the variable is anything else.
But why should it
be like that?
I think you meant hex($1) instead of oct($1) in the s///e statement.
> s/0x(\d+)/sprintf("%d",oct($1))/e;
Regards,
--Rakesh
------------------------------
Date: Thu, 27 Dec 2007 15:22:02 +0100
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: sprintf doesnt work in s///e syntax.
Message-Id: <4773b512$0$16662$9b4e6d93@newsspool3.arcor-online.net>
raksha34@gmail.com wrote:
> On Dec 27, 3:09 pm, Christian Winter <thepoet_nos...@arcor.de> wrote:
> This quoted/unquoted interpretation is unique to hexadecimal/octal
> numbers looks like.
> Because, we dont see any problems if the variable is anything else.
> But why should it be like that?
Well, Perl tries to be as clever as can be without running
too much danger of misinterpreting a string for a number where
it isn't meant to be. The rule of thumb I use to avoid errors
is to convert numbers to base10 as early as possible in a script.
> I think you meant hex($1) instead of oct($1) in the s///e statement.
>> s/0x(\d+)/sprintf("%d",oct($1))/e;
I did. In fact, I managed (not for the first time) to confuse myself
while trying to decide which one to pick. Both hex("11") and oct("0x11")
do the correct thing (hex("0x11") does also). So either works fine:
s/(0x\d+)/sprintf("%d",oct($1))/e;
s/0x(\d+)/sprintf("%d",hex($1))/e;
-Chris
------------------------------
Date: Fri, 28 Dec 2007 00:17:15 +0100
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: sprintf doesnt work in s///e syntax.
Message-Id: <fl1fej.1g4.1@news.isolution.nl>
raksha34@gmail.com schreef:
> perl -le '
> ### this part works
> $var=sprintf "%d", 0x11;
> print $var; ### and we get 17 output
>
> $_ = "{0x11}";
>
> #s/(0x\d+)/hex($1)/e; #### this part works ok and we see
> a {17} output
>
> s/(0x\d+)/sprintf("%d",$1)/e; #### this part does NOT work and we
> see a {0} output
>
> print;
> '
Ask perl for help, by adding -w.
perl -Mstrict -we'
printf "%d\n", "0" . "\x11";
'
> Is this some bug in the sprintf or s///e flag?
No, PEBCAK.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Wed, 26 Dec 2007 16:37:06 -0500
From: monkeys paw <user@example.net>
Subject: Substitute <br> for newlines
Message-Id: <ZtWdncOct-wdVO_anZ2dnUVZ_gKdnZ2d@insightbb.com>
I need to display in HTML exactly the way a sentence is typed
by a user. i.e.
user types: a
line that
wraps
around
I need to put <br/> at the end of each line or it will display
all on one line in HTML. What is the perl substitution for this?
------------------------------
Date: Wed, 26 Dec 2007 21:50:51 +0000
From: Chris Eilbeck <chris@hyperspace.org.uk>
Subject: Re: Substitute <br> for newlines
Message-Id: <4772cca2$0$21097$da0feed9@news.zen.co.uk>
monkeys paw wrote:
> I need to display in HTML exactly the way a sentence is typed
> by a user. i.e.
>
> user types: a
> line that
> wraps
> around
>
> I need to put <br/> at the end of each line or it will display
> all on one line in HTML. What is the perl substitution for this?
I might be a little out of date but isn't that what the <pre> tags are for?
Chris
------------------------------
Date: Wed, 26 Dec 2007 21:55:49 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Substitute <br> for newlines
Message-Id: <cvi5n3t41i6kpatbith7tt80so8dcsomav@4ax.com>
monkeys paw <user@example.net> wrote:
>I need to display in HTML exactly the way a sentence is typed
>by a user. i.e.
Typically the <PRE> element in HTML would be used for this purpose.
>user types: a
>line that
>wraps
>around
>
>I need to put <br/> at the end of each line
> or it will display all on one line in HTML.
Although this has nothing to do with Perl: you are aware that echoing user
input as HTML is a major security hole? And that line wrapping is only one
minor of many changes that will happen?
> What is the perl substitution for this?
That depends upon how that text is being stored in Perl. Is each line an
entry in an array? Then you can just join() them using <br/> as the
separator.
jue
------------------------------
Date: Wed, 26 Dec 2007 22:58:39 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Substitute <br> for newlines
Message-Id: <5tg10pF1cliteU1@mid.individual.net>
monkeys paw wrote:
> I need to display in HTML exactly the way a sentence is typed
> by a user. i.e.
>
> user types: a
> line that
> wraps
> around
>
> I need to put <br/> at the end of each line or it will display
> all on one line in HTML. What is the perl substitution for this?
s/\r?\n|\r/<br \/>\n/g
But it may be better to use the <pre></pre> element.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 26 Dec 2007 22:22:02 GMT
From: still just me <wheeledBobNOSPAM@yahoo.com>
Subject: Re: Substitute <br> for newlines
Message-Id: <gtk5n350q48036f68l9f76f14d2hfdout9@4ax.com>
On Wed, 26 Dec 2007 22:58:39 +0100, Gunnar Hjalmarsson
<noreply@gunnar.cc> wrote:
> s/\r?\n|\r/<br \/>\n/g
>
>But it may be better to use the <pre></pre> element.
Or, to turn this around with the other poster's responses: You need to
learn more about regular expressions and matching/substitution,
because you should be removing any HTML that users input to you for
security reasons.
------------------------------
Date: 27 Dec 2007 13:34:02 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Substitute <br> for newlines
Message-Id: <slrnfn7aea.9lk.abigail@alexandra.abigail.be>
_
monkeys paw (user@example.net) wrote on VCCXXX September MCMXCIII in
<URL:news:ZtWdncOct-wdVO_anZ2dnUVZ_gKdnZ2d@insightbb.com>:
-: I need to display in HTML exactly the way a sentence is typed
-: by a user. i.e.
-:
-: user types: a
-: line that
-: wraps
-: around
-:
-: I need to put <br/> at the end of each line or it will display
-: all on one line in HTML. What is the perl substitution for this?
1) <br> isn't the way to go for this; it's <pre> you need.
2) <br/> ain't HTML.
3) s!\n!<br/>!g would replace any newline with the string '<br/>'.
Abigail
--
$_ = "\nrekcaH lreP rehtona tsuJ"; my $chop; $chop = sub {print chop; $chop};
$chop -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
-> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> () -> ()
------------------------------
Date: 27 Dec 2007 13:35:25 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Substitute <br> for newlines
Message-Id: <slrnfn7ags.9lk.abigail@alexandra.abigail.be>
_
Gunnar Hjalmarsson (noreply@gunnar.cc) wrote on VCCXXX September MCMXCIII
in <URL:news:5tg10pF1cliteU1@mid.individual.net>:
~~ monkeys paw wrote:
~~ > I need to display in HTML exactly the way a sentence is typed
~~ > by a user. i.e.
~~ >
~~ > user types: a
~~ > line that
~~ > wraps
~~ > around
~~ >
~~ > I need to put <br/> at the end of each line or it will display
~~ > all on one line in HTML. What is the perl substitution for this?
~~
~~ s/\r?\n|\r/<br \/>\n/g
~~
~~ But it may be better to use the <pre></pre> element.
If you want to replace generic newlines, I'd use:
s!\R!<br/>!g;
Abigail
--
sub camel (^#87=i@J&&&#]u'^^s]#'#={123{#}7890t[0.9]9@+*`"'***}A&&&}n2o}00}t324i;
h[{e **###{r{+P={**{e^^^#'#i@{r'^=^{l+{#}H***i[0.9]&@a5`"':&^;&^,*&^$43##@@####;
c}^^^&&&k}&&&}#=e*****[]}'r####'`=437*{#};::'1[0.9]2@43`"'*#==[[.{{],,,1278@#@);
print+((($llama=prototype'camel')=~y|+{#}$=^*&[0-9]i@:;`"',.| |d)&&$llama."\n");
------------------------------
Date: Thu, 27 Dec 2007 15:57:03 GMT
From: Juha Laiho <Juha.Laiho@iki.fi>
Subject: Re: Testing File Existence - best way?
Message-Id: <fl0hvk$fc3$1@ichaos2.ichaos-int>
"Jürgen Exner" <jurgenex@hotmail.com> said:
>still just me wrote:
>> I need to test if a file exists. Is there a better technique than
>> "attempt open and watch for an error"? The program will be a cgi
>> running in a windows or linux environment.
>
>Why not use the "file exists" operator? See
>
> perldoc -f -e
Commenting on an old thread here, but it depends on what the OP wants to
do with the file. Separate tests for file access permissions/file existence
may create possibilities for race conditions (if there's a possibility
that an existing file gets its permissions modified/gets removed
between the test and actual desired operation).
So, for most cases attempt to operate on the file in the desired way
(and handling arising error conditions) is the best test.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 1156
***************************************