[32257] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3524 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 20 21:09:30 2011

Date: Thu, 20 Oct 2011 18:09:11 -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           Thu, 20 Oct 2011     Volume: 11 Number: 3524

Today's topics:
        Does plack work with perlbal? <gavcomedy@gmail.com>
    Re: Does plack work with perlbal? <ben@morrow.me.uk>
    Re: Imager module to resize a GIF <jwcarlton@gmail.com>
    Re: Imager module to resize a GIF <rweikusat@mssgmbh.com>
    Re: Imager module to resize a GIF <glex_no-spam@qwest-spam-no.invalid>
    Re: Imager module to resize a GIF <ben@morrow.me.uk>
        Learning Perl CGI and OOP, not printing @c <rodbass63@gmail.com>
    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: Learning Perl CGI and OOP, not printing @c <jurgenex@hotmail.com>
        suggest better trick to handle multiple hash keys <sssdevelop@gmail.com>
    Re: suggest better trick to handle multiple hash keys <glex_no-spam@qwest-spam-no.invalid>
    Re: suggest better trick to handle multiple hash keys <ben@morrow.me.uk>
    Re: Tricky STDERR issue under windows philkime@kime.org.uk
    Re: Tricky STDERR issue under windows philkime@kime.org.uk
    Re: Tricky STDERR issue under windows <ben@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 20 Oct 2011 06:34:13 -0700 (PDT)
From: gavino <gavcomedy@gmail.com>
Subject: Does plack work with perlbal?
Message-Id: <30425700.729.1319117653076.JavaMail.geo-discussion-forums@prlk36>

?


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

Date: Thu, 20 Oct 2011 10:09:24 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Does plack work with perlbal?
Message-Id: <m-ydnY-n7cU5pD3TnZ2dnUVZ8k-dnZ2d@bt.com>

Please write something in the body of your articles.

Quoth comp.lang.perl.misc@googlegroups.com:
> [Does plack work with perlbal]?

'Work' in what sense? If you're using perlbal as a reverse proxy or load
balancer, it just talks HTTP to the backends; there are several
Plack::Engines which will serve HTTP, with different performance
characteristics.

If you want to do something else, you will need to say what.

Ben



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

Date: Wed, 19 Oct 2011 17:47:38 -0700 (PDT)
From: Jason C <jwcarlton@gmail.com>
Subject: Re: Imager module to resize a GIF
Message-Id: <1399606.297.1319071658699.JavaMail.geo-discussion-forums@yqjh13>

On Wednesday, October 19, 2011 8:20:50 AM UTC-4, Ben Morrow wrote:
> What you have there works for me, here. Do you, in fact, have a .pm and
> a .so for Imager::File::GIF? If you run ldd against the .so (which
> should be somewhere like /usr/local/lib/perl5/site_perl/5.12.4/
> amd64-freebsd/auto/Imager/File/GIF/GIF.so) are all the libraries it
> needs found?
>=20
> Is there something wrong with your gif file? There is a file called
> GIF/testimg/expected.gif in the Imager distribution; since that's the
> file Imager uses to make sure giflib works, you could try that (it
> should be 16x16px).
>=20
> Ben

You're right, it's more likely that there's something missing on the server=
 . I thought that the Imager package would include everything it needed, but=
 I guess not.

I don't see Imager::File::GIF in my list of installed modules (from WHM/cPa=
nel), so I tried to install it manually. The install failed with:

perl-64bit-hack: enabled
GIF: building independently
GIF: main: includes not found - libraries not found
GIF: Checking if the compiler can find them on its own
GIF: Test code failed: Can't link/include 'gif_lib.h', 'stdio.h', 'gif'
OS unsupported: GIF libraries or headers not found
No 'Makefile' created  TONYC/Imager-File-GIF-0.81.tar.gz
  /usr/local/cpanel/scripts/cpan_sandbox/x86_64/perl Makefile.PL OTHERLDFLA=
