[22086] in Perl-Users-Digest
Perl-Users Digest, Issue: 4308 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Dec 24 00:05:38 2002
Date: Mon, 23 Dec 2002 21:05:08 -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 Mon, 23 Dec 2002 Volume: 10 Number: 4308
Today's topics:
Re: File::Find module ; want to resume search from poin (Mitchell Laks)
Re: I can't remove the \n please help me! <mbudash@sonic.net>
Re: I can't remove the \n please help me! <usenet@dwall.fastmail.fm>
Re: I can't remove the \n please help me! (Mala Ananthamurthy)
Re: Need help with octal numbers in substitution <No_Mail_Address@cox.net>
Perl Vs PHP <kasp@NO_SPAMepatra.com>
Re: Perl Vs PHP <uri@stemsystems.com>
Processing \0xx in nonliteral strings <bramoshe@runslinux.net>
Re: Processing \0xx in nonliteral strings <ahamm@mail.com>
Re: Processing \0xx in nonliteral strings <bramoshe@runslinux.net>
Re: Processing \0xx in nonliteral strings <uri@stemsystems.com>
Re: Processing \0xx in nonliteral strings <ahamm@mail.com>
Re: Processing \0xx in nonliteral strings <ahamm@mail.com>
Re: Processing \0xx in nonliteral strings <ahamm@mail.com>
Re: Processing \0xx in nonliteral strings <bramoshe@runslinux.net>
Re: Processing \0xx in nonliteral strings <bramoshe@runslinux.net>
Re: Processing \0xx in nonliteral strings <ahamm@mail.com>
Re: Processing \0xx in nonliteral strings <uri@stemsystems.com>
Re: Processing \0xx in nonliteral strings <ahamm@mail.com>
Re: Processing \0xx in nonliteral strings <uri@stemsystems.com>
Re: regular expression help <Jodyman@hotmail.com>
Simple array problem (Stephen Adam)
Re: Simple array problem <ahamm@mail.com>
Re: Simple array problem <ahamm@mail.com>
Re: Tied Filehandles <ahamm@mail.com>
Re: using Perl inside directories and extensions questi <mgjv@tradingpost.com.au>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 23 Dec 2002 15:39:29 -0800
From: mlaks2000@yahoo.com (Mitchell Laks)
Subject: Re: File::Find module ; want to resume search from point of failure.
Message-Id: <ab3b13db.0212231539.7eeb9236@posting.google.com>
(Anno Siegel) wrote in message news:<au6s8j$55d$1@mamenchi.zrz.TU-Berlin.DE>...
> According to Mitchell Laks <mlaks2000@yahoo.com>:
> > Hi -
> > Thanks millions - i guess i want to be able to say is
> >
> > do the file::find algorithm on a directory tree - but begin at
> > specified file xyz in the tree... instead of the beginning...
>
> I don't think you'll find such a feature in File::Find. In general,
> re-starting a process over a file system in this way doesn't make
> much sense, because a live file system changes permanently.
You are right! however I have frozen the directory from new additions,
and am trying to export/convert all the individual files at the
branches of the tree via my "wanted" function to a second machine. I
want to resume the File::find algorithm mechanism from the crash point
in the directory hierarchy, whereever it is - which i can determine by
looking at the target - find out what file came over last.
>
> For a practical approach, I'd consider just printing to a log file
> all directory names File::Find comes across:
>
> print LOG "$File::Find::name\n" if -d;
>
> Then start the thing and hope for the best. If it indeed crashes,
> evaluate the log to see which directories have been finished and
> set up another run that doesn't repeat too much.
what algorithm does file::find use to order the directories and
subdirectories
i guess i can make a array of the remaining directories and feed that
to file::find. i guess file find must use lexicographic order. i am
looking at the code in file/find.pm. :( its a little over my (learning
perl level) head...
>
> The latter step could certainly be automated, but if this is a one-time
> (or rare) thing, I wouldn't bother.
i have 10 machines with about 300gb of data in 500,000 files each.
sort of in between (rare and not rare). :).
thanks
mitchell
>
> Anno
------------------------------
Date: Tue, 24 Dec 2002 00:05:57 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: I can't remove the \n please help me!
Message-Id: <mbudash-939BD3.16055823122002@typhoon.sonic.net>
In article <Xns92ED84EF9882dkwwashere@216.168.3.30>,
"David K. Wall" <usenet@dwall.fastmail.fm> wrote:
> Michael Budash <mbudash@sonic.net> wrote on 23 Dec 2002:
>
> > instead of this:
> >
> >> $line =~ s/(.*?):9999//;
> >> chomp($line);
> >
> > try this:
> >
> > next if ($line =~ /:9999$/);
>
> That's what the OP said was desired, but the examples seem to indicate
> that ':9999' should be removed from the end of any string in which it
> occurs.
>
> $line =~ s/:9999$//;
>
> English doesn't appear to be the OP's native language, so some
> vagueness is understandable... and I might have misunderstood.
david, you've contradicted me so nicely, i almost hate to reply!
however, your example will still modify the line (nearly the same as the
op's regex) then write the remainder (nothing) plus a linefeed to the
file. mine will skip the line altogether (writing nothing to the file),
which is by his example what he wanted...
but then, I might have misunderstood as well! 8^)
------------------------------
Date: Tue, 24 Dec 2002 01:48:53 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: I can't remove the \n please help me!
Message-Id: <Xns92EDD3BC4F9E0dkwwashere@216.168.3.30>
Michael Budash <mbudash@sonic.net> wrote:
> In article <Xns92ED84EF9882dkwwashere@216.168.3.30>,
> "David K. Wall" <usenet@dwall.fastmail.fm> wrote:
>
>> Michael Budash <mbudash@sonic.net> wrote on 23 Dec 2002:
>>
>> > instead of this:
>> >
>> >> $line =~ s/(.*?):9999//;
>> >> chomp($line);
>> >
>> > try this:
>> >
>> > next if ($line =~ /:9999$/);
>>
>> That's what the OP said was desired, but the examples seem to
>> indicate that ':9999' should be removed from the end of any
>> string in which it occurs.
>>
>> $line =~ s/:9999$//;
>>
>> English doesn't appear to be the OP's native language, so some
>> vagueness is understandable... and I might have misunderstood.
>
> david, you've contradicted me so nicely, i almost hate to reply!
Aww, go for it.
> however, your example will still modify the line (nearly the same
> as the op's regex) then write the remainder (nothing) plus a
Nothing? All I'm doing is removing ':9999' from the end of a line.
The preceding portion of the line remains unaltered.
> linefeed to the file. mine will skip the line altogether (writing
> nothing to the file), which is by his example what he wanted...
>
> but then, I might have misunderstood as well! 8^)
Obviously I think you have misunderstood :-), but let me state more
explicitly what I meant, which was something like this:
my @lines = <DATA>; # slurp...
foreach my $line (@lines) {
$line =~ s/:9999$//;
print $line;
}
IIRC the OP chomp()ed each line, then said 'print $line, "\n";',
which to me is pointless; why chomp off the newline if you're just
going to print out another one? I wrote a couple of followups, and
forgot to mention this in the one I actually posted....
Whatever. The OP can pull out whatever fits from this thread. If
I'm wrong, well, it won't be the first time.
--
David Wall - me@dwall.fastmail.fm
"Oook."
------------------------------
Date: 23 Dec 2002 19:16:53 -0800
From: mala_mukund@yahoo.com (Mala Ananthamurthy)
Subject: Re: I can't remove the \n please help me!
Message-Id: <11729251.0212231916.56abf062@posting.google.com>
> My source :
>
> $file = "Developpement.ldif";
>
> open (BEBE, $file) || die "can't open bebe: $!";
>
> @lines = <BEBE>;
>
> close(BEBE);
>
> open (BEBE,'>Developpement.ldif')|| die " can't open newbebe: $!";
>
>
> foreach $line (@lines){
>
#$line =~ s/(.*?):9999//;
#chomp($line);
#print BEBE ($line,"\n");
next if($line =~ s/(.*?):9999$//);
print BEBE ($line);
>
> }
>
> close (BEBE);
>
> Many thanks, it's very urgent.
------------------------------
Date: Tue, 24 Dec 2002 00:10:51 GMT
From: Fred <No_Mail_Address@cox.net>
Subject: Re: Need help with octal numbers in substitution
Message-Id: <3E07A671.13CE763E@cox.net>
Anno Siegel wrote:
>
> According to Fred <No_Mail_Address@cox.net>:
> > Hello there,
> >
> > I want to remove all 8-bit characters from a file, but keep ü ä ö
> > (decimal 129, 132, 148)
>
> tr/üäö/ /cs;
>
> Anno
Thank you Anno, it works fine. I use it in:
while (<IN>) {
if (m/[\200-\377]+?/g) {
tr/üäö/ /cs ;
}
print OUT $_ ;
}
---
Fred
------------------------------
Date: Tue, 24 Dec 2002 09:08:57 +0530
From: "Kasp" <kasp@NO_SPAMepatra.com>
Subject: Perl Vs PHP
Message-Id: <au8ksl$92h$1@newsreader.mailgate.org>
I would like to know your opinion on whether Perl or PHP is better?
Lately, more people are looking into PHP. Why?
------------------------------
Date: Tue, 24 Dec 2002 04:39:03 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Perl Vs PHP
Message-Id: <x7ptrsav4p.fsf@mail.sysarch.com>
>>>>> "K" == Kasp <kasp@NO_SPAMepatra.com> writes:
K> I would like to know your opinion on whether Perl or PHP is better?
K> Lately, more people are looking into PHP. Why?
because they think the web is the entire net out there. which isn't
true if you didn't know that. most perl is not used for web stuff. php
is never used outside the web. perl is a general purpose and powerful
programmin language. php is a specialize web templating system with some
ideas poorly borrowed from perl.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 24 Dec 2002 01:30:57 +0000 (UTC)
From: Moshe Jacobson <bramoshe@runslinux.net>
Subject: Processing \0xx in nonliteral strings
Message-Id: <au8dch$r89$1@news-int.gatech.edu>
Hello all, I hope you can answer this somewhat difficult question:
I'm trying to take a user-provided format string, and provide an
output string with various %-macros replaced with application-specific
data, and also with \n, \r, \0xx, etc backslash escapes expanded as
well.
Currently, I have the following code:
my $fmt = "user: %u\ndomain:%d\n"; # example
my $str = "";
my @chars = split "", $fmt;
for(my $i = 0; $i < @chars; $i++) {
if($chars[$i] ne "%" and $chars[$i] ne "\\") { $str .= $chars[$i]; next; }
$i++;
if($i == @chars) { $str .= "$_"; last; }
if($chars[$i-1] eq "\\") {
SWITCH: for ($chars[$i]) {
/t/ && do { $str .= "\t"; last SWITCH; };
/n/ && do { $str .= "\n"; last SWITCH; };
/r/ && do { $str .= "\r"; last SWITCH; };
/f/ && do { $str .= "\f"; last SWITCH; };
/b/ && do { $str .= "\b"; last SWITCH; };
/a/ && do { $str .= "\a"; last SWITCH; };
/./ && do { $str .= "$_"; last SWITCH; };
}
} else {
SWITCH: for ($chars[$i]) {
/u/ && do { $str .= $usr; last SWITCH; };
/d/ && do { $str .= $dom; last SWITCH; };
.
.
.
/./ && do {
$str .= "\%$_";
};
}
}
}
The problem with this is that I cannot use this method to expand \0xx
(octal value) escape sequences, because I cannot express every
possible value as a literal for the perl preprocessor to expand for me
(as I'm doing in the top there).
How can I take a string provided to me in a variable and expand all
backslash-escaped character sequences as if I were the perl
preprocessor?
Thanks,
Moshe
--
*** SPAM BLOCK: Remove bra before replying! ***
Moshe Jacobson :: http://runslinux.net :: moshe at runslinux dot net
!! FBI Arreseting Programmers? http://www.freesklyarov.org
!! Trust Microsoft Anti-Trust: http://www.anti-dmca.org
------------------------------
Date: Tue, 24 Dec 2002 12:46:13 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8eap$5b8qp$1@ID-79573.news.dfncis.de>
Moshe Jacobson wrote:
> Hello all, I hope you can answer this somewhat difficult question:
>
> I'm trying to take a user-provided format string, and provide an
> output string with various %-macros replaced with application-specific
> data, and also with \n, \r, \0xx, etc backslash escapes expanded as
> well.
>
> Currently, I have the following code:
>
> my $fmt = "user: %u\ndomain:%d\n"; # example
> my $str = "";
> my @chars = split "", $fmt;
There's very little point using split for the job. Keep the string, keep the
$i variable, setup a length variable:
my $l = length $fmt;
access each character with
my $ch = substr($fmt, $i, 1);
which is a personal favourite of mine. Put that JUST inside the for loop
then reference $ch.
I would go for a simpler while loop, for reasons you are about to see:
my $l = length $fmt;
my $i = 0;
while($i < $l) {
my $ch = substr $fmt, $i++, 1;
.... PROCESSING ....
}
Inside this while loop, if you need to collect another character, simply say
my $ch = substr $fmt, $i++, 1;
of course, check that the current value of $i is < $l before you do this. Do
not trust the user to give a properly designed format string.
When you detect "\0" it becomes the simple matter of adding another loop to
collect the digits:
/0/ and do {
my $n = 0;
while($j < 3 and $i < $l and $ch =~ /\d+/) {
$n = $n * 8 + ord($ch) - ord('0');
$ch = substr $fmt, $i++, 1;
}
$ch = chr $n;
.... PROCESS $ch .....
}
see
perldoc -f ord
and
perldoc -f chr
--
There's nowt wrong wi' owt what mitherin' clutterbucks don't barly grummit!
-
Replies directly to this message will go to an account that may not be
checked for a week or two. For more timely e-mail response, use (only
in an emergency) ahamm sanderson net au with all the usual punctuation.
------------------------------
Date: Tue, 24 Dec 2002 01:58:41 +0000 (UTC)
From: Moshe Jacobson <bramoshe@runslinux.net>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8f0h$r89$6@news-int.gatech.edu>
Andrew Hamm had nothing better to do than to say:
> /0/ and do {
> my $n = 0;
> while($j < 3 and $i < $l and $ch =~ /\d+/) {
> $n = $n * 8 + ord($ch) - ord('0');
> $ch = substr $fmt, $i++, 1;
> }
> $ch = chr $n;
> .... PROCESS $ch .....
> }
Thanks for the quick reply, Andrew. ord and chr seem to be exactly
what I'm looking for.
While that solution will suffice, it would still be nice not to have
to specify all the possible escapes they could use, and just let the
perl preprocessor handle it somehow.
Moshe
--
*** SPAM BLOCK: Remove bra before replying! ***
Moshe Jacobson :: http://runslinux.net :: moshe at runslinux dot net
!! FBI Arreseting Programmers? http://www.freesklyarov.org
!! Trust Microsoft Anti-Trust: http://www.anti-dmca.org
------------------------------
Date: Tue, 24 Dec 2002 02:38:16 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <x7bs3ccfag.fsf@mail.sysarch.com>
>>>>> "AH" == Andrew Hamm <ahamm@mail.com> writes:
AH> I would go for a simpler while loop, for reasons you are about to see:
AH> my $l = length $fmt;
AH> my $i = 0;
AH> while($i < $l) {
AH> my $ch = substr $fmt, $i++, 1;
AH> .... PROCESSING ....
AH> }
why program in c when perl can do much better.
<untested>
# grab all known format items and format them
1 while( $fmt =~ s{(%[du]|\\[nr]|\\0x[\da-f]{2})}
{my_format( $1, \@args )}g ) ;
sub my_format {
my ( $fmt_item, $args ) = @_ ;
return sprintf( "%$1", shift @{$args} ) if $fmt_item = /%(.)/ ;
return "\n" if $fmt_item eq '\n' ;
return "\r" if $fmt_item eq '\r' ;
return char( $1 ) if $fmt_item = /0x(..)/ ;
.....
}
in some cases a dispatch table keyed on the format item can also be used
effectively.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 24 Dec 2002 14:18:52 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8jog$5alvh$1@ID-79573.news.dfncis.de>
Uri Guttman wrote:
>>>>>> "AH" == Andrew Hamm <ahamm@mail.com> writes:
>
> why program in c when perl can do much better.
>
Do you think he's ready for that?-) I just improved his loop, however you
are 100% right that it's a very C'ish solution - before and after my input.
You might want to explain each step of your solution? I'd wager that he
doesn't understand the key parts of it.
--
There's nowt wrong wi' owt what mitherin' clutterbucks don't barly grummit!
Replies directly to this message will go to an account that may not be
checked for a week or two. For more timely e-mail response, use (only
in an emergency) ahamm sanderson net au with all the usual punctuation.
------------------------------
Date: Tue, 24 Dec 2002 14:20:27 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8jre$585hb$1@ID-79573.news.dfncis.de>
Moshe Jacobson wrote:
> Andrew Hamm had nothing better to do than to say:
Actually, my boss would disagree most likely ;-)
> Thanks for the quick reply, Andrew. ord and chr seem to be exactly
> what I'm looking for.
>
> While that solution will suffice, it would still be nice not to have
> to specify all the possible escapes they could use, and just let the
> perl preprocessor handle it somehow.
As Uri pointed out, this is not a very Perlish solution at all. How
sophisticated is your programming in general, and how far into Perl are you?
We can make this as ugly and compact as you are willing to bear ;-)
--
There's nowt wrong wi' owt what mitherin' clutterbucks don't barly grummit!
-
Replies directly to this message will go to an account that may not be
checked for a week or two. For more timely e-mail response, use (only
in an emergency) ahamm sanderson net au with all the usual punctuation.
------------------------------
Date: Tue, 24 Dec 2002 14:22:06 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8juh$5cnin$1@ID-79573.news.dfncis.de>
Andrew Hamm wrote:
>
> We can make this as ugly and compact as you are willing to bear ;-)
Did I say ugly? I meant elegant, of course!
------------------------------
Date: Tue, 24 Dec 2002 04:04:27 +0000 (UTC)
From: Moshe Jacobson <bramoshe@runslinux.net>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8mcb$uf$1@news-int.gatech.edu>
Andrew Hamm had nothing better to do than to say:
> Do you think he's ready for that?-) I just improved his loop, however you
> are 100% right that it's a very C'ish solution - before and after my input.
Damn, you figured me out :) Yes, I am definitely a C programmer at
heart. I use Perl strictly for practical extraction and reporting,
because it is very good at it. However, I know that my code probably
does not take full advantage of all of Perl's little nuances.
> You might want to explain each step of your solution? I'd wager that he
> doesn't understand the key parts of it.
There were a couple things. I've posted them in response to Uri's
message.
Moshe
--
*** SPAM BLOCK: Remove bra before replying! ***
Moshe Jacobson :: http://runslinux.net :: moshe at runslinux dot net
!! FBI Arreseting Programmers? http://www.freesklyarov.org
!! Trust Microsoft Anti-Trust: http://www.anti-dmca.org
------------------------------
Date: Tue, 24 Dec 2002 04:08:58 +0000 (UTC)
From: Moshe Jacobson <bramoshe@runslinux.net>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8mkq$uf$2@news-int.gatech.edu>
Uri Guttman had nothing better to do than to say:
> # grab all known format items and format them
> 1 while( $fmt =~ s{(%[du]|\\[nr]|\\0x[\da-f]{2})}
> {my_format( $1, \@args )}g ) ;
Don't you want an e after the g there?
Also, I think you misunderstood my request. I am not looking to
convert \0x###, but rather \0## (octal values).
> sub my_format {
> my ( $fmt_item, $args ) = @_ ;
> return sprintf( "%$1", shift @{$args} ) if $fmt_item = /%(.)/ ;
Well this won't exactly work because the data is application
specific... but no matter. That's not the part that I'm having trouble
with.
> return "\n" if $fmt_item eq '\n' ;
> return "\r" if $fmt_item eq '\r' ;
Okay, similar to mine...
> return char( $1 ) if $fmt_item = /0x(..)/ ;
Here, what if the value matched by (..) is "10"? Going with your
assumption that I wanted hex values, this would be a value of 16, and
I would want to return char(16), not char("10"). Are you sure this
works? If so, why?
> in some cases a dispatch table keyed on the format item can also be used
> effectively.
not a bad idea either.
Thannks for the input,
Moshe
--
*** SPAM BLOCK: Remove bra before replying! ***
Moshe Jacobson :: http://runslinux.net :: moshe at runslinux dot net
!! FBI Arreseting Programmers? http://www.freesklyarov.org
!! Trust Microsoft Anti-Trust: http://www.anti-dmca.org
------------------------------
Date: Tue, 24 Dec 2002 15:15:10 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8n21$5d7lg$1@ID-79573.news.dfncis.de>
Moshe Jacobson wrote:
>
> Damn, you figured me out :)
>
Well, Uri did. He's a proper guru by the way.
> There were a couple things. I've posted them in response to Uri's
> message.
following you over...
------------------------------
Date: Tue, 24 Dec 2002 04:35:59 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <x7u1h4av9t.fsf@mail.sysarch.com>
>>>>> "MJ" == Moshe Jacobson <bramoshe@runslinux.net> writes:
MJ> Uri Guttman had nothing better to do than to say:
>> # grab all known format items and format them
>> 1 while( $fmt =~ s{(%[du]|\\[nr]|\\0x[\da-f]{2})}
>> {my_format( $1, \@args )}g ) ;
MJ> Don't you want an e after the g there?
yes. like i said, untested and also written with fuzzy brain from a cold
MJ> Also, I think you misunderstood my request. I am not looking to
MJ> convert \0x###, but rather \0## (octal values).
your spec was not clear so i just demoed a way to handle a variety of
format stuff. you should be able to change it to handle your real needs.
>> sub my_format {
>> my ( $fmt_item, $args ) = @_ ;
>> return sprintf( "%$1", shift @{$args} ) if $fmt_item = /%(.)/ ;
MJ> Well this won't exactly work because the data is application
MJ> specific... but no matter. That's not the part that I'm having trouble
MJ> with.
like i said, this was an example of a technique, not a direct answer to
your coding needs.
>> return "\n" if $fmt_item eq '\n' ;
>> return "\r" if $fmt_item eq '\r' ;
MJ> Okay, similar to mine...
>> return char( $1 ) if $fmt_item = /0x(..)/ ;
MJ> Here, what if the value matched by (..) is "10"? Going with your
MJ> assumption that I wanted hex values, this would be a value of 16, and
MJ> I would want to return char(16), not char("10"). Are you sure this
MJ> works? If so, why?
it may not work. again, very rough code. it should be easy to get the
behavior you want. my main goal was to get you away from the c like
char-by-char processing with substr. that is rarely the best way to
process perl strings.
i have done a format handler with a hash table for the possible keys but
all it handles was a few % things. it basically converts a %X to the
named value in the %entry hash.
sub _format_entry {
my( $entry ) = @_ ;
my $formatted = $entry->{'format'} ;
$formatted =~ s/%(.)/_format_field( $entry, $1 )/seg ;
return $formatted ;
}
my %letter_to_key = (
'T' => 'text',
't' => 'time',
'L' => 'label',
'l' => 'level',
'H' => 'hub_name',
'h' => 'host_name',
'P' => 'program_name',
) ;
sub _format_field {
my( $entry, $letter ) = @_ ;
if ( my $key = $letter_to_key{ $letter } ) {
return $entry->{$key} ;
}
if ( $letter eq 'f' ) {
require POSIX ;
$entry->{'formatted_time'} ||= do {
my @times = ( $entry->{'use_gmt'} ) ?
gmtime( $entry->{'time'} ) :
localtime( $entry->{'time'} ) ;
POSIX::strftime( $entry->{'strftime'}, @times ) ;
} ;
return $entry->{'formatted_time'} ;
}
return $letter ;
}
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 24 Dec 2002 15:43:32 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <au8on8$5d162$1@ID-79573.news.dfncis.de>
Moshe Jacobson wrote:
> Uri Guttman had nothing better to do than to say:
>>
>> # grab all known format items and format them
>
>> 1 while( $fmt =~ s{(%[du]|\\[nr]|\\0x[\da-f]{2})}
>> {my_format( $1, \@args )}g ) ;
>
> Don't you want an e after the g there?
That would be true. This is where the <untested> disclaimer comes in very
useful. I'd go for an x too; I luv regex but they still make me cross-eyed:
1 while( $fmt =~ s{ (%[du]
| \\[nr]
| \\0x [\da-f] {2}
)
}
{my_format( $1, \@args )}gex
) ;
Now, that will probably need indenting properly; i'm too lazy to change from
the proportional font in this newsreader and i'm just pressing spaces until
it looks good here;-)
Actually, I'm quizzical about the need for
1 while ....
because that will surely convert the contents of any of your substitutions
ad-nauseum if your subsitutions happen to resolve to \\n or %d or similar.
The /g should pump the string for all it's worth. Really, we should test but
of course that's your job, right?
> Also, I think you misunderstood my request. I am not looking to
> convert \0x###, but rather \0## (octal values).
>
>> return char( $1 ) if $fmt_item = /0x(..)/ ;
>
> Here, what if the value matched by (..) is "10"? Going with your
> assumption that I wanted hex values, this would be a value of 16, and
> I would want to return char(16), not char("10"). Are you sure this
> works? If so, why?
Most likely just attempting to interpret your words. Your sample "\0xx" is
ambiguous since \0x is the start of a hex escape. You needed to add "where x
is any digit" or something... If you want octal then use oct($1).
Your pattern (in part) thus becomes
\\0 [0-7]{1,2}
>> sub my_format {
>
>> my ( $fmt_item, $args ) = @_ ;
>
>> return sprintf( "%$1", shift @{$args} ) if $fmt_item = /%(.)/ ;
>
> Well this won't exactly work because the data is application
> specific... but no matter. That's not the part that I'm having trouble
> with.
If your %x (where x is any letter ;-) invokes specialised services then
you'll have to deal with them individually. If they all can be resolved by
variables then perhaps you can use a lookup table which stores references to
those variables:
%percenters = ('d' => \$domain, 'u' => \$usr, .... );
and then to get the value you say:
${$percenters{$1)}
which de-references the reference which is looked up. Are you familiar and
comfortable with references? Very similar in principle to pointers etc,
however the syntax for dereferencing them can get very hairy.
>> return "\n" if $fmt_item eq '\n' ;
>> return "\r" if $fmt_item eq '\r' ;
>
> Okay, similar to mine...
If you want to cover most or all of the usual escapes available in C or
Perl, then the lookup would probably make life easier. Initialise
%sloshers = ("\\\\" => "\\", "\\n" => "\n", '\\t' => "\t" ....)
and then include all the appropriate characters in the \\[\\dntr....] part
of the pattern.
Is this getting closer to usable?
------------------------------
Date: Tue, 24 Dec 2002 04:56:52 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Processing \0xx in nonliteral strings
Message-Id: <x7lm2gaub0.fsf@mail.sysarch.com>
>>>>> "AH" == Andrew Hamm <ahamm@mail.com> writes:
AH> Actually, I'm quizzical about the need for
AH> 1 while ....
AH> because that will surely convert the contents of any of your
AH> substitutions ad-nauseum if your subsitutions happen to resolve to
AH> \\n or %d or similar. The /g should pump the string for all it's
AH> worth. Really, we should test but of course that's your job,
AH> right?
which is what i did in my formatter. but my excuse is a very fuzzy and
ill brain.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Damian Conway Perl Classes - January 2003 -- http://www.stemsystems.com/class
------------------------------
Date: Tue, 24 Dec 2002 04:54:16 GMT
From: "Jodyman" <Jodyman@hotmail.com>
Subject: Re: regular expression help
Message-Id: <YLRN9.3339$ka5.383005@newsread1.prod.itd.earthlink.net>
"Mark" <mfaine@knology.net> wrote in message
news:329dd608.0212231225.383e4793@posting.google.com...
> I admit I've never been good with regular expressions but this one I
> imagine would challenge even a pro. In order to comply with a new
> security directive passwords must meet the following guidelines:
>
> eight characters exactly
>
> The eight characters will contain at least one character each from at
> least three of the following sets of characters:
>
> At least one uppercase letter
> At least one lowercase letters
> At least one numbers
> At least one special characters
>
> I've tried various times, but as I said this one is hard:
>
> My feeble attempts have gotten me this far:
>
> /([A-Z]|[a-z]|[0-9]|(\W)){3,8}/
>
> but this does not account for order and, well it doesn't accout for so
> many things...
>
> that's why I'm here, I can work out the simple ones but for this one I
> have to ask for help from the experts. I know if I spent the next two
> or three days working on this I might could get close or even figure
> it out but time is not a luxury I have on this one and hey why
> re-invent the wheel.
Do you want to validate passwords or generate passwords with this criteria?
------------------------------
Date: 23 Dec 2002 18:50:01 -0800
From: 00056312@brookes.ac.uk (Stephen Adam)
Subject: Simple array problem
Message-Id: <945bf980.0212231850.79d28452@posting.google.com>
Hi, a really simple problem here (i think!)
I need to create a new array and its contents need to the result of a range
of another array. I have shown the vague code of what I want to do below.
If $i is 1 and $f is 3 then the contents of @new should be @new = ("e", "l",
"l");
@docarray = ("h","e","l","l","o"," ","w","o","r","l","d");
$i = 1;
$f = 3;
@new = ($docarray[$i] to $docarray[$f]);
print @new;
exit(0);
Is this possible in one easy line of code in the same way as splice works
for deletion of elements?
Thanks
Steve
00056312@brookes.ac.uk
------------------------------
Date: Tue, 24 Dec 2002 14:27:49 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Simple array problem
Message-Id: <au8k98$5cbe7$1@ID-79573.news.dfncis.de>
Stephen Adam wrote:
> Hi, a really simple problem here (i think!)
>
> I need to create a new array and its contents need to the result of a
> range of another array. I have shown the vague code of what I want to
> do below.
> Is this possible in one easy line of code in the same way as splice
> works for deletion of elements?
Absolutely!!!
@new = @docarray[$i .. $f];
This is wierd to explain and Perl is fairly unique with the reasons for it.
However, it's sorta like this:
$docarray[N] means "a SCALAR part of docarray"
@docarray[N,N,N ....] means "an ARRAY part of docarray"
it's really that simple, and IMHO just to look at it and know what it does
is all that you need to know to develop and understanding and explanation
for yourself.
--
There's nowt wrong wi' owt what mitherin' clutterbucks don't barly grummit!
-
Replies directly to this message will go to an account that may not be
checked for a week or two. For more timely e-mail response, use (only
in an emergency) ahamm sanderson net au with all the usual punctuation.
------------------------------
Date: Tue, 24 Dec 2002 14:30:13 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Simple array problem
Message-Id: <au8kdo$5benr$1@ID-79573.news.dfncis.de>
Andrew Hamm wrote:
>
> Absolutely!!!
>
> @new = @docarray[$i .. $f];
>
In fact, chew on this:
%hash = (a => 1, b => 2, c => 3);
@arr = @hash{a,c};
Consufed? Delighted? Excited?
------------------------------
Date: Tue, 24 Dec 2002 11:10:24 +1100
From: "Andrew Hamm" <ahamm@mail.com>
Subject: Re: Tied Filehandles
Message-Id: <au88n4$5b0gv$1@ID-79573.news.dfncis.de>
Benjamin Goldberg wrote:
>
> I would suggest that you try upgrading to 5.8 -- I believe that this
> should allow warn(), carp(), etc, to properly send their data to the
> tied handle.
Emmmm - I've been thinking about upgrading but I'm very conservative and
don't like using anything that is .0 But this is Perl right? So it's solid!
I don't see any complaints floating thru the Ether about 5.8 so maybe it's
time to take a chance.
For the record I'll post whether it fixes my problem. If it doesn't, is this
worthy of a bug notification or feature request? If warn is still not
testing for and invoking ties then presumably it's fairly routine to make
that happen.
------------------------------
Date: Tue, 24 Dec 2002 13:04:44 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: using Perl inside directories and extensions questions
Message-Id: <slrnb0fg5s.aid.mgjv@martien.heliotrope.home>
On Mon, 23 Dec 2002 10:24:14 -0600,
Tad McClellan <tadmc@augustmail.com> wrote:
> Patrick Smith <patrick@ocg6.marine.usf.edu> wrote:
>
>> Is it best done inside Perl, or from a shell script that
>> calls the Perl script?
>
>
> It is nearly always better to do things in native Perl rather
> than rely on external programs.
>
> Native Perl will, in general, be faster and more portable.
I agree, in general. However, that isn't what the OP asked.
There are things that are often more naturally done in a shell script
_before_ the Perl program is called. I have many shell wrappers for Perl
programs, many of which are used to set up environments from cron jobs
or so, before calling Perl.
For example, I have loads of shell stuff that sets up environment
variables based on the OS I am on, the domain and network I am in, and
whether certain services are running or not. Those shell scripts are
used by all kinds of shell wrappers, that invoke all kinds of programs
written in all kinds of languages. Doing all this in shell avoids
duplication of all that logic over 5 or 6 languages, and is more natural
than doing it in Perl, since much of the information I need can only be
obtained by using external programs or platform-specific modules.
For this particular case it might very well be easier to let the shell
do the globbing and stuff, and write a Perl program that takes a bunch
of files as arguments. It might also be easier to let the Perl program
do everything. I tend to write most of my Perl program as filters, in
the tradition of most Unix programs, so for me it would be most likely
much better to write a shell wrapper, and keep the Perl program general.
I would hesitate to come up with a general rule of thumb on this one,
simply because it depends entirely on whether the program needs to be
reused for something else or not.
And of course, in my case: I almost never work on non-unix platforms,
and if I do, I tend to have compatibility layers installed.
Martien
--
|
Martien Verbruggen | The problem with sharks is that they are too
| large to get to the shallow end of the gene
| pool. -- Scott R. Godin
------------------------------
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.
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 4308
***************************************