[25646] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 7888 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 16 14:10:22 2005

Date: Wed, 16 Mar 2005 11:10: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           Wed, 16 Mar 2005     Volume: 10 Number: 7888

Today's topics:
        Probably a dumb s/// question. <die@spammer.die>
    Re: Probably a dumb s/// question. <mritty@gmail.com>
    Re: Probably a dumb s/// question. <xx087@freenet.carleton.ca>
    Re: Probably a dumb s/// question. <tzz@lifelogs.com>
    Re: Probably a dumb s/// question. <tzz@lifelogs.com>
    Re: Probably a dumb s/// question. <noreply@gunnar.cc>
    Re: Probably a dumb s/// question. <tzz@lifelogs.com>
    Re: problem writing to stdin of child process (john)
    Re: Random String Generator <dummymb@hotmail.com>
    Re: Random String Generator <1usa@llenroc.ude.invalid>
    Re: Random String Generator <richard@zync.co.uk>
    Re: regular expression help with apostrophe <wyzelli@yahoo.com>
    Re: regular expression help with apostrophe <tzz@lifelogs.com>
    Re: regular expression help with apostrophe <mritty@gmail.com>
    Re: regular expression help with apostrophe <tzz@lifelogs.com>
    Re: unicode study with unicodedata module <xah@xahlee.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 16 Mar 2005 16:14:48 GMT
From: Mark Healey <die@spammer.die>
Subject: Probably a dumb s/// question.
Message-Id: <pan.2005.03.16.16.17.19.236667@spammer.die>

I'm trying to craft a search that capitalizes letters depending on their
context, specifically after a space or the beginning of a string.

For example I'd like to turn

the quick brown fox jumped over the lazy dogs.

to 

The Quick Brown Fox Jumped Over the Lazy Dogs.

Is this doable on a single line?


-- 
Mark Healey
marknews(at)healeyonline(dot)com



------------------------------

Date: Wed, 16 Mar 2005 16:27:13 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Probably a dumb s/// question.
Message-Id: <B7ZZd.4834$db6.3238@trndny02>

"Mark Healey" <die@spammer.die> wrote in message
news:pan.2005.03.16.16.17.19.236667@spammer.die...
> I'm trying to craft a search that capitalizes letters depending on
their
> context, specifically after a space or the beginning of a string.
>
> For example I'd like to turn
>
> the quick brown fox jumped over the lazy dogs.
>
> to
>
> The Quick Brown Fox Jumped Over the Lazy Dogs.
>
> Is this doable on a single line?

What have you tried so far?

Have you read the posting guidelines for this group, posted twice a
week?

Because I'm feeling generous (and bored) anyway:

s/(^|\s)([a-z])/$1\u$2/g;


for more information on ^, |, (), $1 & $2:
perldoc perlre
perldoc perlretut
perldoc perlreref

for more information on \u:
perldoc -f ucfirst

Paul Lalli



------------------------------

Date: 16 Mar 2005 16:51:18 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Probably a dumb s/// question.
Message-Id: <slrnd3gp05.h9m.xx087@smeagol.ncf.ca>

At 2005-03-16 11:14AM, Mark Healey <die@spammer.die> wrote:
>  For example I'd like to turn
>  the quick brown fox jumped over the lazy dogs.
>  to 
>  The Quick Brown Fox Jumped Over the Lazy Dogs.
>  
>  Is this doable on a single line?

    my $string = 'the quick brown fox jumped over the lazy dogs.';
    my $String = join ' ', map {ucfirst lc} split ' ', $string;

That forces your string to lower case first then capitalizes the first
letter.  It won't preserve whitespace though.

-- 
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca


------------------------------

Date: Wed, 16 Mar 2005 13:19:54 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Probably a dumb s/// question.
Message-Id: <4nbr9jmp11.fsf@lifelogs.com>

On Wed, 16 Mar 2005, die@spammer.die wrote:

> I'm trying to craft a search that capitalizes letters depending on their
> context, specifically after a space or the beginning of a string.
> 
> For example I'd like to turn
> 
> the quick brown fox jumped over the lazy dogs.
> 
> to 
> 
> The Quick Brown Fox Jumped Over the Lazy Dogs.
> 
> Is this doable on a single line?

Generally no, because of strange combinations like "Dog+Cat" or
"yes/no".  There is a tool that will do it, but the internals are much
more than a single line :)

http://search.cpan.org/~doom/Text-Capitalize-0.4/Capitalize.pm

Always check CPAN first.

Ted


------------------------------

Date: Wed, 16 Mar 2005 13:22:35 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Probably a dumb s/// question.
Message-Id: <4n7jk7mowk.fsf@lifelogs.com>

On 16 Mar 2005, xx087@freenet.carleton.ca wrote:

>     my $string = 'the quick brown fox jumped over the lazy dogs.';
>     my $String = join ' ', map {ucfirst lc} split ' ', $string;
> 
> That forces your string to lower case first then capitalizes the first
> letter.  It won't preserve whitespace though.

It's probably better to do something like this:

perl -p -e's/(\w+)/ucfirst($1)/eg'