GS=3D-L/usr/lib64 LDFLAGS=3D-L/usr/lib64 EXTRALIBDIR=3D/usr/lib64 -- NOT OK
Running make install
  Make had some problems, won't install
Could not read metadata file. Falling back to other methods to determine pr=
erequisites
perlmod--Install done


I also tried to install it via SSH, like so:

perl -MCPAN -e shell;
install Imager::File::GIF

And received the same error. I even tried:

force install Imager::File::GIF

With the same result.

Interestingly, my modules go to /usr/lib/perl5/site_perl/5.8.8, but I don't=
 see Imager installed there at all! And I definitely don't have Imager/File=
/GIF. Imager is working, though, so it exists somewhere... I just don't kno=
w where. Could this be why it's failing?


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

Date: Thu, 20 Oct 2011 14:02:55 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: Imager module to resize a GIF
Message-Id: <87botbn7cw.fsf@sapphire.mobileactivedefense.com>

Jason C <jwcarlton@gmail.com> writes:

[...]

> I don't see Imager::File::GIF in my list of installed modules (from WHM/cPanel), so I tried to install it manually. The install failed with:
>
> perl-64bit-hack: enabled
> GIF: building independently
> GIF: main: includes not found - libraries not found
> GIF: Checking if the compiler can find them on its own
> GIF: Test code failed: Can't link/include 'gif_lib.h', 'stdio.h',
> 'gif'

According to

http://www.google.com/#hl=en&sugexp=kjrmc&cp=9&gs_id=3&xhr=t&q=gif_lib.h&pf=p&sclient=psy-ab&site=&source=hp&pbx=1&oq=gif_lib.h&aq=0&aqi=g1g-v3&aql=f&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=b24facda13a68307&biw=1537&bih=858

this should mean that you're either missing the giflib library,

	http://giflib.sourceforge.net

or (if you are using a package provided by some Linux distribution)
the corresponding 'development' package which would usually include
the headers (since disks are so tiny nowadays and just keep shrinking
and shrinking, Linux distributors have commonly chosen that the
overhead of having a bunch of possibly unused text files installed
would be too much for 'end users' to bear, given that only relatively
few of them will likely ever compile something on their computers
 ...).


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

Date: Thu, 20 Oct 2011 14:55:52 +0000
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Imager module to resize a GIF
Message-Id: <4ea03678$0$73603$815e3792@news.qwest.net>

On 10/20/11 00:47, Jason C wrote:
> On Wednesday, October 19, 2011 8:20:50 AM UTC-4, Ben Morrow wrote:
>> What you have there works for me, here. Do you, in fact, have a .pm and
>> a .so for Imager::File::GIF? If you run ldd against the .so (which
>> should be somewhere like /usr/local/lib/perl5/site_perl/5.12.4/
>> amd64-freebsd/auto/Imager/File/GIF/GIF.so) are all the libraries it
>> needs found?
>>
>> Is there something wrong with your gif file? There is a file called
>> GIF/testimg/expected.gif in the Imager distribution; since that's the
>> file Imager uses to make sure giflib works, you could try that (it
>> should be 16x16px).
>>
>> Ben
>
> You're right, it's more likely that there's something missing on the server. I thought that the Imager package would include everything it needed, but I guess not.
>
> I don't see Imager::File::GIF in my list of installed modules (from WHM/cPanel), so I tried to install it manually. The install failed with:
>
> perl-64bit-hack: enabled
> GIF: building independently
> GIF: main: includes not found - libraries not found
                                   ^^^^^^^^^^^^^^^^^^^
> GIF: Checking if the compiler can find them on its own
> GIF: Test code failed: Can't link/include 'gif_lib.h', 'stdio.h', 'gif'
> OS unsupported: GIF libraries or headers not found

[...]

You probably need to have the various gif, jpeg, etc. libraries 
installed, or possibly modify your Include path.  See the README
file, which is part of the distribution, or you can view it online:
http://cpansearch.perl.org/src/TONYC/Imager-0.85/README

