[32258] in Perl-Users-Digest
Perl-Users Digest, Issue: 3525 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 21 18:09:30 2011
Date: Fri, 21 Oct 2011 15:09:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 21 Oct 2011 Volume: 11 Number: 3525
Today's topics:
deisng perl perl programmibng langs in perl <r@thevoid1.net>
Re: Does plack work with perlbal? <peter@makholm.net>
Issue with Time::Local <sssdevelop@gmail.com>
Re: Issue with Time::Local (hymie!)
Re: Issue with Time::Local <sssdevelop@gmail.com>
Re: Issue with Time::Local (hymie!)
Re: Issue with Time::Local (hymie!)
Re: Issue with Time::Local (Jens Thoms Toerring)
Re: Issue with Time::Local (Tony Mountifield)
Re: Issue with Time::Local <ben@morrow.me.uk>
Re: Issue with Time::Local <rweikusat@mssgmbh.com>
Re: Issue with Time::Local <ben@morrow.me.uk>
Re: Learning Perl CGI and OOP, not printing @c <ben@morrow.me.uk>
Re: Learning Perl CGI and OOP, not printing @c <rodbass63@gmail.com>
Re: Questions re using Require <Joey@still_Learning.invalid>
Re: suggest better trick to handle multiple hash keys <sssdevelop@gmail.com>
Re: suggest better trick to handle multiple hash keys <jurgenex@hotmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 21 Oct 2011 08:38:49 -0600
From: robin <r@thevoid1.net>
Subject: deisng perl perl programmibng langs in perl
Message-Id: <j7s05u$nrp$1@speranza.aioe.org>
then liberate big war zones with notes on their download pages.
-r
www.thevoid1.net/
------------------------------
Date: Fri, 21 Oct 2011 09:46:33 +0200
From: Peter Makholm <peter@makholm.net>
Subject: Re: Does plack work with perlbal?
Message-Id: <87hb32iy7a.fsf@vps1.hacking.dk>
gavino <gavcomedy@gmail.com> writes:
> ?
As Ben already wrote, please explain what your needs in the posting a
bit more than just a question mark.
You might want Perlbal::Plugin::PSGI, but I think just having Perlbal in
front of Starman is a quite normal Plack setup.
//Makholm
------------------------------
Date: Fri, 21 Oct 2011 05:29:10 -0700 (PDT)
From: SSS Develop <sssdevelop@gmail.com>
Subject: Issue with Time::Local
Message-Id: <19440d40-21c9-4312-99bb-7e2a7813422e@1g2000vbx.googlegroups.com>
Hello,
When i run the following script, I get error:
Day '31' out of range 1..28 at exp.pl line 18
-----------
use strict;
use warnings;
use Time::Local;
my $date_string = '2011-01-31 17:30:55';
my ($date, $time) = split /\s+/, $string;
my ($y, $m, $d) = split /-/, $date;
my ($hr, $min, $sec) = split /:/, $time;
print timelocal($sec,$min,$hr,$d,$m,$y);
--------------
what could be issue?
------------------------------
Date: 21 Oct 2011 12:51:33 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: Re: Issue with Time::Local
Message-Id: <4ea16ad5$0$14014$882e7ee2@usenet-news.net>
In our last episode, the evil Dr. Lacto had captured our hero,
SSS Develop <sssdevelop@gmail.com>, who said:
>Hello,
>
>When i run the following script, I get error:
>
>
>Day '31' out of range 1..28 at exp.pl line 18
>
>
>
>-----------
>use strict;
>use warnings;
>use Time::Local;
>
>my $date_string = '2011-01-31 17:30:55';
>my ($date, $time) = split /\s+/, $string;
Global symbol "$string" requires explicit package name at - line 6.
Execution of - aborted due to compilation errors.
--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
-------------------------------------------------------------------------------
------------------------------
Date: Fri, 21 Oct 2011 05:54:27 -0700 (PDT)
From: SSS Develop <sssdevelop@gmail.com>
Subject: Re: Issue with Time::Local
Message-Id: <fbdb36f0-98db-4e03-b116-7b1c555aeb11@y32g2000yqh.googlegroups.com>
On Oct 21, 5:51=A0pm, hy...@lactose.homelinux.net (hymie!) wrote:
> In our last episode, the evil Dr. Lacto had captured our hero,
> =A0 SSS Develop <sssdeve...@gmail.com>, who said:
>
>
>
>
>
>
>
>
>
> >Hello,
>
> >When i run the following script, I get error:
>
> >Day '31' out of range 1..28 at exp.pl line 18
>
> >-----------
> >use strict;
> >use warnings;
> >use Time::Local;
>
> >my $date_string =A0=3D '2011-01-31 17:30:55';
> >my ($date, $time) =3D split /\s+/, $string;
>
> Global symbol "$string" requires explicit package name at - line 6.
> Execution of - aborted due to compilation errors.
>
> --hymie! =A0 =A0http://lactose.homelinux.net/~hymie=A0 =A0hy...@lactose.h=
omelinux.net
> -------------------------------------------------------------------------=
-- ----
opps... the correct version is as follow:
------------
use strict;
use warnings;
use Time::Local;
my $date_string =3D '2011-01-31 17:30:55';
my ($date, $time) =3D split /\s+/, $date_string;
my ($y, $m, $d) =3D split /-/, $date;
my ($hr, $min, $sec) =3D split /:/, $time;
print timelocal($sec,$min,$hr,$d,$m,$y);
---------
------------------------------
Date: 21 Oct 2011 12:54:48 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: Re: Issue with Time::Local
Message-Id: <4ea16b98$0$10313$882e7ee2@usenet-news.net>
In our last episode, the evil Dr. Lacto had captured our hero,
SSS Develop <sssdevelop@gmail.com>, who said:
>Hello,
>
>When i run the following script, I get error:
>
>
>Day '31' out of range 1..28 at exp.pl line 18
>my $date_string = '2011-01-31 17:30:55';
>my ($date, $time) = split /\s+/, $string;
>
>my ($y, $m, $d) = split /-/, $date;
>my ($hr, $min, $sec) = split /:/, $time;
>
>print timelocal($sec,$min,$hr,$d,$m,$y);
>
>what could be issue?
You mean, aside from the obvious problem that February 2011 only has
28 days in it?
--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
-------------------------------------------------------------------------------
------------------------------
Date: 21 Oct 2011 12:57:23 GMT
From: hymie@lactose.homelinux.net (hymie!)
Subject: Re: Issue with Time::Local
Message-Id: <4ea16c33$0$4420$882e7ee2@usenet-news.net>
In our last episode, the evil Dr. Lacto had captured our hero,
hymie@lactose.homelinux.net (hymie!), who said:
>In our last episode, the evil Dr. Lacto had captured our hero,
> SSS Develop <sssdevelop@gmail.com>, who said:
>>Hello,
>>
>>When i run the following script, I get error:
>>
>>
>>Day '31' out of range 1..28 at exp.pl line 18
>
>>my $date_string = '2011-01-31 17:30:55';
>>my ($date, $time) = split /\s+/, $string;
>>
>>my ($y, $m, $d) = split /-/, $date;
>>my ($hr, $min, $sec) = split /:/, $time;
>>
>>print timelocal($sec,$min,$hr,$d,$m,$y);
>>
>>what could be issue?
>
>You mean, aside from the obvious problem that February 2011 only has
>28 days in it?
I must have undo'd the last line of my post, which said something like:
Or that you didn't read the documentation, which states
>It is worth drawing particular attention to the expected
>ranges for the values provided.
--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
-------------------------------------------------------------------------------
------------------------------
Date: 21 Oct 2011 13:02:38 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Issue with Time::Local
Message-Id: <9gd8reFgrbU1@mid.uni-berlin.de>
SSS Develop <sssdevelop@gmail.com> wrote:
> When i run the following script, I get error:
> Day '31' out of range 1..28 at exp.pl line 18
> -----------
> use strict;
> use warnings;
> use Time::Local;
> my $date_string = '2011-01-31 17:30:55';
> my ($date, $time) = split /\s+/, $string;
-----------------------------------^^^^^^^
Better make that '$date_string'.
> my ($y, $m, $d) = split /-/, $date;
> my ($hr, $min, $sec) = split /:/, $time;
> print timelocal($sec,$min,$hr,$d,$m,$y);
-----------------------------------^^
The month value is interpreted as the distance to January,
so you need 0 for January, 1 for February etc. Thus you're
asking here for February 31 - which isn't a valid date.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Fri, 21 Oct 2011 13:22:25 +0000 (UTC)
From: tony@mountifield.org (Tony Mountifield)
Subject: Re: Issue with Time::Local
Message-Id: <j7rrmh$mds$1@softins.clara.co.uk>
In article <19440d40-21c9-4312-99bb-7e2a7813422e@1g2000vbx.googlegroups.com>,
SSS Develop <sssdevelop@gmail.com> wrote:
> Hello,
>
> When i run the following script, I get error:
>
>
> Day '31' out of range 1..28 at exp.pl line 18
>
> -----------
> use strict;
> use warnings;
> use Time::Local;
>
> my $date_string = '2011-01-31 17:30:55';
> my ($date, $time) = split /\s+/, $string;
>
> my ($y, $m, $d) = split /-/, $date;
> my ($hr, $min, $sec) = split /:/, $time;
>
> print timelocal($sec,$min,$hr,$d,$m,$y);
This line should be:
print timelocal($sec,$min,$hr,$d,$m-1,$y-1900);
Although I expect it will also accept just $y as the last arg.
It's the $m-1 that is critical.
Cheers
Tony
--
Tony Mountifield
Work: tony@softins.co.uk - http://www.softins.co.uk
Play: tony@mountifield.org - http://tony.mountifield.org
------------------------------
Date: Fri, 21 Oct 2011 13:02:27 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Issue with Time::Local
Message-Id: <R76dnaSrGaMuLjzTnZ2dnUVZ7oudnZ2d@bt.com>
Quoth tony@mountifield.org (Tony Mountifield):
> In article <19440d40-21c9-4312-99bb-7e2a7813422e@1g2000vbx.googlegroups.com>,
> SSS Develop <sssdevelop@gmail.com> wrote:
> >
> > When i run the following script, I get error:
> >
> > Day '31' out of range 1..28 at exp.pl line 18
> >
> > -----------
> > use strict;
> > use warnings;
> > use Time::Local;
> >
> > my $date_string = '2011-01-31 17:30:55';
> > my ($date, $time) = split /\s+/, $string;
> >
> > my ($y, $m, $d) = split /-/, $date;
> > my ($hr, $min, $sec) = split /:/, $time;
It's usually a mistake to try to parse date formats yourself. Use
DateTime instead.
> > print timelocal($sec,$min,$hr,$d,$m,$y);
>
> This line should be:
>
> print timelocal($sec,$min,$hr,$d,$m-1,$y-1900);
>
> Although I expect it will also accept just $y as the last arg.
> It's the $m-1 that is critical.
This is documented in Time::Local. Years >=1000 are absolute, years
<1000 (and <0, though this isn't explicitly documented) are an offset
from 1900, years <100 are no more than 50 years away from now. This
means that if you want to reliably supply an arbitrary year value, you
need to use
($y < 1900 ? $y - 1900 : $y)
which is rather awkward. Of course, if you want to represent dates
outside the range Dec 1901 to Jan 2038 you need 5.12 or an OS with a
64-bit time_t.
Ben
------------------------------
Date: Fri, 21 Oct 2011 19:14:44 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Issue with Time::Local
Message-Id: <871uu6jjor.fsf@sapphire.mobileactivedefense.com>
Ben Morrow <ben@morrow.me.uk> writes:
> Quoth tony@mountifield.org (Tony Mountifield):
>> In article <19440d40-21c9-4312-99bb-7e2a7813422e@1g2000vbx.googlegroups.com>,
>> SSS Develop <sssdevelop@gmail.com> wrote:
>> >
>> > When i run the following script, I get error:
>> >
>> > Day '31' out of range 1..28 at exp.pl line 18
>> >
>> > -----------
>> > use strict;
>> > use warnings;
>> > use Time::Local;
>> >
>> > my $date_string = '2011-01-31 17:30:55';
>> > my ($date, $time) = split /\s+/, $string;
>> >
>> > my ($y, $m, $d) = split /-/, $date;
>> > my ($hr, $min, $sec) = split /:/, $time;
>
> It's usually a mistake to try to parse date formats yourself. Use
> DateTime instead.
Why would this 'usually be a mistake'?
------------------------------
Date: Fri, 21 Oct 2011 15:17:44 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Issue with Time::Local
Message-Id: <zNCdnXPhVvv1TjzTnZ2dnUVZ8i6dnZ2d@bt.com>
Quoth Rainer Weikusat <rweikusat@mssgmbh.com>:
> Ben Morrow <ben@morrow.me.uk> writes:
> >
> > It's usually a mistake to try to parse date formats yourself. Use
> > DateTime instead.
>
> Why would this 'usually be a mistake'?
Because, as this thread has demonstrated, it's extremely easy to get it
wrong.
Ben
------------------------------
Date: Thu, 20 Oct 2011 20:26:38 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Learning Perl CGI and OOP, not printing @c
Message-Id: <LLmdnfdILJjTVz3TnZ2dnUVZ8iudnZ2d@bt.com>
Quoth Nene <rodbass63@gmail.com>:
> On Oct 20, 4:33 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Nene <rodbas...@gmail.com>:
> >
> > > foreach my $line (@values) {
> > > my $line =~ s/$_/$line:a/;
> >
> > This doesn't do what you expect. 'my $line' allocates a new variable, so
> > the s/// will always be applied to the empty string. (If you'd had
> > warnings on you would have got an uninitialised value warning, thereby
> > possibly proving that my hatred of that particular warning is
> > unjustified.)
>
> This chuck of code is actually doing what it's intended to do: my
> $line =~ s/$_/$line:a/
Goodness me, you're right, it's actually got so many levels of Wrong it
comes out giving the right answer. However, what that piece of code does
is:
- allocate a variable called $line to hold the current item from
@values,
- allocate a second variable, *also* called $line, which is
initialized to undef,
- match the current value of $_ against that new variable: since $_
happens to be empty and the new $line is undef, it matches,
- replace the matched portion with "$line:a"; but since the second
$line hasn't come into scope yet this uses the *first* $line, so
you get the result you wanted.
I can't imagine how you came by that bit of code, but I'm sure the
sequence of steps above wasn't what you intended.
If you add '$_ = "x";' above the loop you will see it breaks everything,
because the LHS of the s/// no longer matches the empty string. If you
had turned warnings on you would have seen three uninitialised value
warnings, which should have let you know something was not as you
expected.
What you want is simply
for my $value (@values) {
my $line = "$value:a";
> > > push(@n, "$line");
> >
> > Where do you declare @n? Are you using 'strict'?
>
> Yes, I'm declaring 'strict', the 'my @n;' is at the top of the file.
When posting code for review it's helpful if you can post all the bits
that matter. Ideally you should cut down your program as much as you can
while still leaving the bit that you're having a problem with, then post
all of what's left.
> > Why are you stringifying $line? You don't need to.
>
> Again, it's doing what it's suppose to do--is it bad?, for example:
> 10.223.22.66:a 10.223.22.67:a 10.223.22.68:a 10.223.22.69:a
Jue has already pointed you to the FAQ for this one.
> > > sub setPse {
> > > my ( $self, @pse ) = @_;
> > > $self->{_pse} = @pse if defined(@pse);
> >
> > Don't apply defined to an array. It doesn't return a useful answer. The
> > condition you want is 'if @pse', which tells you if @pse has any
> > elements.
>
> Ok, but I used this example from a tutorial.
Most Perl tutorials are complete rubbish. Stick to the docs which come
with perl and books written by reputable authors. (The list in perldoc
-q book is a good place to start, if you're looking for a book.)
> > > sub setNodes {
> > > my ( $self, $nodes ) = @_;
> > > $self->{_nodes} = $pse if defined($nodes);
> >
> > Where does $pse come from? Assuming you meant $nodes, you are calling
> > ->setNodes with a list, but only using the first value in that list. If
> > you want to store the whole list you need
>
> Again, a typo. I changed the variables a little so that my boss won't
> catch me at Google groups :-)
This isn't Google Groups, it's Usenet. It's been here a lot longer than
Google, not to mention the Web.
I'm not really interested in the politics between you and your boss, but
until you know a bit more about what you are doing please don't post
code you haven't actually run. It just wastes everybody's time, which is
rather rude when you're asking for help.
Ben
------------------------------
Date: Fri, 21 Oct 2011 13:03:34 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Re: Learning Perl CGI and OOP, not printing @c
Message-Id: <af8bf846-807a-4b92-8698-781dffff5326@g16g2000yqa.googlegroups.com>
On Oct 20, 9:26=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Nene <rodbas...@gmail.com>:
>
> > On Oct 20, 4:33=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > > Quoth Nene <rodbas...@gmail.com>:
>
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach my $line (@value=
s) {
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0my $line =3D~ s/$_/$line=
:a/;
>
> > > This doesn't do what you expect. 'my $line' allocates a new variable,=
so
> > > the s/// will always be applied to the empty string. (If you'd had
> > > warnings on you would have got an uninitialised value warning, thereb=
y
> > > possibly proving that my hatred of that particular warning is
> > > unjustified.)
>
> > This chuck of code is actually doing what it's intended to do: my
> > $line =3D~ s/$_/$line:a/
>
> Goodness me, you're right, it's actually got so many levels of Wrong it
> comes out giving the right answer. However, what that piece of code does
> is:
>
> =A0 =A0 - allocate a variable called $line to hold the current item from
> =A0 =A0 =A0 @values,
> =A0 =A0 - allocate a second variable, *also* called $line, which is
> =A0 =A0 =A0 initialized to undef,
> =A0 =A0 - match the current value of $_ against that new variable: since =
$_
> =A0 =A0 =A0 happens to be empty and the new $line is undef, it matches,
> =A0 =A0 - replace the matched portion with "$line:a"; but since the secon=
d
> =A0 =A0 =A0 $line hasn't come into scope yet this uses the *first* $line,=
so
> =A0 =A0 =A0 you get the result you wanted.
Thank you for the detailed explaination.
>
> I can't imagine how you came by that bit of code, but I'm sure the
> sequence of steps above wasn't what you intended.
>
> If you add '$_ =3D "x";' above the loop you will see it breaks everything=
,
> because the LHS of the s/// no longer matches the empty string. If you
> had turned warnings on you would have seen three uninitialised value
> warnings, which should have let you know something was not as you
> expected.
>
> What you want is simply
>
> =A0 =A0 for my $value (@values) {
> =A0 =A0 =A0 =A0 my $line =3D "$value:a";
Thank you for cleaning up my code.
>
> > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0push(@n, "$line");
>
> > > Where do you declare @n? Are you using 'strict'?
>
> > Yes, I'm declaring 'strict', the 'my @n;' is at the top of the file.
>
> When posting code for review it's helpful if you can post all the bits
> that matter. Ideally you should cut down your program as much as you can
> while still leaving the bit that you're having a problem with, then post
> all of what's left.
>
> > > Why are you stringifying $line? You don't need to.
>
> > Again, it's doing what it's suppose to do--is it bad?, for example:
> > 10.223.22.66:a 10.223.22.67:a 10.223.22.68:a 10.223.22.69:a
>
> Jue has already pointed you to the FAQ for this one.
>
> > > > sub setPse {
> > > > =A0 =A0 my ( $self, @pse ) =3D @_;
> > > > =A0 =A0 $self->{_pse} =3D @pse if defined(@pse);
>
> > > Don't apply defined to an array. It doesn't return a useful answer. T=
he
> > > condition you want is 'if @pse', which tells you if @pse has any
> > > elements.
>
> > Ok, but I used this example from a tutorial.
>
> Most Perl tutorials are complete rubbish. Stick to the docs which come
> with perl and books written by reputable authors. (The list in perldoc
> -q book is a good place to start, if you're looking for a book.)
>
> > > > sub setNodes {
> > > > =A0 =A0 my ( $self, $nodes ) =3D @_;
> > > > =A0 =A0 $self->{_nodes} =3D $pse if defined($nodes);
>
> > > Where does $pse come from? Assuming you meant $nodes, you are calling
> > > ->setNodes with a list, but only using the first value in that list. =
If
> > > you want to store the whole list you need
>
> > Again, a typo. I changed the variables a little so that my boss won't
> > catch me at Google groups :-)
>
> This isn't Google Groups, it's Usenet. It's been here a lot longer than
> Google, not to mention the Web.
Thank you for clarifying.
>
> I'm not really interested in the politics between you and your boss, but
> until you know a bit more about what you are doing please don't post
> code you haven't actually run. It just wastes everybody's time, which is
> rather rude when you're asking for help.
I did run the code many times, I try not post until I'm really stuck.
However, if I have posted both the
package and the script, it would make your job easier. I have learned,
thank you.
>
> Ben
------------------------------
Date: Thu, 20 Oct 2011 18:12:58 -0700
From: "Joey@still_Learning.invalid" <Joey@still_Learning.invalid>
Subject: Re: Questions re using Require
Message-Id: <nnh1a7dv4ekkg8jp3u7pcueqf4horb78jf@4ax.com>
Rainer Weikusat wrote:
>"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:
>> 1. Do I put the 'require' statement in the script at the place I want to
>> import the code, or can it be placed at the start of the script?
>
>It can be placed at the start of the script and IMO, it should also be
>placed there: It's easier to determine what other files will be used
>when some code is going to be executed when 'inclusion of other files'
>always happens at the same, prominent location. Putting require into a
>BEGIN block so that it is processed at the same time all the other
>code passes through the compiler (or using use instead of require)
>should also be considered.
>
>> 2. Two of the three pages print the contents to the screen (e.g., print
>> "content\n";), but the third page prints the contents to a file, e.g.,
>> print tfile "content\n", although some of the third page also prints to
>> the screen.
>>
>> Is it somehow possible to use Require for all three pages? How do I
>> account for writing content to the file?
>
>Two ways to do this which suggest themselves would be
>
> - pass the output file handle as argument, using \*STDOUT for
> printing 'to the screen' (if the code is suitably structured
> for that)
>
> - remove any explicitly specified filehandles from the code
> and use select(FILEHANDLE) to set a suitable 'default output
> filehandle'.
>
Thank you very much.
--
Joey
------------------------------
Date: Thu, 20 Oct 2011 23:31:11 -0700 (PDT)
From: SSS Develop <sssdevelop@gmail.com>
Subject: Re: suggest better trick to handle multiple hash keys
Message-Id: <e5be1d9c-ac03-4ed8-8344-692fcc3b8db6@q13g2000vbd.googlegroups.com>
On Oct 20, 10:32=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth SSS Develop <sssdeve...@gmail.com>:
>
>
>
> > I have =A0customers information into database - the "cust_id" is one of
> > the key. The cust_is not unique.
> > I would like to create the hash as keys "cust_id" - but since they are
> > not unique so problem.
>
> What do these multiple records with the same cust_id represent? If you
> actually have several customers with the same ID, how do you tell them
> apart? If, instead, these multiple records are all referring to the same
> customer, you want to keep them grouped together by customer rather than
> faking a new cust_id.
>
> > I am thinking of appending =A0 "_NUMBER" (example. =A0cust001, cust001_=
0,
> > cust001_1 ) =A0whenever keys repeat.
>
> Whenever you find yourself appending a number to something, like this,
> you should be using an array. Instead of this
>
> =A0 =A0 {
> =A0 =A0 =A0 =A0 cust001 =A0 =A0 =3D> ...,
> =A0 =A0 =A0 =A0 cust001_0 =A0 =3D> ...,
> =A0 =A0 }
>
> you should be using this
>
> =A0 =A0 {
> =A0 =A0 =A0 =A0 cust001 =3D> [
> =A0 =A0 =A0 =A0 =A0 =A0 ...,
> =A0 =A0 =A0 =A0 =A0 =A0 ...,
> =A0 =A0 =A0 =A0 ],
> =A0 =A0 }
>
[ssdevelop] - agree with you. By doing this - i will be forced to more
work in other parts of the code. will definitely think of doing
this.
> <snip>
>
> > =A0#!/usr/bin/perl
> > use strict;
> > use warnings;
>
> > my $s =A0 =A0 =A0 =A0 =3D "cs011";
> > my $customers =3D {};
> > $s =3D generate_key( $s, $customers );
> > print "The generated key is: $s\n";
> > $customers->{$s} =3D 1;
> <snip>
>
> > sub generate_key {
>
> You aren't using the arguments you pass. This happens to work because
> you're expecting the arguments to have the same names as the globals you
> passed, but that's extremely fragile.
>
[sssdevelop] - my mistake, pasted small portion of the code her and
forgot to add argument related part. adding new code here below:
> =A0 =A0 my ($s, $customers) =3D @_;
>
> > =A0 =A0 if ( !defined $customers->{$s} ) { return $s; }
>
> You should probably be using exists rather than defined.
>
> > =A0 =A0 else {
> > =A0 =A0 =A0 =A0 if ( $s =3D~ /(.*)(_)(\d+$)/ ) {
> > =A0 =A0 =A0 =A0 =A0 =A0 my $int =3D $3;
> > =A0 =A0 =A0 =A0 =A0 =A0 $int++;
> > =A0 =A0 =A0 =A0 =A0 =A0 $s =3D $1 . "_" . $int;
> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 =A0 =A0 else { $s .=3D "_0"; }
> > =A0 =A0 =A0 =A0 generate_key( $s, $customers );
>
> Going round and round joining up a string and breaking it apart again is
> rather a waste of time. Try
>
> =A0 =A0 =A0 =A0 my $int =3D 0;
> =A0 =A0 =A0 =A0 while (exists $customers->{"$s_$int"}) {
> =A0 =A0 =A0 =A0 =A0 =A0 $int++;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 =A0 =A0 $s .=3D "_$int";
>
> > =A0 =A0 }
>
[sssdevelop] - helps, this is helpful suggestion.
> You don't return $s either: the calling code is relying on the sub to
> modify the global $s.
>
> Ben
[sssdevelop] new code looks as below:
-----------------------
#!/usr/bin/perl
use strict;
use warnings;
my $s =3D "cs011";
my $customers =3D {};
$s =3D generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} =3D 1;
$s =3D "cs011"; # Resetng key back to cs001
$s =3D generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} =3D 2;
$s =3D "cs011"; # Resetng key back to cs001
$s =3D generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} =3D 3;
$s =3D "cs011"; # Resetng key back to cs001
$s =3D generate_key( $s, $customers );
print "The generated key is: $s\n";
sub generate_key {
my ($string, $custhash) =3D @_;
return $string unless defined $custhash->{$string};
my $int =3D 0;
while (exists $customers->{$string . "_" . $int}) {
$int++;
}
return $string .=3D "_" . $int;
}
-------------
------------------------------
Date: Fri, 21 Oct 2011 06:19:50 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: suggest better trick to handle multiple hash keys
Message-Id: <3vr2a71dgu6gb1i1manlchad2ru0u6ulq5@4ax.com>
SSS Develop <sssdevelop@gmail.com> wrote:
>On Oct 20, 10:32 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> > I am thinking of appending "_NUMBER" (example. cust001, cust001_0,
>> > cust001_1 ) whenever keys repeat.
>>
>> Whenever you find yourself appending a number to something, like this,
>> you should be using an array. Instead of this
>
>[ssdevelop] - agree with you. By doing this - i will be forced to more
>work in other parts of the code.
Actually that is not true because an array allows you to index the
values. Using individual variables you have to write
DoSomeThingWith $cust001;
DoSomeThingWith $cust001_1;
DoSomeThingWith $cust001_2;
DoSomeThingWith $cust001_3;
DoSomeThingWith $cust001_4;
DoSomeThingWith $cust001_5;
....
while when using an array you can use a short
foreach (@cust) {
doDoSomeThingWith $_
}
and you don't even have to modify a single line when adding another
element
jue
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3525
***************************************