Text::Capitalize is even better, but the above will be closer to what
the OP wanted I think.

HTH
Ted


------------------------------

Date: Wed, 16 Mar 2005 19:22:19 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Probably a dumb s/// question.
Message-Id: <39rc1pF64sktgU1@individual.net>

Mark Healey wrote:
> I'm trying to craft a search that capitalizes letters depending on their
> context, specifically after a space or the beginning of a string.
> 
> For example I'd like to turn
> 
> the quick brown fox jumped over the lazy dogs.
> 
> to 
> 
> The Quick Brown Fox Jumped Over the Lazy Dogs.
----------------------------------^

What determines that "the" is *not* converted to "The"?

> Is this doable on a single line?

If it is, I suppose it would be a *very* long line. :)

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Wed, 16 Mar 2005 13:30:40 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Probably a dumb s/// question.
Message-Id: <4ny8cnl9yn.fsf@lifelogs.com>

On Wed, 16 Mar 2005, mritty@gmail.com wrote:

> s/(^|\s)([a-z])/$1\u$2/g;

I always find it better to work with Perl built-ins such as ucfirst
and \w, which respect locale and know about Unicode uppercasing rules.
Any time I see a range like [a-z] or [A-Za-z] I try to reduce it to at
least a POSIX class like [:alpha:] unless I must only accept [a-z].

Ted


------------------------------

Date: 16 Mar 2005 01:40:26 -0800
From: lqueryvg@gmail.com (john)
Subject: Re: problem writing to stdin of child process
Message-Id: <9190653e.0503160140.7419766b@posting.google.com>

Big and Blue <No_4@dsl.pipex.com> wrote in message news:<V--dnX1WaaAa16rfRVnygg@pipex.net>...
> john wrote:
> > 
> > Sorry, I spoke too soon. I'm still left with my original problem of
> > how to attach the pipe to STDIN of the child. The various approaches
> > I've tried either raise an invalid argument error or the child appears
> > to read the number of the file descriptor rather than the data.
> 
>     Hav a look at the documentation for open.  It gives (or gave...) an 
> example of saving STDOUT and STDERR, opening somethign else on them and 
> then restoring the original.
> 
>     Rearrange as appropriate for STDIN.

Once again, I may be speaking too soon...
It's not obvious from the perdoc, but the following seems to work with
the IO::Pipe version of the testcase...

  my $fd = fileno($pipe);
  open(STDIN, "<&$fd") or die "STDIN open: $!";

I'll plug this back into my original program and see if it keeps
working :-)
Thanks for your help.

Here's the modified code...

#!/usr/bin/perl
use strict;
use warnings;

use IO::Pipe;

my $pipe = new IO::Pipe or die 'pipe';

my $pid = fork();
if ($pid > 0) {

  # Parent
  $pipe->writer();

  print $pipe "hello!\n";       # to the child
  close($pipe);

} else {
  # Child

  # Connect stdin to pipe

  $pipe->reader();

  my $fd = fileno($pipe);
  open(STDIN, "<&$fd") or die "STDIN open: $!";

  my $line = <STDIN>;   # from the pipe ?
  chomp $line;
  print "child received: ($line)\n";
  close($pipe);
  exit(0);
}

wait;


------------------------------

Date: 16 Mar 2005 06:58:30 -0800
From: "DMB" <dummymb@hotmail.com>
Subject: Re: Random String Generator
Message-Id: <1110985110.078483.177460@o13g2000cwo.googlegroups.com>

Now that's cool!  Thanks.



------------------------------

Date: 16 Mar 2005 15:32:41 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Random String Generator
Message-Id: <Xns961B6B441B71Basu1cornelledu@132.236.56.8>

"DMB" <dummymb@hotmail.com> wrote in news:1110985110.078483.177460
@o13g2000cwo.googlegroups.com:

> Now that's cool!  Thanks.

What is cool?

Please provide context when you post.

Please do read the posting guidelines for this group.

Sinan


------------------------------

Date: Wed, 16 Mar 2005 16:08:14 +0000
From: Richard Gration <richard@zync.co.uk>
Subject: Re: Random String Generator
Message-Id: <pan.2005.03.16.16.08.02.406574@zync.co.uk>

On Tue, 15 Mar 2005 12:53:26 -0800, DMB wrote:

> I need to write a Perl function that returns a 32 character randomly
> generated string of characters.

This thread has been done to death now, but I think this solution is
rather elegant. Needless to say I did not invent it. I originally found it
in Wing, the webmail app, but I don't know if it originates with Wing's
author ...

# It's important that there are 64 chars in this array ...
my @session_chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9, '.', '-');
my $raw_rand;
my $length = 32;
open(RANDOM,"/dev/urandom") or die "Couldn't open /dev/urandom: $!";
FORKED: {
	redo FORKED if (read(RANDOM,$raw_rand,$length) != $length);
	$raw_rand =~ s/(.)/$session_chars[ord($1) & 63]/esg;
}
close (RANDOM);

Rich


------------------------------