>
> Interestingly, my modules go to /usr/lib/perl5/site_perl/5.8.8, but I don't see Imager installed there at all! And I definitely don't have Imager/File/GIF. Imager is working, though, so it exists somewhere... I just don't know where. Could this be why it's failing?

To see where the module is located, try: perldoc -l Imager


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

Date: Thu, 20 Oct 2011 10:02:33 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Imager module to resize a GIF
Message-Id: <G_-dnXDaoKeUpT3TnZ2dnUVZ8lGdnZ2d@bt.com>


Quoth comp.lang.perl.misc@googlegroups.com:
> On Wednesday, October 19, 2011 8:20:50 AM UTC-4, Ben Morrow wrote:
> > What you have there works for me, here. Do you, in fact, have a .pm and
> > a .so for Imager::File::GIF? If you run ldd against the .so (which
> > should be somewhere like /usr/local/lib/perl5/site_perl/5.12.4/
> > amd64-freebsd/auto/Imager/File/GIF/GIF.so) are all the libraries it
> > needs found?
> > 
> > Is there something wrong with your gif file? There is a file called
> > GIF/testimg/expected.gif in the Imager distribution; since that's the
> > file Imager uses to make sure giflib works, you could try that (it
> > should be 16x16px).
> 
> You're right, it's more likely that there's something missing on the
> server. I thought that the Imager package would include everything it
> needed, but I guess not.
> 
> I don't see Imager::File::GIF in my list of installed modules (from
> WHM/cPanel), so I tried to install it manually. The install failed with:
> 
> perl-64bit-hack: enabled
> GIF: building independently
> GIF: main: includes not found - libraries not found
> GIF: Checking if the compiler can find them on its own
> GIF: Test code failed: Can't link/include 'gif_lib.h', 'stdio.h', 'gif'
> OS unsupported: GIF libraries or headers not found

Well, that seems fairly clear: it can't find the gif headers. Have you
installed all appropriate 'development' packages? Often these packaging
systems give you a normal libgif package with libgif.so but split the
headers out into a separate package called something like libgif-devel.

Otherwise, if you *have* got a gif_lib.h but it's somewhere other than
/usr/include or /usr/local/include, run the build again with
$ENV{IM_INCPATH} set to the directory it's installed in.

> Interestingly, my modules go to /usr/lib/perl5/site_perl/5.8.8, but I
> don't see Imager installed there at all! And I definitely don't have
> Imager/File/GIF. Imager is working, though, so it exists somewhere... I
> just don't know where. Could this be why it's failing?

locate(1) is your friend in situations like this, assuming its database
is getting updated.

Imager is an XS module (it has a compiled portion) so it installs in the
sitearch directory. You can get the path by running perl -V:sitearch;
the answer will probably be /usr/lib/perl5/site_perl/5.8.8/i386-linux,
but it depends on how your perl was built. I::F::G should have a .pm at
$sitearch/Imager/File/GIF.pm and a .so at
$sitearch/auto/Imager/File/GIF/GIF.so.

(5.8.8 is officially unsupported; in fact all versions of 5.8 have been
unsupported since 2008. You should really see if you can upgrade to at
least 5.12.)

Ben



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

Date: Thu, 20 Oct 2011 12:49:26 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Learning Perl CGI and OOP, not printing @c
Message-Id: <0ef3b0af-6f9f-4c27-9a76-93761e51ce40@q12g2000yqe.googlegroups.com>

I=92m learning PERL CGI with OOP.

Below is my code for the user to select the number of servers.

print "<p><em>Servers</em><br>",
                       checkbox_group(
                               -name=3D>'sequence',
                               -default=3D>'',
                               -linebreak=3D>'true',
                               -values=3D>['10.233.22.66',
                                         '10.233.22.67',
                                         '10.233.22.68',
                                         '10.233.22.69',
                                         '10.233.22.70',
                                         '10.233.22.71',
                                         '10.233.22.72',
                                         '10.233.22.73'],

         -labels=3D>\my %labels,
         -attributes=3D>\my %attributes);

