[26572] in Perl-Users-Digest
Perl-Users Digest, Issue: 8708 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Nov 26 06:05:21 2005
Date: Sat, 26 Nov 2005 03:05:04 -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 Sat, 26 Nov 2005 Volume: 10 Number: 8708
Today's topics:
Hash's Regex Transformation with Map <ewijaya@singnet.com.sg>
Re: Hash's Regex Transformation with Map <noreply@gunnar.cc>
Re: Hash's Regex Transformation with Map robic0
Re: Matching spaces at start of line <someone@example.com>
Re: potential problem with fork() <someone@example.com>
Re: using perl to print yesterday's date, but with form robic0
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sat, 26 Nov 2005 11:49:02 +0800
From: "Edward WIJAYA" <ewijaya@singnet.com.sg>
Subject: Hash's Regex Transformation with Map
Message-Id: <op.s0twv0d5r4r1oi@localhost.localdomain>
Hi all,
How can I make my snippet below:
$ perl -MData::Dumper -e '
$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
print Dumper \%nh;'
To produce:
$VAR1 = {
'ASGG 3' => 'foo',
'CTGS 2' => 'bar',
};
Principally I want to change the key of the old hash by
replacing bracketed strings with "S".
--
Regards,
Edward WIJAYA
SINGAPORE
------------------------------
Date: Sat, 26 Nov 2005 10:53:36 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Hash's Regex Transformation with Map
Message-Id: <3uqph8F12b2gmU1@individual.net>
Edward WIJAYA wrote:
> How can I make my snippet below:
>
>
> $ perl -MData::Dumper -e '
> $h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
> %nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
> print Dumper \%nh;'
>
> To produce:
>
> $VAR1 = {
> 'ASGG 3' => 'foo',
> 'CTGS 2' => 'bar',
> };
For a start, you'd better ask Perl for help:
$ perl -wMData::Dumper -e ...
------------^
Personally, when the output from a code snippet differs from what you
had expected, I don't think that one-liners should be posted here. By
writing the program in a file and enabling strictures, there is a good
chance that you find your mistake.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sat, 26 Nov 2005 02:25:57 -0800
From: robic0
Subject: Re: Hash's Regex Transformation with Map
Message-Id: <8ndgo11bdods1vvr2il9mdonov5jli4r1a@4ax.com>
On Sat, 26 Nov 2005 11:49:02 +0800, "Edward WIJAYA"
<ewijaya@singnet.com.sg> wrote:
>Hi all,
>
>How can I make my snippet below:
>
>
>$ perl -MData::Dumper -e '
>$h = {"A[TCG]GG 3" => foo, "CTG[AA] 2" => bar};
>%nh = map { my $k =~ s/\[[ATCG]+\]/S/g; $k=> $h->{$_} } (keys %{$h});
>print Dumper \%nh;'
>
>To produce:
>
>$VAR1 = {
> 'ASGG 3' => 'foo',
> 'CTGS 2' => 'bar',
> };
>
>Principally I want to change the key of the old hash by
>replacing bracketed strings with "S".
Have to copy $_ in the map statement otherwise $h gets altered.
I would not create a variable to anonymous reference this way
unless it is going to be passed to subroutines alot or creating
a class. Reserve anonymous array/hash references for array elements.
Just my opinion.....
use strict;
use warnings;
use Data::Dumper;
#my $h = {'A[TCG]GG 3' => 'foo', 'CTG[AA] 2' => 'bar'};
#my %nh = map { my $k=$_; $k =~s/\[[ATCG]+\]/S/g; $k=> $h->{$_} }
(keys %{$h});
my %h = ('A[TCG]GG 3' => 'foo', 'CTG[AA] 2' => 'bar');
my %nh = map { my $k=$_; $k =~s/\[[ATCG]+\]/S/g; $k=> $h{$_} } (keys
%h);
print Dumper \%nh;
#print Dumper $h;
print Dumper \%h;
------------------------
output:
$VAR1 = {
'ASGG 3' => 'foo',
'CTGS 2' => 'bar'
};
$VAR1 = {
'CTG[AA] 2' => 'bar',
'A[TCG]GG 3' => 'foo'
};
------------------------------
Date: Sat, 26 Nov 2005 02:11:04 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Matching spaces at start of line
Message-Id: <YuPhf.170272$Io.137396@clgrps13>
Anno Siegel wrote:
> John W. Krahn <krahnj@telus.net> wrote in comp.lang.perl.misc:
>>Tad McClellan wrote:
>>>Paul Jones <pj123781@hotmail.com> wrote:
>>>
>>>>the comments header is often split over multiple lines (usually
>>>>4) and I want to include them all. The 2-4th lines of the header
>>>>all begin with spaces (usually 6 - perhaps its a tab).
>>>
>>>> print INEWS
>>>>
>>>> if /^(Date|From|Subject|Newsgroups|Comments):/i;
>>>
>>> if /^(Date|From|Subject|Newsgroups|Comments):/i or /^\s/;
>>That is going to print from every header that is folded, not just the Comments
>>header. :-(
>
> I think it should, for those headers that are to be printed at all. I
> know that OP singled out the Comments header, but that's probably just
> because it's the only one that *has* continuation lines with any regularity.
>
> I read the problem as "Print all valid headers ( Date, From, ...) and
> their continuation lines, suppress all others, including their continuation
> lines". Could be done like this:
>
> my $saw_newsgroup;
> my $valid_header;
> while (<DATA>) {
> last if /^$/;
>
> if ( /^\S/ ) {
> $valid_header =
> s{^(Date|From|Subject|Newsgroups|Comments):}
> {\u\L$1:}i;
> $saw_newsgroup ||= $valid_header && $1 eq 'Newsgroups';
If the original header is not exactly 'Newsgroups', for example 'newsgroups'
or 'NewsGroups', then that expression will fail. Perhaps:
$saw_newsgroup ||= $valid_header && lc( $1 ) eq 'newsgroups';
> }
> print if $valid_header;
> }
> die "didn't get newsgroup from headers\n" unless $saw_newsgroup;
>
> print "\n";
> print while <DATA>; # gobble rest of message
>
> Besides adding the logic to control $valid_header, I have used the
> s/// operation that normalizes header spelling to determine if
> we have a valid header. This way, the list of valid headers appears
> only once in the code, while the original code had it twice. A
> maintainer will appreciate that.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 26 Nov 2005 02:20:52 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: potential problem with fork()
Message-Id: <8EPhf.130775$y_1.44600@edtnps89>
Jim Gibson wrote:
>
> You should use the 3-argument version of open(). You should print why
> open did not succeed if it failed. It is usually better to use
> variables rather than barewords for file handles:
>
> if ( open( my $file, '<', $books ) ) {
^^^
> ...
> }else{
> die("Can't open $books for writing: $!");
^^^^^^^
I'm not suprised that it won't open for writing.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sat, 26 Nov 2005 03:12:01 -0800
From: robic0
Subject: Re: using perl to print yesterday's date, but with formatting options ?
Message-Id: <4kggo1d67vipb0ihesfsstnmv111iciacu@4ax.com>
On Mon, 31 Oct 2005 17:57:53 +0100, "Tom Van Overbeke"
<tomvo@hahaha.be> wrote:
>Hi,
>
>I need a way to use yesterday's date in a variable. The tricks with 'date'
>don't work on my system, but perl does.
>
>so i've got:
> perl -le 'print scalar localtime time - 86400' which outputs:
>Sun Oct 30 17:56:52 2005
>
>But the format I need is: 30.10.05
>
>Any ideas on how to add this to the above piece of code ?
>
>thanks,
>
>tom.
>
Tom, if your into thinking and roll-your-own, try this:
sub FmtTime
{
my ($ttype, $since) = @_;
$since = time() unless defined $since;
$ttype = 0 unless (defined $ttype);
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday,
$isdst) = localtime($since);
my %months = (1 => 'Jan', 2 => 'Feb', 3 => 'Mar', 4 => 'Apr',
5 => 'May', 6 => 'Jun',
7 => 'Jul', 8 => 'Aug', 9 => 'Sep', 10 => 'Oct',
11 => 'Nov', 12 => 'Dec');
return sprintf ("%s %s %d:%0.2d:%0.2d", $months{$mon+1},
$mday, $hour, $min, $sec) if ($ttype == 1);
return sprintf ("%0.2d%0.2d%4d", $mon+1, $mday, ($year+1900))
if ($ttype == 2);
return sprintf ("%d/%d/%0.2d", $mon+1, $mday, ($year-100)) if
($ttype == 3);
return sprintf ("%0.2d/%0.2d/%0.2d", $mon+1, $mday,
($year-100)) if ($ttype == 4);
if ($ttype == 5) {
my $am_pm = ($hour >= 12 && $min >= 0 && $sec >= 0) ?
"PM" : "AM";
my $hour_12 = ($hour < 13) ? $hour : $hour-12;
return sprintf ("%0.2d/%0.2d/%0.2d - %2s:%0.2d %s",
$mon+1, $mday, ($year-100), $hour_12, $min, $am_pm)
}
return sprintf ("%0.2d/%0.2d/%d", $mon+1, $mday, ($year+1900))
if ($ttype == 6);
#return sprintf ("%0.2d/%0.2d/%0.2d", $mon+1, $mday,
($year-100)) if ($ttype == 6);
return "";
}
------------------------------
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 8708
***************************************