[22590] in Perl-Users-Digest
Perl-Users Digest, Issue: 4811 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Apr 3 18:06:44 2003
Date: Thu, 3 Apr 2003 15:05:12 -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 Apr 2003 Volume: 10 Number: 4811
Today's topics:
Re: Algorithm Suggestion <krahnj@acm.org>
Re: Algorithm Suggestion (Walter Roberson)
Re: Can Perl mimic the awk '/start/,/end/' syntax (Walter Roberson)
Re: CGI.pm or roll-your-own? <mpapec@yahoo.com>
Re: CGI.pm or roll-your-own? <wsegrave@mindspring.com>
Re: CGI.pm or roll-your-own? <flavell@mail.cern.ch>
Re: comments within complex structures <usenet@NOSPAM.matthewb.org>
Re: comments within complex structures <perl-dvd@darklaser.com>
Re: comments within complex structures <wksmith@optonline.net>
Customizing CSVREAD.PL script (Kevin McGurk)
DBI webserver reports error 503 when SQL-statement retu <nospam_stigerikson@yahoo.se>
Re: DBI webserver reports error 503 when SQL-statement <glex_nospam@qwest.net>
Re: DBI webserver reports error 503 when SQL-statement (Malcolm Dew-Jones)
Re: File more recent than 7 days ? <tore@aursand.no>
Re: File more recent than 7 days ? <krahnj@acm.org>
Re: File more recent than 7 days ? <me@privacy.net>
Re: File more recent than 7 days ? <Jodyman@hotmail.com>
Formatting output in columns: (evolutionx1945@yahoo.com)
Re: Formatting output in columns: <mbudash@sonic.net>
Re: Formatting output in columns: <tore@aursand.no>
Re: How to use newlines in DOS command options (Malcolm Dew-Jones)
Re: implementing a threaded serial device (or any other <goldbb2@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 03 Apr 2003 22:25:54 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Algorithm Suggestion
Message-Id: <3E8CB4E9.F10C6741@acm.org>
Ed Napier wrote:
>
> I'm looking for suggestions on a fast algorithm to do the following:
>
> I have a list (thousands) of computers and IP addresses (10.10.10.20)
> and flat file with thousands of entries relating to subnets and
> gateways. The format is:
>
> First.3.Octets,lower range, upper range, gatewayname
>
> which ends up looking something like this:
>
> 172.18.121,0,15,gateway1
> 172.18.121,16,30,gateway2
> 172.18.121,31,100,gateway3
>
> What I need to do is find the gateway that corresponds to each
> computer. Now, I can come up with BFI algorithm to do it, but I was
> hoping that someone out there has a suggestion that borders on magic.
This will do what you want assuming the computer file is comma separated as well:
#!/usr/bin/perl
use warnings;
use strict;
use Socket;
my $gateways = 'gateways.txt';
my $computers = 'computers.txt';
my $search = do {
my @gateways;
open my $fh, $gateways or die "Cannot open $gateways: $!";
while ( <$fh> ) {
chomp;
my ( $ip, $start, $end, $gateway ) = split /,/;
push @gateways, [ inet_aton( "$ip.$start" ), inet_aton( "$ip.$end" ), $gateway ];
}
# needs to be sorted for binay search to work
@gateways = sort { $a->[0] cmp $b->[0] } @gateways;
sub {
my $word = shift;
my ( $low, $high ) = ( 0, $#gateways );
while ( $low <= $high ) {
my $try = int( ( $low + $high ) / 2 );
if ( $gateways[ $try ][ 1 ] lt $word ) {
$low = $try + 1;
next;
}
if ( $gateways[ $try ][ 0 ] gt $word ) {
$high = $try - 1;
next;
}
return $gateways[ $try ][ 2 ];
}
return;
}
};
open my $fh, $computers or die "Cannot open $computers: $!";
while ( <$fh> ) {
chomp;
my ( $name, $ip ) = split /,/;
if ( defined( my $found = $search->( inet_aton( $ip ) ) ) ) {
print "$name -> $found\n";
}
else {
print "Gateway for $name not found.\n";
}
}
__END__
John
--
use Perl;
program
fulfillment
------------------------------
Date: 3 Apr 2003 22:27:52 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: Algorithm Suggestion
Message-Id: <b6ich8$4d7$1@canopus.cc.umanitoba.ca>
In article <bb040849.0304030702.2e5b4d36@posting.google.com>,
Ed Napier <p1edn01@netscape.net> wrote:
:I'm looking for suggestions on a fast algorithm to do the following:
:I have a list (thousands) of computers and IP addresses (10.10.10.20)
:and flat file with thousands of entries relating to subnets and
:gateways. The format is:
:First.3.Octets,lower range, upper range, gatewayname
:which ends up looking something like this:
:172.18.121,0,15,gateway1
:172.18.121,16,30,gateway2
:172.18.121,31,100,gateway3
Is that just examplese, or is it truly the case that your
gateways do no align with subnet boundaries?
:What I need to do is find the gateway that corresponds to each
:computer. Now, I can come up with BFI algorithm to do it, but I was
:hoping that someone out there has a suggestion that borders on magic.
I don't know about magic, but I've done pretty much the same thing
in some perl scripts in a slightly different guise, and it's reasonably
fast.
My code is for a smart 'whois' replacement that knows about numerous
registries and knows how to deal with rwhois (but not how do do
lookups on WWW pages yet). The control file defining the registries
and which global IP range is associated with which registry is
close to 9000 lines long at the moment, but the code gets through
everything in a small number of seconds.
My algorithm has multiple phases:
A) I write the control file entries in terms of IP ranges.
I then have a simple parser that converts those entries into
CIDR format, automatically generating all the necessary CIDRs
right down to the host level if need be. It would be fairly simple
to modify the code to create a CIDR file listing associated
"gateways" instead of registries -- or you could easily
do a textual conversion on your existing gateways list by using
sed to put it into the format that my control file is expecting.
B) When someone runs a request, the first thing the algorithm does
is read the control file, and build a table of registries,
base IP's, and netmasks. The IPs and netmasks are stored in binary
for this purpose.
C) Then the table has to be reverse sorted by the binary base IPs.
This is necessary because for my application, there can be
registries that cover only some of the subranges, leaving the
other subranges to "default" to the enclosing CIDR. A more
specific CIDR will always have a larger [unsigned] binary value
than an enclosing less-specific CIDR. This is about the only
"magic" in the algorithm.
D) Then I just do a linear search, does source IP & mask equal
the base IP. If so, I've found the match. And yes, this code is
already written to handle searching on multiple items simultaneously
[but I don't promise at the moment that the output is in the same order
as the input. It wouldn't be much of a change if that were necessary.]
E) With lists of IPs and corresponding registries constructed, I
then run the 'whois' queries.
Although it's a linear algorithm, it doesn't take very long to
execute even on a 150 MHz machine. Someone who was ambitious code
recode as a binary search algorithm... or could recode to store
the binary data directly in a db3... or whatever.
> What I need to do is find the gateway that corresponds to each
> computer.
I think you can see that this would be a very simple modification.
Indeed, it so happens that I broke the search algorithm itself out
to a different script, so about the only modification might be to
the output format.
Disclaimer: I wrote these programs for my own [spam-control] purposes,
not to be Perl expert or to create a CPAN Module out of it.
Anyone who is more interested in the [lack of] elegance of my code
than in getting the job done is free to write their own.
--
What is "The Ultimate Meme"? Would it, like Monty Python's
"The World's Funniest Joke", lead to the deaths of everyone who
encountered it? Ideas *have* lead to the destruction of entire cultures.
-- A Child's Garden Of Memes
------------------------------
Date: 3 Apr 2003 21:53:13 GMT
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Subject: Re: Can Perl mimic the awk '/start/,/end/' syntax
Message-Id: <b6iag9$3gk$1@canopus.cc.umanitoba.ca>
In article <x7istwi5bq.fsf@mail.sysarch.com>,
Uri Guttman <uri@stemsystems.com> wrote:
:>>>>> "WR" == Walter Roberson <roberson@ibd.nrc-cnrc.gc.ca> writes:
: WR> perl -pe 'undef $_ unless /start/../end/' myfile.txt
:bah! that will spit out warnings.
It doesn't put out any warnings with perl 5.004_05 or 5.6.1 on IRIX.
--
"There are three kinds of lies: lies, damn lies, and statistics."
-- not Twain, perhaps Disraeli, first quoted by Leonard Courtney
------------------------------
Date: Thu, 03 Apr 2003 21:55:14 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: CGI.pm or roll-your-own?
Message-Id: <134p8voqa3daovvdqqcmfliv0apfhgb0nh@4ax.com>
X-Ftn-To: Randal L. Schwartz
merlyn@stonehenge.com (Randal L. Schwartz) wrote:
>>> The code already "doesn't load" what it doesn't need.
>
>Gunnar> I know that you don't need to import all the symbols, but the whole
>Gunnar> thing is still compiled, isn't it?
>
>No, it uses a variant of "SelfLoader" called "the lincoln loader". It
>compiles code of itself only as necessary.
Can I assume that using only param sub from current CGI.pm would be
equal/similar in loading time to another(much smaller) module with just that
one function?
I'm somehow suspicious about CGI.pm as it is one of larger modules from
standard library and I'm condemned to 5.005 so it would be helpful to know
if "SelfLoader" was present in earlier versions.
--
Matija
------------------------------
Date: Thu, 3 Apr 2003 14:18:18 -0600
From: "William Alexander Segraves" <wsegrave@mindspring.com>
Subject: Re: CGI.pm or roll-your-own?
Message-Id: <b6i5h5$s9m$1@slb3.atl.mindspring.net>
"Matija Papec" <mpapec@yahoo.com> wrote in message
news:134p8voqa3daovvdqqcmfliv0apfhgb0nh@4ax.com...
<snip>
> >No, it uses a variant of "SelfLoader" called "the lincoln loader". It
> >compiles code of itself only as necessary.
<snip>
> I'm somehow suspicious about CGI.pm as it is one of larger modules from
> standard library and I'm condemned to 5.005 so it would be helpful to know
> if "SelfLoader" was present in earlier versions.
See Revision History in the CGI.htm web page at Linclon Stein's web site and
search for "SelfLoader".
Bill Segraves
------------------------------
Date: Thu, 3 Apr 2003 22:51:17 +0200
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: CGI.pm or roll-your-own?
Message-Id: <Pine.LNX.4.53.0304032245150.6155@lxplus094.cern.ch>
On Thu, Apr 3, William Alexander Segraves inscribed on the eternal scroll:
> "Matija Papec" <mpapec@yahoo.com> wrote in message
> > I'm somehow suspicious about CGI.pm as it is one of larger modules from
> > standard library and I'm condemned to 5.005 so it would be helpful to know
> > if "SelfLoader" was present in earlier versions.
>
> See Revision History in the CGI.htm web page at Linclon Stein's web site and
> search for "SelfLoader".
Right. And then, if appropriate, read the Perl FAQ section about
installing a private version of a module. I had to upgrade CGI.pm
when I installed a faqomatic on someone else's web server, because the
CGI.pm bundled with the supported Perl version there was too old. It
was no big deal, I just followed the instructions.
------------------------------
Date: Thu, 03 Apr 2003 16:42:32 +0100
From: Matthew Browning <usenet@NOSPAM.matthewb.org>
Subject: Re: comments within complex structures
Message-Id: <pan.2003.04.03.16.42.28.656169.572@NOSPAM.matthewb.org>
On Thu, 03 Apr 2003 15:41:52 +0100, David wrote:
> This hash definition works just fine on my linux machine.
>
use strict, dude.
[MB]
------------------------------
Date: Thu, 03 Apr 2003 16:48:57 GMT
From: "David" <perl-dvd@darklaser.com>
Subject: Re: comments within complex structures
Message-Id: <ZBZia.37$NY5.14308@news-west.eli.net>
"Matthew Browning" <usenet@NOSPAM.matthewb.org> wrote in message
news:pan.2003.04.03.16.42.28.656169.572@NOSPAM.matthewb.org...
> On Thu, 03 Apr 2003 15:41:52 +0100, David wrote:
>
> > This hash definition works just fine on my linux machine.
> >
> use strict, dude.
Sorry about that. I usually just copy and paste peoples code into my
test.pl which I don't have anything else in (strict or warnings), but
after making a few minor modifications, this works:
######################################
use strict;
my %complex_set1 = ( # Complex set 1.
fld_set1 => { # Field set 1.
fs1_fld1=> # fs1 field 1.
'fs1_fld2' # fs1 field 2.
},
fld_set2 => { # Field set 2.
fs2_fld1=> # fs2 field 1.
'fs2_fld2' # fs2 field 2.
}
);
######################################
Regards,
David
perl -e'print for map chr$_+1,"111100113107044099117099132"=~/(.{3})/g'
------------------------------
Date: Thu, 03 Apr 2003 17:10:59 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: comments within complex structures
Message-Id: <DWZia.10689$B8.3257351@news4.srv.hcvlny.cv.net>
"qanda" <fumail@freeuk.com> wrote in message
news:62b4710f.0304030404.53a9d533@posting.google.com...
> Hi all, can you tell me if there is any way to include comments
> directly inside complex structures. The following is an example of
> what I would like, however Perl would not like this ...
>
> my %complex_set1 = ( # Complex set 1.
> fld_set1 => { # Field set 1.
> fs1_fld1, # fs1 field 1.
> fs1_fld2, # fs1 field 2.
> },
> fld_set2 => { # Field set 2.
> fs2_fld1, # fs2 field 1.
> fs2_fld2, # fs2 field 2.
> },
> );
> (followed by other complex sets)
>
>
This is a great example of the advantage of 'use strict' and 'use
warnings'. Without strict, this code is valid, but probably does not
mean what you intend. Use the command 'x %complex_set1' under the perl
debugger (perl -d ...) to see what perl thinks it means. You will see
that the problem has nothing to do with comments. Test your corrections
the same way.
With strict, the script does not compile. You receive several error
messages which may not be clear at first. They certainly refer to the
correct statement. You suspected the comments were a problem. If you
had temporarily removed them, you would have found that that did not fix
the problem. You would then have had the information to ask the right
question.
Good Luck,
Bill
------------------------------
Date: 3 Apr 2003 12:40:11 -0800
From: kgmmusic@comcast.net (Kevin McGurk)
Subject: Customizing CSVREAD.PL script
Message-Id: <805e32e3.0304031240.5443c00b@posting.google.com>
Hello all!
I am using the csvread.pl script from http://www.ezscripting.co.uk/
and have it working OK. The script reads a CSV database and displays
results using an HTML template.
What I'd like to know is if there is a way that I can display results
in a table with alternating colors (see sample links below). I have it
working OK now but it will only repeat the dynamic results using one
row format and I can't quite figure out how to make the script
alternate the row format colors. Any and all help is much appreciated!
SAMPLE LINKS:
http://www.apronsetc.com/cgi-bin/csvread.txt (PERL SCRIPT/text format)
http://www.apronsetc.com/cgi-bin/template.htm (HTML TEMPLATE/view
source)*
*note: dynamic section contained within <template> and </template>
tags.
http://www.apronsetc.com/cgi-bin/csvread.pl (SCRIPT IN ACTION)*
*note: bottom 2 lines static, would like all rows to match these.
http://www.apronsetc.com/cgi-bin/database.txt (CSV DATABASE)
Kevin McGurk - kgmmusic(at)comcast.net
------------------------------
Date: Thu, 03 Apr 2003 22:14:47 +0200
From: stig <nospam_stigerikson@yahoo.se>
Subject: DBI webserver reports error 503 when SQL-statement returns error.
Message-Id: <b6i4qe$q48$1@oden.abc.se>
hi
in a script running on a webserver, doing UPDATE on a table with a column
that has the property UNIQUE set, when a user tries to change one email
address to an email address that already exists, insted of feching the
error the error kills the process and the webserver reports error 503 to
the user, not very nice.
here is the code that is supposed to update the email address.
i would like to fetch the error somehow so that i can report to the user
that this email is already existing.
how should i do to fetch the error?
---START CODE---
my $dbh = DBI->connect("DBI:mysql:someDB:localhost:3306", '?', '?',
{ RaiseError => 1, AutoCommit => 1 });
my $sth = $dbh->prepare("UPDATE persons SET email=" .
$dbh->quote($personEmail) . " " .
"WHERE personID=" . $dbh->quote($personID) )
or warn "\ncannot do UPDATE on persons\n";
$sth->execute;
$sth->finish;
$dbh->disconnect;
---END CODE---
thanks
stig
------------------------------
Date: Thu, 03 Apr 2003 14:43:55 -0600
From: Jeff D Gleixner <glex_nospam@qwest.net>
Subject: Re: DBI webserver reports error 503 when SQL-statement returns error.
Message-Id: <f%0ja.60$uo6.76549@news.uswest.net>
stig wrote:
[...]
> here is the code that is supposed to update the email address.
> i would like to fetch the error somehow so that i can report to the user
> that this email is already existing.
>
> how should i do to fetch the error?
>
> ---START CODE---
> my $dbh = DBI->connect("DBI:mysql:someDB:localhost:3306", '?', '?',
> { RaiseError => 1, AutoCommit => 1 });
>
> my $sth = $dbh->prepare("UPDATE persons SET email=" .
> $dbh->quote($personEmail) . " " .
> "WHERE personID=" . $dbh->quote($personID) )
> or warn "\ncannot do UPDATE on persons\n";
>
> $sth->execute;
> $sth->finish;
>
> $dbh->disconnect;
You could first do a 'select', then display the message of
your choice if it finds a value, or wrap all of your code in an
eval {} and take a look at the value of $@ and display an
appropriate message.
Also, your prepare() might be better with placeholders.
perldoc -f eval
------------------------------
Date: 3 Apr 2003 14:28:03 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: DBI webserver reports error 503 when SQL-statement returns error.
Message-Id: <3e8cb573@news.victoria.tc.ca>
stig (nospam_stigerikson@yahoo.se) wrote:
: hi
: in a script running on a webserver, doing UPDATE on a table with a column
: that has the property UNIQUE set, when a user tries to change one email
: address to an email address that already exists, insted of feching the
: error the error kills the process and the webserver reports error 503 to
: the user, not very nice.
: here is the code that is supposed to update the email address.
: i would like to fetch the error somehow so that i can report to the user
: that this email is already existing.
: how should i do to fetch the error?
: ---START CODE---
: my $dbh = DBI->connect("DBI:mysql:someDB:localhost:3306", '?', '?',
: { RaiseError => 1, AutoCommit => 1 });
Look up the docs of the "RaiseError" option in connect.
I forget which, but it either causes the code to die on an error (directly
causing your problem), or it automatically prints a warning message (which
would mess up your HTTP headers and annoy the server, and thus indirectly
cause your problem).
: my $sth = $dbh->prepare("UPDATE persons SET email=" .
: $dbh->quote($personEmail) . " " .
: "WHERE personID=" . $dbh->quote($personID) )
: or warn "\ncannot do UPDATE on persons\n";
:
: $sth->execute;
: $sth->finish;
: $dbh->disconnect;
Somewhere in there you could check the DBI error message, which might be
$dbh->err() or $DBI::Errstr or such like (perldoc DBI will tell you).
------------------------------
Date: Thu, 03 Apr 2003 21:17:42 +0200
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: File more recent than 7 days ?
Message-Id: <pan.2003.04.03.19.13.50.377574@aursand.no>
On Thu, 03 Apr 2003 16:03:03 +0000, Jodyman wrote:
> #!c:/perl/bin/perl.exe
> use warnings;
> use diagnostics;
Use forgot the most important one;
use strict;
> [...]
> PS - If anyone knows a more elegant way of doing the above, feel free
> to rip it up and show me a faster/better way.
The code below is a bit more compact. It doesn't do excactly the same
thing as your script, and definitely not what the OP is after;
#!/usr/bin/perl
#
use strict;
use warnings;
my $today = time;
my %dates = ('Today' => $today,
'Yesterday' => $today - 86_400,
'One week ago' => $today - ( 7 * 86_400 ));
foreach ( keys %dates ) {
my ($y, $m, $d) = (localtime($dates{$_}))[5,4,3];
$year += 1900;
$month++;
print "$_ was " . sprintf("%4d-%02d-%02d", ($y, $m, $d)) . "\n";
}
--
Tore Aursand <tore@aursand.no>
"You know the world is going crazy when the best rapper is white, the best
golfer is black, France is accusing US of arrogance and Germany doesn't
want to go to war."
------------------------------
Date: Thu, 03 Apr 2003 20:14:21 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: File more recent than 7 days ?
Message-Id: <3E8C9616.8922ED56@acm.org>
Chris Lowth wrote:
>
> Michael Budash wrote:
>
> > In article <v8nfbed3dkqhfe@corp.supernews.com>,
> > "Michael Hill" <hillmw@charter.net> wrote:
> >
> >> I have a need to determine if my file is less than 7 days old. Below I
> >> find the date on the file, but don't really know how to see it it is less
> >> tahn 7 days from today.
> >>
> >> Anyone done this before?
> >>
> >> use POSIX 'strftime';
> >> use File::stat;
> >> my $selected = "/home/usr/file.cgi";
> >> my $st = stat($selected) or die "Can't stat($selected): $!\n";
> >> my $modstr = strftime('%b %d %Y', localtime($st->mtime));
> >>
> >> #here i need to determine it its less than 7 days.
> >
> > i usually use:
> >
> > if (-M $thefile > 7) {
> > # $thefile is older than 7 days
> > }
>
> "-M" gives the file age at the time the script started. If this doesnt work
> for you (ie, the script runs long-term as a daemon or some such), then
> try..
>
> [not tested]
>
> my @st = stat($filename);
> if ( (time() - $st[9]) > (7*24*60*60) ) {
> print "File is 7 or more days old\n";
> }
Or set $^T to the current time:
$^T = time;
if ( -M $thefile > 7 ) {
# $thefile is older than 7 days
}
John
--
use Perl;
program
fulfillment
------------------------------
Date: Fri, 4 Apr 2003 08:09:07 +1000
From: "Tintin" <me@privacy.net>
Subject: Re: File more recent than 7 days ?
Message-Id: <b6ibe5$5h2vp$1@ID-172104.news.dfncis.de>
"Jodyman" <Jodyman@hotmail.com> wrote in message
news:XWYia.7146$4P1.559587@newsread2.prod.itd.earthlink.net...
> "Michael Hill" <hillmw@charter.net> wrote in message
>
> > I have a need to determine if my file is less than 7 days old. Below I
> find
> > the date on the file, but don't really know how to see it it is less
tahn
> 7
> > days from today.
>
> I use this all the time, it might help you.
>
> #!c:/perl/bin/perl.exe
> use warnings;
> use diagnostics;
> use Date::Calc qw(Add_Delta_Days);
>
> my ($mo, $day, $yr, $new_year, $new_month, $new_day, $delta_days, $today,
> $previous);
> $mo =
>
("01","02","03","04","05","06","07","08","09","10","11","12")[(localtime)[4]
> ];
> $day = (qw\00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
21
> 22 23 24 25 26 27 28 29 30 31\)[(localtime)[3]];
> $yr = substr(localtime,22,4);
>
> $delta_days = -7;
>
> ($new_year, $new_month, $new_day) = Add_Delta_Days($yr, $mo, $day,
> $delta_days);
> $_ = sprintf "%02d", $_ for $new_month, $new_day, $new_year; # Normalize 2
> digit
>
> #$today = "$mo-$day-$yr"; # WIN98
> #$previous = "$new_month-$new_day-$new_year"; # WIN98
> $today = "$mo/$day/$yr"; # WINNT
> $previous = "$new_month/$new_day/$new_year"; # WINNT
>
> print "Today is: $today, Seven Days ago was: $previous\n";
>
> PS - If anyone knows a more elegant way of doing the above, feel free to
rip
> it up
> and show me a faster/better way.
#!/usr/bin/perl
use strict;
use Date::Manip;
print UnixDate("today","Using ambigous date formats, today is %m/%d/%Y, ");
my $previous = DateCalc("today","7 days ago");
print UnixDate($previous,"Seven days ago was: %m/%d/%Y\n");
------------------------------
Date: Thu, 03 Apr 2003 22:10:33 GMT
From: "Jodyman" <Jodyman@hotmail.com>
Subject: Re: File more recent than 7 days ?
Message-Id: <tj2ja.6853$ey1.545368@newsread1.prod.itd.earthlink.net>
"Tore Aursand" <tore@aursand.no> wrote in message
> On Thu, 03 Apr 2003 16:03:03 +0000, Jodyman wrote:
[snip]
> The code below is a bit more compact. It doesn't do excactly the same
> thing as your script, and definitely not what the OP is after;
>
> #!/usr/bin/perl
> #
> use strict;
> use warnings;
>
> my $today = time;
> my %dates = ('Today' => $today,
> 'Yesterday' => $today - 86_400,
> 'One week ago' => $today - ( 7 * 86_400 ));
>
> foreach ( keys %dates ) {
> my ($y, $m, $d) = (localtime($dates{$_}))[5,4,3];
> $year += 1900;
> $month++;
> print "$_ was " . sprintf("%4d-%02d-%02d", ($y, $m, $d)) . "\n";
> }
Tore, I get the following errors with your example:
Global symbol "$year" requires explicit package name at lastweek.pl line 13.
Global symbol "$month" requires explicit package name at lastweek.pl line
14.
Execution of lastweek.pl aborted due to compilation errors.
Ah strict, get ya everytime.
Jody
------------------------------
Date: 3 Apr 2003 09:02:24 -0800
From: evolutionx1945@yahoo.com (evolutionx1945@yahoo.com)
Subject: Formatting output in columns:
Message-Id: <7566e799.0304030902.692f52de@posting.google.com>
I thouroughly searched newsgroups and faq, but I couldn't find the
answer to my question, although I saw references that led me
to believe that it can be easily done.
I have following array:
my @array=(str1, str2, str3, str4, str5, str6, str7,
str8, str9, str12 ...)
I need to format output text in following fashion:
str1 str5 str9 ...
str2 str6 str10
str3 str7 str11
str4 str8 str12
How can i do that in perl?
thank you very much!!!
------------------------------
Date: Thu, 03 Apr 2003 17:39:08 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Formatting output in columns:
Message-Id: <mbudash-9C50FD.09390603042003@typhoon.sonic.net>
In article <7566e799.0304030902.692f52de@posting.google.com>,
evolutionx1945@yahoo.com (evolutionx1945@yahoo.com) wrote:
> I thouroughly searched newsgroups and faq, but I couldn't find the
> answer to my question, although I saw references that led me
> to believe that it can be easily done.
>
> I have following array:
>
> my @array=(str1, str2, str3, str4, str5, str6, str7,
> str8, str9, str12 ...)
>
> I need to format output text in following fashion:
>
> str1 str5 str9 ...
> str2 str6 str10
> str3 str7 str11
> str4 str8 str12
>
>
> How can i do that in perl?
>
> thank you very much!!!
i'm sure there's modules to do that, but in straight perl, here's one
way: (i've made some assumptions, but it compiles and runs)
my $rows = 5;
my @array = qw/str1 str2 str3 str4 str5 str6
str7 str8 str9 str10 str11 str12/;
my $cols = @array/$rows;
# calc max col width
my $max;
foreach (@array) {
$max = length($_) unless $max > length($_);
}
# allow for margins between columns
my $colwidth = $max + 2;
# print it!
for my $r (0..($rows - 1)) {
last unless $array[$r];
for my $c (0..$cols) {
printf "%-${colwidth}s", $array[$r + ($rows * $c)];
}
print "\n";
}
yields:
str1 str6 str11
str2 str7 str12
str3 str8
str4 str9
str5 str10
hth-
--
Michael Budash
------------------------------
Date: Thu, 03 Apr 2003 21:17:42 +0200
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Formatting output in columns:
Message-Id: <pan.2003.04.03.19.17.05.377371@aursand.no>
On Thu, 03 Apr 2003 09:02:24 -0800, evolutionx1945@yahoo.com wrote:
> I thouroughly searched newsgroups and faq, but I couldn't find the
> answer to my question
I don't believe you. 'perldoc -q format' gives you _excactly_ what you're
after.
> I have following array:
>
> my @array=(str1, str2, str3, str4, str5, str6, str7,
> str8, str9, str12 ...)
No you don't.
--
Tore Aursand <tore@aursand.no>
"You know the world is going crazy when the best rapper is white, the best
golfer is black, France is accusing US of arrogance and Germany doesn't
want to go to war."
------------------------------
Date: 3 Apr 2003 09:40:34 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: How to use newlines in DOS command options
Message-Id: <3e8c7212@news.victoria.tc.ca>
Gian-Reto Alig (gian-reto.alig@gmx.ch) wrote:
: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones) wrote in message news:<3e8b5627@news.victoria.tc.ca>...
: > Benjamin Goldberg (goldbb2@earthlink.net) wrote:
: > : Gian-Reto Alig wrote:
: [snip]
: > : > Is there any way to use newlines in a commandline option in DOS?
: >
: > : I'm afraid not. It's not perl which is changing your newlines, it's the
: > : operating system itself. You simply cannot get newlines into arguments
: > : on windows. (Maybe NT or Win2000, I don't know, but definitely not on
: > : Win95).
: >
: > : > Is it possible using the Win32::Process module?
: >
: > : I don't think so.
: >
: > : #######
: >
: > : What I suspect you will need to do, is get your tool to read attributes
: > : from stdin, or from a file, instead of from the commandline.
: >
: > You might be able to use another shell. Cygwin comes with a small shell
: > called sh, and you can run it as an independent program as long as the
: > right .dll is also in the path (i.e. you do not need to "install" cygwin
: > to use it).
: Sounds interesting... will it run with Win NT 4.0?
It ran on NT, though I can't test the newlines in commands anymore, it
does it fine on XP
D:\malcolm>sh
$ perl -e 'print "hello
> world"'
hello
world$
$ exit
D:\malcolm>where sh
f:\gnu (F:\GNU)
SH.EXE ----- 68,608 12-01-98 05:55p
It came with the cygwin install of that time period.
------------------------------
Date: Thu, 03 Apr 2003 14:31:53 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: implementing a threaded serial device (or any other parallelway)
Message-Id: <3E8C8C29.C05D3BCF@earthlink.net>
Alexander Eisenhuth wrote:
[snip]
> my $comObj = bless {running=>1}, $packageName;
This creates a non-empty hash.
> threads::shared::share $comObj;
This is roughly eqivilant to:
tie(%$comOjb, "threads::shared");
As with all tie()s, any data previously in there is gone.
Well, not *gone*, but rather hidden and inaccessible until the thing is
untie()d... but since there's no "unshare" function, you won't get the
hidden data back.
So the data you had in the hash before (running => 1) is gone.
> $comObj->{$watchThread} =
> create threads \&watchDevice, $comObj; # line 11
[snip]
> output:
> Invalid value for shared scalar at developThreadInPerlWork.pm line 11.
Since $comObj is a shared hashref, you can only store either scalar
values (undefs or strings or ints), or shared references (shared
scalarrefs, shared hashrefs, or scalar arrayrefs) into it.
That's why you should only store the thread id (which is a scalar, an
integer), not the thread object (a non-shared reference), there.
my $watchThread = threads->create(\&watchDevice, $comObj);
$comObj->{watchThread} = $watchThread->tid;
> A thread exited while 2 other threads were still running.
Some thread called the exit() function (or the main thread die()d or
reached __END__) while other threads were still alive.
That "Invalid value..." is an error (not warning) message, so in this
case, the problem is that the main thread die()d.
--
$a=24;split//,240513;s/\B/ => /for@@=qw(ac ab bc ba cb ca
);{push(@b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$@[$a%6
]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop @b))&&redo;}
------------------------------
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 4811
***************************************