###This is how process the selection of servers.

                     @values =3D param('sequence');
                     chomp(@values);

                     foreach my $line (@values) {
                     my $line =3D~ s/$_/$line:a/;
                     push(@n, "$line");
                                               }


Here=92s my OOP code that is in the same file.

my $object =3D new Misc();
my @e =3D $object->setNodes(@n);
my @c =3D $object->getNodes();
print "\n", br;
#######################################
print @c, br;
#######################################
But it=92s not printing at all.
#######################################

Below is my package:

package Misc;

sub new
{
    my $class =3D shift;
    my $self =3D {};
    bless $self, $class;
    return $self;
}

sub setPse {
    my ( $self, @pse ) =3D @_;
    $self->{_pse} =3D @pse if defined(@pse);
    return $self->{_pse};
}

sub getPse {
    my( $self ) =3D @_;
    return $self->{_pse};
}

sub setStatus {
    my ( $self, $status ) =3D @_;
    $self->{_status} =3D $status if defined($pse);
    return $self->{_status};
}

sub getStatus {
    my( $self ) =3D @_;
    return $self->{_status};
}

sub setNodes {
    my ( $self, $nodes ) =3D @_;
    $self->{_nodes} =3D $pse if defined($nodes);
    return $self->{_nodes};
}

sub getNodes {
    my( $self ) =3D @_;
    return $self->{_nodes};
}

1;


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

Date: Thu, 20 Oct 2011 15:33:24 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Learning Perl CGI and OOP, not printing @c
Message-Id: <x_ednc4UfOwJGD3TnZ2dnUVZ8iWdnZ2d@bt.com>


Quoth Nene <rodbass63@gmail.com>:
> I’m learning PERL CGI with OOP.
<snip>
> 
> ###This is how process the selection of servers.
> 
>                      @values = param('sequence');
>                      chomp(@values);
> 
>                      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.)

>                      push(@n, "$line");

Where do you declare @n? Are you using 'strict'?

Why are you stringifying $line? You don't need to.

>                                                }
> 
> 
> Here’s my OOP code that is in the same file.
> 
> my $object = new Misc();

    my $object = Misc->new();

> my @e = $object->setNodes(@n);

What do you do with @e?

> my @c = $object->getNodes();
> print "\n", br;
> #######################################
> print @c, br;
> #######################################
> But it’s not printing at all.
> #######################################
> 
> Below is my package:
> 
> package Misc;

You are likely to get in trouble in the future if you give your packages
very generic names like 'Misc'. It's worth giving them a prefix that's
likely to be unique, so you might call it something like Nene::Pse.

> sub new
> {
>     my $class = shift;
>     my $self = {};
>     bless $self, $class;
>     return $self;
> }
> 
> 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.

Apart from that, this assignment probably doesn't do what you expect.
@pse is in scalar context, so it returns the number of elements in the
array. If you want to set the whole list as the value of the attribute
you need to take a reference:

    $self->{_pse} = \@pse if @pse;

>     return $self->{_pse};
> }
> 
> sub getPse {
>     my( $self ) = @_;
>     return $self->{_pse};

If you're taking a reference, as above, and you want getPse to return a
list, you will need to dereference:

    return @{ $self->{_pse} };

I'm afraid the syntax is unavoidably rather awkward.

> }
> 
> sub setStatus {
>     my ( $self, $status ) = @_;
>     $self->{_status} = $status if defined($pse);

Where does $pse come from?

>     return $self->{_status};
> }
> 
> sub getStatus {
>     my( $self ) = @_;
>     return $self->{_status};
> }
> 
> 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

    my ($self, @nodes) = @_;
    $self->{_nodes} = \@nodes if @nodes;