Date: Wed, 16 Mar 2005 13:05:18 GMT
From: "Peter Wyzl" <wyzelli@yahoo.com>
Subject: Re: regular expression help with apostrophe
Message-Id: <iaWZd.639$C7.294@news-server.bigpond.net.au>

"phaylon" <phaylon@dunkelheit.at> wrote in message 
news:pan.2005.03.15.12.38.01.684058@dunkelheit.at...
: Peter Wyzl wrote:
:
: > That is like asking "What is a /usr/var directory?"
:
: Wouldn't say "/usr/var" is a "fully qualified" pathname. "Local Settings
: Folder" is not.

What I was getting at is that 'Local Settings Folder' is a well known and 
expected default storage location on a Windows system.  Likewise /usr/var is 
a well known and expected default storage location on a *nix system.

There are many differences, not least being the semantic one you pointed 
out, but that is not what I was getting at.

P 




------------------------------

Date: Wed, 16 Mar 2005 11:16:32 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: regular expression help with apostrophe
Message-Id: <4n7jk7o9b3.fsf@lifelogs.com>

On Tue, 15 Mar 2005, hendrik_maryns@despammed.com wrote:

> Ok, this works now, but I still don't see the whole point:
> <quote>
>>Regular expressions do not "yield" characters.
>>>
>>> They either match (succeed), or they don't match (fail).
> 
> 
> This is not always true, e.g.
> 
> perl -n -e '@all = m/(.)/g; print @all'
> 
> to give a simple example of a regex that returns characters.
> </quote>
> 
> This isn't a regex that returns characters, it are the extended Perl
> memory functions concerning regexes that make this possible (i.e. (.) 
> returning a value, $1), but a regex 'an sich' indeed only matches or 
> doesn't.  (One could also see a regex as a description of a certain set 
> of strings).

We are discussing Perl, so yes, *Perl* regexes return values.  It's
not something external to them, it's a feature of the capturing
mechanism in the regex engine.  You may be thinking of the computer
science definition of regexes as FSMs - Perl goes far beyond that.

This example does not have anything to do with the $1...$9 variables.
There are only 9 of them, while the example above will match and
return any number of characters into @all.

I have used this feature in real code many times.  For example, to
extract data from a format like this:

$text = "Field1 = Value1
Field2 = Value2
Field3 = 
Field4 = Value4";

you can use a regex like this:

my %results = ($text =~ m/^(.*?)\s*=\s*?(.*?)$/gm);

and this populates %results with the right key/value pairs, no matter
how many there are.

Hope that helps...
Ted


------------------------------

Date: Wed, 16 Mar 2005 16:31:19 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: regular expression help with apostrophe
Message-Id: <rbZZd.5414$aS5.4899@trndny05>


"Ted Zlatanov" <tzz@lifelogs.com> wrote in message
news:4n7jk7o9b3.fsf@lifelogs.com...
> This example does not have anything to do with the $1...$9 variables.
> There are only 9 of them,

Who on earth told you that?

#!/usr/bin/perl
use strict;
no strict 'refs';
use warnings;

$_ = join '', 'a'..'z';
/(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.
)(.)(.)/;
for (1..26){
  print "${$_}\n";
}
__END__


Paul Lalli



------------------------------

Date: Wed, 16 Mar 2005 11:40:12 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: regular expression help with apostrophe
Message-Id: <4nsm2vmtn7.fsf@lifelogs.com>

On Wed, 16 Mar 2005, mritty@gmail.com wrote:

> "Ted Zlatanov" <tzz@lifelogs.com> wrote in message
> news:4n7jk7o9b3.fsf@lifelogs.com...
>> This example does not have anything to do with the $1...$9 variables.
>> There are only 9 of them,
> 
> Who on earth told you that?

In my defense, there ARE only 9 of the $1 ... $9 variables :)

But yes, my mistake.  Sorry.  It was too early in the morning.

Ted


------------------------------

Date: 16 Mar 2005 02:02:35 -0800
From: "Xah Lee" <xah@xahlee.org>
Subject: Re: unicode study with unicodedata module
Message-Id: <1110967355.019016.159190@z14g2000cwz.googlegroups.com>

here's a snippet of code that prints a range of unicode chars, along
with their ordinal in hex, and name.

chars without a name are skipped. (some of such are undefined code
points.)

On Microsoft Windows the encoding might need to be changed to utf-16.

Change the range to see different unicode chars.

# -*- coding: utf-8 -*-

from unicodedata import *

l=[]
for i in range(0x0000, 0x0fff):
    l.append(eval('u"\\u%04x"' % i))

for x in l:
    if name(x,'-')!='-':
        print x.encode('utf-8'),'|', "%04x"%(ord(x)), '|', name(x,'-')
--
http://xahlee.org/perl-python/unicodedata_module.html

anyone wants to supply a Perl version?

 Xah
 xah@xahlee.org
 http://xahlee.org/PageTwo_dir/more.html



Brian McCauley wrote:
> Xah Lee wrote:
>
> > i don't know what's the state of Perl's unicode.
> 
> perldoc perlunicode



------------------------------

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 V10 Issue 7888
***************************************


home help back first fref pref prev next nref lref last post