as for setPse above. You'll also need the corresponding modification to
getNodes, of course.

All these, taken together, mean that getNodes is always returning a
single 'undef', so it's not surprising you don't see anything when you
print it.

Ben



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

Date: Thu, 20 Oct 2011 17:10:30 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Re: Learning Perl CGI and OOP, not printing @c
Message-Id: <caba71e9-59a2-42f2-a106-6a5bee7b77f5@20g2000yqz.googlegroups.com>

On Oct 20, 4:33=A0pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Nene <rodbas...@gmail.com>:
>
> > I=92m learning PERL CGI with OOP.
> <snip>
>
> > ###This is how process the selection of servers.
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0@values =3D param('sequence'=
);
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0chomp(@values);
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0foreach my $line (@values) {
> > =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, 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 =3D~ s/$_/$line:a/

>
> > =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.
>
> 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
>
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A0 =A0 =A0 =A0 =A0 =A0}
>
> > Here=92s my OOP code that is in the same file.
>
> > my $object =3D new Misc();
>
> =A0 =A0 my $object =3D Misc->new();
>
> > my @e =3D $object->setNodes(@n);
>
> What do you do with @e?
>
> > my @c =3D $object->getNodes();
> > print "\n", br;
> > #######################################
> > print @c, br;
> > #######################################
> > But it=92s not printing at all.
> > #######################################
>
> > Below is my package:
>
> > package Misc;
>
> You are likely to get in trouble in the future if you give your packages
> very generic names like 'Misc'. It's worth giving them a prefix that's
> likely to be unique, so you might call it something like Nene::Pse.

I usually don't call it these names, this is just a practice package.
>
> > sub new
> > {
> > =A0 =A0 my $class =3D shift;
> > =A0 =A0 my $self =3D {};
> > =A0 =A0 bless $self, $class;
> > =A0 =A0 return $self;
> > }
>
> > 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. The
> condition you want is 'if @pse', which tells you if @pse has any
> elements.

Ok, but I used this example from a tutorial.

>
> Apart from that, this assignment probably doesn't do what you expect.
> @pse is in scalar context, so it returns the number of elements in the
> array. If you want to set the whole list as the value of the attribute
> you need to take a reference:
>
> =A0 =A0 $self->{_pse} =3D \@pse if @pse;
>
> > =A0 =A0 return $self->{_pse};
> > }
>
> > sub getPse {
> > =A0 =A0 my( $self ) =3D @_;
> > =A0 =A0 return $self->{_pse};
>
> If you're taking a reference, as above, and you want getPse to return a
> list, you will need to dereference:
>
> =A0 =A0 return @{ $self->{_pse} };
>
> I'm afraid the syntax is unavoidably rather awkward.
>
> > }
>
> > sub setStatus {
> > =A0 =A0 my ( $self, $status ) =3D @_;
> > =A0 =A0 $self->{_status} =3D $status if defined($pse);
>
That is a typo error, sorry.



>
> > =A0 =A0 return $self->{_status};
> > }
>
> > sub getStatus {
> > =A0 =A0 my( $self ) =3D @_;
> > =A0 =A0 return $self->{_status};
> > }
>
> > 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 :-)
>
> =A0 =A0 my ($self, @nodes) =3D @_;
> =A0 =A0 $self->{_nodes} =3D \@nodes if @nodes;
>
> as for setPse above. You'll also need the corresponding modification to
> getNodes, of course.
>
> All these, taken together, mean that getNodes is always returning a
> single 'undef', so it's not surprising you don't see anything when you
> print it.
>
> Ben



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

Date: Thu, 20 Oct 2011 17:33:36 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Learning Perl CGI and OOP, not printing @c
Message-Id: <c6f1a7dbbik81i86nva8ceg1r9om3ethku@4ax.com>

Nene <rodbass63@gmail.com> wrote:
>On Oct 20, 4:33 pm, Ben Morrow <b...@morrow.me.uk> wrote:
>> Quoth Nene <rodbas...@gmail.com>:
>> >                      push(@n, "$line");
>>
>> 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

See "perldoc -q quoting": 
	What's wrong with always quoting "$vars"?

jue


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

Date: Thu, 20 Oct 2011 08:50:19 -0700 (PDT)
From: SSS Develop <sssdevelop@gmail.com>
Subject: suggest better trick to handle multiple hash keys
Message-Id: <f2c489d4-ef92-4969-b957-f2885fdeae64@l10g2000pra.googlegroups.com>

Hello,

I have  customers 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.

I am thinking of appending   "_NUMBER" (example.  cust001, cust001_0,
cust001_1 )  whenever keys repeat.

I have working code - but looking for help to better solution/better
code. Please help me.

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


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

my $s         = "cs011";
my $customers = {};
$s = generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} = 1;

$s = generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} = 2;

$s = generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} = 3;

$s = generate_key( $s, $customers );
print "The generated key is: $s\n";
$customers->{$s} = 4;

sub generate_key {
    if ( !defined $customers->{$s} ) { return $s; }
    else {
        if ( $s =~ /(.*)(_)(\d+$)/ ) {
            my $int = $3;
            $int++;
            $s = $1 . "_" . $int;
        }
        else { $s .= "_0"; }
        generate_key( $s, $customers );
    }
}

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

The output is:
The generated key is: cs011
The generated key is: cs011_0
The generated key is: cs011_1
The generated key is: cs011_2

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


thank you for your time !




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

Date: Thu, 20 Oct 2011 17:02:27 +0000
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: suggest better trick to handle multiple hash keys
Message-Id: <4ea05423$0$52263$815e3792@news.qwest.net>

On 10/20/11 15:50, SSS Develop wrote:
> Hello,
>
> I have  customers 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.

First, why do you feel you want to use a hash?

>
> I am thinking of appending   "_NUMBER" (example.  cust001, cust001_0,
> cust001_1 )  whenever keys repeat.

You neglected to say what you are going to do with this field.
If you're going to modify that value and eventually do some
operations in the DB, then you have to rethink it because you'll
lose the original value. If you're not going to do anything
else with the DB, then do whatever you want with the data.

Maybe rethinking your data structure is better??

push( @{ $customer{ $s } }, 'some data' );

or don't use a hash at all:

push( @arr, $s );

Look at using DBI, which will make interacting with a DB much easier.

If you're going to do any further DB work, you'd be better off
coming up with unique values for each row,  e.g. adding ROWID, or
using @rownum, or adding other columns in your SQL.  If you can improve
the table, by adding a unique column (auto increment, sequence, or
something), you'll be better off in the future.



>
> I have working code - but looking for help to better solution/better
> code. Please help me.
>
> --------------
>
>
>   #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $s         = "cs011";
> my $customers = {};
> $s = generate_key( $s, $customers );
> print "The generated key is: $s\n";
> $customers->{$s} = 1;
Why set it to 1??
[...]
>
> sub generate_key {
ahhh.. you're passing $s and $customers to this sub, but
you're using the global $s and $customers... not a good idea.

This can be improved a bit, but since the data is coming from
a DB, I question if it's even needed.

>      if ( !defined $customers->{$s} ) { return $s; }
>      else {
>          if ( $s =~ /(.*)(_)(\d+$)/ ) {
>              my $int = $3;
>              $int++;
>              $s = $1 . "_" . $int;
>          }
>          else { $s .= "_0"; }
>          generate_key( $s, $customers );
>      }
> }
>
> ----------------
>
> The output is:
> The generated key is: cs011
> The generated key is: cs011_0
> The generated key is: cs011_1
> The generated key is: cs011_2
>
> -------------
>
>
> thank you for your time !
>
>



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

Date: Thu, 20 Oct 2011 12:32:41 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: suggest better trick to handle multiple hash keys
Message-Id: <_NqdnU9mNMukxj3TnZ2dnUVZ8mqdnZ2d@bt.com>


Quoth SSS Develop <sssdevelop@gmail.com>:
> 
> I have  customers 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   "_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

    {
        cust001     => ...,
        cust001_0   => ...,
    }

you should be using this

    {
        cust001 => [
            ...,
            ...,
        ],
    }

<snip>
>  #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my $s         = "cs011";
> my $customers = {};
> $s = generate_key( $s, $customers );
> print "The generated key is: $s\n";
> $customers->{$s} = 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.

    my ($s, $customers) = @_;

>     if ( !defined $customers->{$s} ) { return $s; }

You should probably be using exists rather than defined.

>     else {
>         if ( $s =~ /(.*)(_)(\d+$)/ ) {
>             my $int = $3;
>             $int++;
>             $s = $1 . "_" . $int;
>         }
>         else { $s .= "_0"; }
>         generate_key( $s, $customers );

Going round and round joining up a string and breaking it apart again is
rather a waste of time. Try

        my $int = 0;
        while (exists $customers->{"$s_$int"}) {
            $int++;
        }
        $s .= "_$int";

>     }

You don't return $s either: the calling code is relying on the sub to
modify the global $s. 

Ben



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

Date: Thu, 20 Oct 2011 02:09:23 -0700 (PDT)
From: philkime@kime.org.uk
Subject: Re: Tricky STDERR issue under windows
Message-Id: <12905843.1001.1319101763472.JavaMail.geo-discussion-forums@yqgn17>

Everything is gcc so there is no C runtime problem. It seems to be a Windows issue - this example shows the problem: even using Capture::Tiny it fails, but only on Windows:

#!/usr/bin/perl

use Capture::Tiny 'capture';
use File::Temp qw(tempfile);
use Inline C;

my ($stdout, $stderr) = capture { test_err() };

print "|$stderr|\n";

__END__
__C__
void test_err()
{
(void)fprintf (stderr, "ERROR");
}


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

Date: Thu, 20 Oct 2011 09:00:10 -0700 (PDT)
From: philkime@kime.org.uk
Subject: Re: Tricky STDERR issue under windows
Message-Id: <29594634.180.1319126410586.JavaMail.geo-discussion-forums@yqja14>

Someone mailed me and pointed out that Windows doesn't slush STDERR like other OSES ... so with one more line, it works in windows:

#!/usr/bin/perl 

use Capture::Tiny 'capture'; 
use File::Temp qw(tempfile); 
use Inline C; 

my ($stdout, $stderr) = capture { test_err() }; 

print "|$stderr|\n"; 

__END__ 
__C__ 
void test_err() 
{ 
(void)fprintf (stderr, "ERROR"); 
(void)fflush(stderr);
} 


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

Date: Thu, 20 Oct 2011 12:40:14 -0500
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Tricky STDERR issue under windows
Message-Id: <eo2dnQGmw4xjwT3TnZ2dnUVZ7tSdnZ2d@bt.com>


Quoth comp.lang.perl.misc@googlegroups.com:
> Someone mailed me and pointed out that Windows doesn't slush STDERR like
> other OSES ... so with one more line, it works in windows:
> 
> #!/usr/bin/perl 
> 
> use Capture::Tiny 'capture'; 
> use File::Temp qw(tempfile); 
> use Inline C; 
> 
> my ($stdout, $stderr) = capture { test_err() }; 
> 
> print "|$stderr|\n"; 
> 
> __END__ 
> __C__ 
> void test_err() 
> { 
> (void)fprintf (stderr, "ERROR"); 
> (void)fflush(stderr);
> } 

The fact this works with Inline::C doesn't mean it will work with an
external library. The perl headers redefine the stdio functions like
fprintf and fflush to refer to the PerlIO functions, for compatibility
with older extensions which assumed perl was using stdio.

Ben



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

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 3524
***************************************


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