[29811] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1054 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 23 11:09:36 2007

Date: Fri, 23 Nov 2007 08:09:07 -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           Fri, 23 Nov 2007     Volume: 11 Number: 1054

Today's topics:
    Re: A little help on perl hashes.. <simon.chao@fmr.com>
    Re: A little help on perl hashes.. <simon.chao@fmr.com>
    Re: A little help on perl hashes.. <simon.chao@fmr.com>
    Re: Can you pl. take a look here? <1usa@llenroc.ude.invalid>
    Re: Can you pl. take a look here? clearguy02@yahoo.com
    Re: float -> RGB <spamtrap@dot-app.org>
    Re: How to clean up this ugly code? <ced@blv-sam-01.ca.boeing.com>
    Re: How to clean up this ugly code? <blaine@worldweb.com>
    Re: How to get the string Cartesian Products of 2 list xueweizhong@gmail.com
    Re: How to get the string Cartesian Products of 2 list <njus@larshaugseth.com>
    Re: How to get the string Cartesian Products of 2 list xueweizhong@gmail.com
    Re: How to get the string Cartesian Products of 2 list <brian.d.foy@gmail.com>
    Re: minicpan, CPAN/CPANPLUS and autobundles on linux <brian.d.foy@gmail.com>
    Re: packaging apps as a distributable exe <spamtrap@dot-app.org>
    Re: Script to disconnect Linksys WRT54G wireless router <spamtrap@dot-app.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 23 Nov 2007 07:15:29 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: A little help on perl hashes..
Message-Id: <3792b543-1908-4cfd-849c-eed12535b028@o6g2000hsd.googlegroups.com>

On Nov 23, 12:32 am, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "c" == clearguy02  <cleargu...@yahoo.com> writes:
>
>   >> > I am still looking our the help.. Can some kindly take a look at it?
>   >>
>   >> Being a pain doesn't often lead to useful help on Usenet. Responding to
>   >> your own post after 12 hours simply to ask again and quoting Google's
>   >> rubbish both count as being a pain, especially when you've been warned
>   >> you're being rude.
>
>   c> When I was asked to post my code, I did it.. If you don't want to
>   c> help.. pl. kindly stay away. Pl. understand that I am stuck with an
>   c> issue and that I am not fooling around here. I have clearly told what
>   c> the issue is, what I have done and I am seeking out for some help
>   c> here.
>
> did you pay for your service here? since you didn't you don't have a
> right to expect anything from usenet. you just post code and ask for
> help and shut up about it. this is a place to discuss perl issues and if
> your code or problem interests anyone they will reply. your attitude has
> already lost you several possible repliers including me. not much more
> to say.
>

What am I missing here? It appears that for some reason the number of
keys in my hash are doubling for no reason.

use strict; use warnings;
use Data::Dumper;

my %ids;

while ( <DATA> ) {
    next if /^\s*$/;
    chomp;
    my ( $userid, $uid, $mid ) = split;
    $ids{$uid} = { userid    => $userid,
                   mid       => $mid,
                   managerid => '' };
}

print Dumper( \%ids ), "\n";

my $num_keys = keys %ids;
print "num_keys: $num_keys\n";

my $itr = 0;
for my $key ( %ids ) {
    print ++$itr, " $key\n";
#    my $managerid;
#    my $mid = $ids{$key}{mid};

#    print "mid: $mid\n";

    # find userid where uid = this $mid
#    if ( exists $ids{$mid} ) {
#        $managerid = lookup_userid( $mid );
#        $ids{$key}{managerid} = $managerid;
#    }
}

print '-' x 20, "\n";
print Dumper( \%ids ), "\n";



#=========
# subs
#=========

sub lookup_userid {
    my ( $mid ) = @_;
    return $ids{$mid}{userid};
}


__DATA__
jsmiths         00206251        00207609
jcarter         00207609        00220458
cgoldberg       00220458        00202003
hwatson         00202003        00201869



--------------
__OUTPUT__
bash-2.03$ ./par_chld.pl
$VAR1 = {
          '00206251' => {
                          'mid' => '00207609',
                          'userid' => 'jsmiths',
                          'managerid' => ''
                        },
          '00202003' => {
                          'mid' => '00201869',
                          'userid' => 'hwatson',
                          'managerid' => ''
                        },
          '00207609' => {
                          'mid' => '00220458',
                          'userid' => 'jcarter',
                          'managerid' => ''
                        },
          '00220458' => {
                          'mid' => '00202003',
                          'userid' => 'cgoldberg',
                          'managerid' => ''
                        }
        };

num_keys: 4
1 00206251
2 HASH(0x11acd0)
3 00202003
4 HASH(0x1bf570)
5 00207609
6 HASH(0x11abec)
7 00220458
8 HASH(0x1a9ce8)
--------------------
$VAR1 = {
          '00206251' => {
                          'mid' => '00207609',
                          'userid' => 'jsmiths',
                          'managerid' => ''
                        },
          '00202003' => {
                          'mid' => '00201869',
                          'userid' => 'hwatson',
                          'managerid' => ''
                        },
          '00207609' => {
                          'mid' => '00220458',
                          'userid' => 'jcarter',
                          'managerid' => ''
                        },
          '00220458' => {
                          'mid' => '00202003',
                          'userid' => 'cgoldberg',
                          'managerid' => ''
                        }
        };




When I remove my comments in my for loop, I get a data structure that
allows me to answer the ops question, but this bizarre issue comes up
with extra hash keys. Have I just not drunk enough coffee??


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

Date: Fri, 23 Nov 2007 07:27:34 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: A little help on perl hashes..
Message-Id: <445c3388-22bc-412e-b642-793eb8e44724@e4g2000hsg.googlegroups.com>

On Nov 23, 10:15 am, nolo contendere <simon.c...@fmr.com> wrote:
> On Nov 23, 12:32 am, Uri Guttman <u...@stemsystems.com> wrote:
>
>
>
> > >>>>> "c" == clearguy02  <cleargu...@yahoo.com> writes:
>
> >   >> > I am still looking our the help.. Can some kindly take a look at it?
>
> >   >> Being a pain doesn't often lead to useful help on Usenet. Responding to
> >   >> your own post after 12 hours simply to ask again and quoting Google's
> >   >> rubbish both count as being a pain, especially when you've been warned
> >   >> you're being rude.
>
> >   c> When I was asked to post my code, I did it.. If you don't want to
> >   c> help.. pl. kindly stay away. Pl. understand that I am stuck with an
> >   c> issue and that I am not fooling around here. I have clearly told what
> >   c> the issue is, what I have done and I am seeking out for some help
> >   c> here.
>
> > did you pay for your service here? since you didn't you don't have a
> > right to expect anything from usenet. you just post code and ask for
> > help and shut up about it. this is a place to discuss perl issues and if
> > your code or problem interests anyone they will reply. your attitude has
> > already lost you several possible repliers including me. not much more
> > to say.
>
> What am I missing here? It appears that for some reason the number of
> keys in my hash are doubling for no reason.
>
> use strict; use warnings;
> use Data::Dumper;
>
> my %ids;
>
> while ( <DATA> ) {
>     next if /^\s*$/;
>     chomp;
>     my ( $userid, $uid, $mid ) = split;
>     $ids{$uid} = { userid    => $userid,
>                    mid       => $mid,
>                    managerid => '' };
>
> }
>
> print Dumper( \%ids ), "\n";
>
> my $num_keys = keys %ids;
> print "num_keys: $num_keys\n";
>
> my $itr = 0;
> for my $key ( %ids ) {
>     print ++$itr, " $key\n";
> #    my $managerid;
> #    my $mid = $ids{$key}{mid};
>
> #    print "mid: $mid\n";
>
>     # find userid where uid = this $mid
> #    if ( exists $ids{$mid} ) {
> #        $managerid = lookup_userid( $mid );
> #        $ids{$key}{managerid} = $managerid;
> #    }
>
> }
>
> print '-' x 20, "\n";
> print Dumper( \%ids ), "\n";
>
> #=========
> # subs
> #=========
>
> sub lookup_userid {
>     my ( $mid ) = @_;
>     return $ids{$mid}{userid};
>
> }
>
> __DATA__
> jsmiths         00206251        00207609
> jcarter         00207609        00220458
> cgoldberg       00220458        00202003
> hwatson         00202003        00201869
>
> --------------
> __OUTPUT__
> bash-2.03$ ./par_chld.pl
> $VAR1 = {
>           '00206251' => {
>                           'mid' => '00207609',
>                           'userid' => 'jsmiths',
>                           'managerid' => ''
>                         },
>           '00202003' => {
>                           'mid' => '00201869',
>                           'userid' => 'hwatson',
>                           'managerid' => ''
>                         },
>           '00207609' => {
>                           'mid' => '00220458',
>                           'userid' => 'jcarter',
>                           'managerid' => ''
>                         },
>           '00220458' => {
>                           'mid' => '00202003',
>                           'userid' => 'cgoldberg',
>                           'managerid' => ''
>                         }
>         };
>
> num_keys: 4
> 1 00206251
> 2 HASH(0x11acd0)
> 3 00202003
> 4 HASH(0x1bf570)
> 5 00207609
> 6 HASH(0x11abec)
> 7 00220458
> 8 HASH(0x1a9ce8)
> --------------------
> $VAR1 = {
>           '00206251' => {
>                           'mid' => '00207609',
>                           'userid' => 'jsmiths',
>                           'managerid' => ''
>                         },
>           '00202003' => {
>                           'mid' => '00201869',
>                           'userid' => 'hwatson',
>                           'managerid' => ''
>                         },
>           '00207609' => {
>                           'mid' => '00220458',
>                           'userid' => 'jcarter',
>                           'managerid' => ''
>                         },
>           '00220458' => {
>                           'mid' => '00202003',
>                           'userid' => 'cgoldberg',
>                           'managerid' => ''
>                         }
>         };
>
> When I remove my comments in my for loop, I get a data structure that
> allows me to answer the ops question, but this bizarre issue comes up
> with extra hash keys. Have I just not drunk enough coffee??

omg. I forgot 'keys' in the loop. not enough coffee.


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

Date: Fri, 23 Nov 2007 07:31:50 -0800 (PST)
From: nolo contendere <simon.chao@fmr.com>
Subject: Re: A little help on perl hashes..
Message-Id: <b1a75e80-a2ad-45f9-b6c1-010fd961ec5d@s6g2000prc.googlegroups.com>

On Nov 21, 3:36 pm, cleargu...@yahoo.com wrote:
> Hi all,
>
> I am struggling with the below puzzle:
>
> Here is the contents of a file, c:\test.txt
>
> ============================================================
> user id         name              uid              mid
>
> jsmiths         john            00206251        00207609
> jcarter                         joseph          00207609        00220458
> mstella         mary            00202227        00207609
> cgoldberg                       cindy           00220458        00202003
> mshiva          murthy          00224981        00207609
> hwatson         henry           00202003        00201869
> =============================================================
>
> jcarter is the manager of jsmiths, mstella and mshiva;
> cgoldberg is the manager of jcarter;
> hwatson is the manager of cgoldberg;
>
> In the output file, I need to replace the mid with the manager id as
> follows. And the topmost manager (hwatson) can remain with his id.
>
> ================================================
> user id         name            manager id
>
> jsmiths         john            jcarter
> jcarter                         joseph          cgoldberg
> mstella         mary            jcarter
> cgoldberg                       cindy           hwatson
> mshiva          murthy          jcarter
> hwatson         henry           hwatson
> =================================================
>



use strict; use warnings;
use Data::Dumper;

my %ids;

while ( <DATA> ) {
    next if /^\s*$/;
    chomp;
    my ( $userid, $uid, $mid ) = split;
    $ids{$uid} = { userid    => $userid,
                   mid       => $mid,
                   managerid => '' };
}

my $itr = 0;
for my $key ( keys %ids ) {
    my $managerid;
    my $mid = $ids{$key}{mid};

    # find userid where uid = this $mid
    if ( exists $ids{$mid} ) {
        $managerid = lookup_userid( $mid );
        $ids{$key}{managerid} = $managerid;
    }
}

for my $uid ( keys %ids ) {
    unless ( $ids{$uid}{managerid} ) { $ids{$uid}{managerid} =
$ids{$uid}{userid}; };
    print "$ids{$uid}{userid}\t$ids{$uid}{managerid}\n";
}


#=========
# subs
#=========

sub lookup_userid {
    my ( $mid ) = @_;
    return $ids{$mid}{userid};
}


__DATA__
jsmiths         00206251        00207609
jcarter         00207609        00220458
cgoldberg       00220458        00202003
hwatson         00202003        00201869



========
__OUTPUT__
jsmiths jcarter
hwatson hwatson
jcarter cgoldberg
cgoldberg       hwatson



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

Date: Fri, 23 Nov 2007 14:25:37 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Can you pl. take a look here?
Message-Id: <Xns99F15FE5458D7asu1cornelledu@127.0.0.1>

clearguy02@yahoo.com wrote in
news:66e8698f-caef-4e09-8202-cfc81e692bd1@e6g2000prf.googlegroups.com: 

> Subject: Can you pl. take a look here?

Please do read the posting guidelines for this group and spend some time 
composing a meaningful subject line.

I only looked at this post by accident (I had figured it for a Viagra ad 
based on the subject line).

> Here is the problem description.

Are you assigning homework now?

> __DATA__
> # user_id	                name	email		user_uid	manager_uid
> 
> jcarter        	john          a@gmail.com	00206251	00207609
> mstella      	mary	b@yahoo.com	00207609	00220458
> msmith      	martin	c@gmail.com	00202227	00207609
> bborders     	bob	d@gmail.com	00220458	00202003
> swatson     	sush	e@yahoo.com           00224981	00207609
> rcasey        	rick	f@gmail.com	00202003	00201009
> ------------------------------------------------------------------
> 
> mstella is the boss of jcarter, msmith and swatson;
> bborders  is the boss  of mstella;
> rcasey is the boss of bborders;
> 
> Now I need to replace the manager uid's with the boss id's; for the
> top-most manager's id, you can replace his manager uid with his user
> id itself (in this case rcasey is the top-most guy).

So, if manager_uid is not in the user_uid's supplied, then assume user 
answers to higher authority than the mortals in this set.


> i.e the output file should be as follows:
> 
> ++++++++++++++++++++++++++++++++++++++++++++++++++++
> # user_id                     name     email          manager_id
> 
> jcarter             john          a@gmail.com     mstella
> mstella           mary     b@yahoo.com     bborders
> msmith           martin     c@gmail.com     mstella
> bborders          bob     d@gmail.com     rcasey
> swatson          sush     e@yahoo.com           mstella
> rcasey             rick     f@gmail.com     rcasey
> +++++++++++++++++++++++++++++++++++++++++++++++++++++

But now you are contradicting yourself. rcasey has a manager. The 
manager's id is 00201009. rcasey answers to someone who is not him.

>  while (<DATA>)
> {
>    $line = $_;
>    @lineArray = split (/\s+/, $line);

Don't forget to chomp.

No need for silly intermediate assignments. Are you being paid by the 
line?

while ( <DATA> ) {
   chomp;
   my @lineArray = split /\s+/;

>    if ($lineArray[3] == $lineArray[2])

You are using numeric comparison when the input data are really strings 
(i.e. things like ZIP codes, badge numbers etc -- even when they consist 
entirely of digits -- are not numbers).

>      {
>        s/$lineArray[3]/$lineArray[0]/g;
>      }
>     print "$lineArray[0]\t\t$lineArray[1]\t\t$lineArray[3]\n";
> }
> 
> I am struggling with the part of replacing the the numerical
> manager_uid with the manager_id.

To my simple mind, it looks like your problem requires two passes over 
data.

I assumed, based on your example, that the output had to be in the same 
order as the input.

#!/usr/bin/perl

use strict;
use warnings;

my ( %users, @users );

while ( my $record = <DATA> ) {
    next if $record =~/^#/;
    chomp $record;
    next if $record =~ /^\s*$/;

    my @fields = split /\s+/, $record;
    $users{ $fields[3] } = $fields[0];
    push @users, \@fields;
}

for my $user ( @users ) {
    my ($user_uid, $manager_uid) = @$user[3,4];

    my $manager = exists $users{ $manager_uid }
                ? $users{ $manager_uid }
                : $users{ $user_uid } ;
    print join("\t", @$user[0 .. 2], $manager), "\n";
}

__DATA__
# user_id   name	email		user_uid	manager_uid
jcarter     john    a@gmail.com	00206251	00207609
mstella     mary	b@yahoo.com	00207609	00220458
msmith      martin	c@gmail.com	00202227	00207609
bborders    bob	    d@gmail.com	00220458	00202003
swatson     sush	e@yahoo.com 00224981	00207609
rcasey      rick	f@gmail.com	00202003	00201009



-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>



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

Date: Fri, 23 Nov 2007 07:26:22 -0800 (PST)
From: clearguy02@yahoo.com
Subject: Re: Can you pl. take a look here?
Message-Id: <47f887ab-a850-4201-ae3a-f6c79aefc1cd@d27g2000prf.googlegroups.com>

On Nov 23, 6:25 am, "A. Sinan Unur" <1...@llenroc.ude.invalid> wrote:
> cleargu...@yahoo.com wrote innews:66e8698f-caef-4e09-8202-cfc81e692bd1@e6g2000prf.googlegroups.com:
>
> > Subject: Can you pl. take a look here?
>
> Please do read the posting guidelines for this group and spend some time
> composing a meaningful subject line.
>
> I only looked at this post by accident (I had figured it for a Viagra ad
> based on the subject line).
>
> > Here is the problem description.
>
> Are you assigning homework now?
>
>
>
>
>
> > __DATA__
> > # user_id                  name    email           user_uid        manager_uid
>
> > jcarter            john          a...@gmail.com   00206251        00207609
> > mstella            mary    b...@yahoo.com 00207609        00220458
> > msmith             martin  c...@gmail.com 00202227        00207609
> > bborders           bob     d...@gmail.com 00220458        00202003
> > swatson            sush    e...@yahoo.com           00224981      00207609
> > rcasey             rick    f...@gmail.com 00202003        00201009
> > ------------------------------------------------------------------
>
> > mstella is the boss of jcarter, msmith and swatson;
> > bborders  is the boss  of mstella;
> > rcasey is the boss of bborders;
>
> > Now I need to replace the manager uid's with the boss id's; for the
> > top-most manager's id, you can replace his manager uid with his user
> > id itself (in this case rcasey is the top-most guy).
>
> So, if manager_uid is not in the user_uid's supplied, then assume user
> answers to higher authority than the mortals in this set.
>
> > i.e the output file should be as follows:
>
> > ++++++++++++++++++++++++++++++++++++++++++++++++++++
> > # user_id                     name     email          manager_id
>
> > jcarter             john          a...@gmail.com     mstella
> > mstella           mary     b...@yahoo.com     bborders
> > msmith           martin     c...@gmail.com     mstella
> > bborders          bob     d...@gmail.com     rcasey
> > swatson          sush     e...@yahoo.com           mstella
> > rcasey             rick     f...@gmail.com     rcasey
> > +++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> But now you are contradicting yourself. rcasey has a manager. The
> manager's id is 00201009. rcasey answers to someone who is not him.
>
> >  while (<DATA>)
> > {
> >    $line = $_;
> >    @lineArray = split (/\s+/, $line);
>
> Don't forget to chomp.
>
> No need for silly intermediate assignments. Are you being paid by the
> line?
>
> while ( <DATA> ) {
>    chomp;
>    my @lineArray = split /\s+/;
>
> >    if ($lineArray[3] == $lineArray[2])
>
> You are using numeric comparison when the input data are really strings
> (i.e. things like ZIP codes, badge numbers etc -- even when they consist
> entirely of digits -- are not numbers).
>
> >      {
> >        s/$lineArray[3]/$lineArray[0]/g;
> >      }
> >     print "$lineArray[0]\t\t$lineArray[1]\t\t$lineArray[3]\n";
> > }
>
> > I am struggling with the part of replacing the the numerical
> > manager_uid with the manager_id.
>
> To my simple mind, it looks like your problem requires two passes over
> data.
>
> I assumed, based on your example, that the output had to be in the same
> order as the input.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my ( %users, @users );
>
> while ( my $record = <DATA> ) {
>     next if $record =~/^#/;
>     chomp $record;
>     next if $record =~ /^\s*$/;
>
>     my @fields = split /\s+/, $record;
>     $users{ $fields[3] } = $fields[0];
>     push @users, \@fields;
>
> }
>
> for my $user ( @users ) {
>     my ($user_uid, $manager_uid) = @$user[3,4];
>
>     my $manager = exists $users{ $manager_uid }
>                 ? $users{ $manager_uid }
>                 : $users{ $user_uid } ;
>     print join("\t", @$user[0 .. 2], $manager), "\n";
>
> }
>
> __DATA__
> # user_id   name        email           user_uid        manager_uid
> jcarter     john    a...@gmail.com     00206251        00207609
> mstella     mary        b...@yahoo.com 00207609        00220458
> msmith      martin      c...@gmail.com 00202227        00207609
> bborders    bob     d...@gmail.com     00220458        00202003
> swatson     sush        e...@yahoo.com 00224981        00207609
> rcasey      rick        f...@gmail.com 00202003        00201009
>
> --
> A. Sinan Unur <1...@llenroc.ude.invalid>
> (remove .invalid and reverse each component for email address)
> clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>- Hide quoted text -
>
> - Show quoted text -

Thanks a bunch.. Sinan.

JC


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

Date: Fri, 23 Nov 2007 10:58:09 -0500
From: Sherman Pendley <spamtrap@dot-app.org>
Subject: Re: float -> RGB
Message-Id: <m1zlx5osxa.fsf@dot-app.org>

"alexxx.magni@gmail.com" <alexxx.magni@gmail.com> writes:

> Greetings,
> I know this isnt a perlish problem,
> but I need to transform a float $x=0..1 in RGB values ($R,$G,$B)
> according to whichever palette you want...

RGB colors don't refer to an index in a palette.

So, you have three float values $r, $g, and $b - and you want to transform
that into what, exactly? What have you tried so far? What were the results,
and how did they differ from the results you expected?

    <http://www.catb.org/~esr/faqs/smart-questions.html>

sherm--

-- 
WV News, Blogging, and Discussion: http://wv-www.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


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

Date: Fri, 23 Nov 2007 07:33:31 -0800 (PST)
From: "comp.llang.perl.moderated" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: How to clean up this ugly code?
Message-Id: <a27940e0-276e-4833-8604-f9782201023f@e25g2000prg.googlegroups.com>

On Nov 22, 3:21 pm, "bla...@worldweb.com" <bla...@worldweb.com> wrote:
> Hey,
>
> I have this ugly code below that I would like to get rid of everything
> in the switch statement. My preference would be to have something
> simple that just takes and id and calls that sub routine number.
>
> I suppose I could use and eval like
> eval( "\$self->DisplayPage$subpage_id");
>
> but was wondering if someone can think of something better, as then if
> there is an error it's trapped in eval..
>
> Code I'd like to clean is below...
>
>     SWITCH: for ($subpage_id)
>         {
>         if    (/^1$/)  {$self->DisplayPage; last SWITCH;}
>         elsif (/^2$/)  {$self->DisplayPage2; last SWITCH;}
>         elsif (/^3$/)  {$self->DisplayPage3; last SWITCH;}
>         elsif (/^4$/)  {$self->DisplayPage4; last SWITCH;}
>         elsif (/^5$/)  {$self->DisplayPage5; last SWITCH;}
>         elsif (/^6$/)  {$self->DisplayPage6; last SWITCH;}
>         elsif (/^7$/)  {$self->DisplayPage7; last SWITCH;}
>         elsif (/^8$/)  {$self->DisplayPage8; last SWITCH;}
>         elsif (/^9$/)  {$self->DisplayPage9; last SWITCH;}
>         }

Untested:

my %methods = map
   { ( qr/^$_/, 'DisplayPage' . $_ )  }
   1..9;

SWITCH: {
 while ( my ($re, $method ) = each %methods ) {
    $self->$method if $subpage_id =~ /$re/;
    last SWITCH;
 }
 die "no method for $subpage_id..\n";
}

--
Charles DeRykus


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

Date: Fri, 23 Nov 2007 07:51:45 -0800 (PST)
From: "blaine@worldweb.com" <blaine@worldweb.com>
Subject: Re: How to clean up this ugly code?
Message-Id: <10a4af32-b4f7-4c5c-b93d-d79551ec2cad@s12g2000prg.googlegroups.com>

Wow thanks for all your input! Everyone has some great ideas.

I'm going to use Joost's idea, seems really nice!

Thanks again!
Blaine

my $method = "DisplayPage$subpage_id";
$self->$method;

On Nov 22, 4:21 pm, "bla...@worldweb.com" <bla...@worldweb.com> wrote:
> Hey,
>
> I have this ugly code below that I would like to get rid of everything
> in the switch statement. My preference would be to have something
> simple that just takes and id and calls that sub routine number.
>
> I suppose I could use and eval like
> eval( "\$self->DisplayPage$subpage_id");
>
> but was wondering if someone can think of something better, as then if
> there is an error it's trapped in eval..
>
> Code I'd like to clean is below...
>
>     SWITCH: for ($subpage_id)
>         {
>         if    (/^1$/)  {$self->DisplayPage; last SWITCH;}
>         elsif (/^2$/)  {$self->DisplayPage2; last SWITCH;}
>         elsif (/^3$/)  {$self->DisplayPage3; last SWITCH;}
>         elsif (/^4$/)  {$self->DisplayPage4; last SWITCH;}
>         elsif (/^5$/)  {$self->DisplayPage5; last SWITCH;}
>         elsif (/^6$/)  {$self->DisplayPage6; last SWITCH;}
>         elsif (/^7$/)  {$self->DisplayPage7; last SWITCH;}
>         elsif (/^8$/)  {$self->DisplayPage8; last SWITCH;}
>         elsif (/^9$/)  {$self->DisplayPage9; last SWITCH;}
>         }
>
> Thanks,
> Blaine



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

Date: Fri, 23 Nov 2007 06:30:10 -0800 (PST)
From: xueweizhong@gmail.com
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <f6c3d2e3-e257-4c38-9001-025a3fdaf48f@e6g2000prf.googlegroups.com>

Hi Michele,

perl glob in perl5.8 is not power enough than the bash3.0 glob, it
doesn't support .. operator which is fatal to this issue:

#!/bin/perl

sub X {split / /,qx/bash -c "echo @_"/}

print "glob:\n";
print join "\n", glob "{a..b}{1..2}";

print "\n\nX:\n";
print join "\n", X    "{a..b}{1..2}";

glob:
a..b1..2

X:
a1
a2
b1
b2


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

Date: 23 Nov 2007 15:56:50 +0100
From: Lars Haugseth <njus@larshaugseth.com>
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <m1myt5c8nh.fsf@vader.eiendomsnett.no>


* xueweizhong@gmail.com wrote:
> 
> Hi Michele,
> 
> perl glob in perl5.8 is not power enough than the bash3.0 glob, it
> doesn't support .. operator which is fatal to this issue:

Why? In practice, the lists being globbed are likely to be variables,
not hard coded inside the glob call.

{
    local $" = ','; # List separator

    my @letters = 'a'..'c';
    my @numbers = 1..3;

    my @globbed = glob "{@letters}{@numbers}";

    print "@globbed\n";
}

Highly preferrable to forking a new shell process.

-- 
Lars Haugseth

"If anyone disagrees with anything I say, I am quite prepared not only to
 retract it, but also to deny under oath that I ever said it." -Tom Lehrer


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

Date: Fri, 23 Nov 2007 07:59:26 -0800 (PST)
From: xueweizhong@gmail.com
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <a6c10e47-6888-4821-befd-03a0b9e2b307@i29g2000prf.googlegroups.com>

> > perl glob in perl5.8 is not power enough than the bash3.0 glob, it
> > doesn't support .. operator which is fatal to this issue:
>
> Why? In practice, the lists being globbed are likely to be variables,
> not hard coded inside the glob call.
>
> {
>     local $" = ','; # List separator
>
>     my @letters = 'a'..'c';
>     my @numbers = 1..3;
>
>     my @globbed = glob "{@letters}{@numbers}";
>
>     print "@globbed\n";
>
> }
I do think these lines are worse than using loop BLOCK. What I want is
an single expression, not statements with so many nosiy lines.


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

Date: Fri, 23 Nov 2007 09:55:58 -0600
From: brian d  foy <brian.d.foy@gmail.com>
Subject: Re: How to get the string Cartesian Products of 2 list
Message-Id: <231120070955581757%brian.d.foy@gmail.com>

In article
<3bc8b657-828f-4cde-bbf5-9d9c09b8e59f@s8g2000prg.googlegroups.com>,
<xueweizhong@gmail.com> wrote:

> Give 2 string list such as: a..b and 1..3, i want to get a list which
> is (a1, a2, a3, b1, b2, b3). Is there an elegant way in perl to
> fullfill this withou using loop BLOCK? Or Is there a elegant one line
> expression to get this?

Although you've already said that you don't want non-core modules,
Set::CrossProduct can help here. It wouldn't be useful for this trivial
example, but for large sets it comes in handy. It provides an iterator
to get the next item so you don't have to compute the entire list at
once.

Good luck, :)


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

Date: Fri, 23 Nov 2007 10:00:20 -0600
From: brian d  foy <brian.d.foy@gmail.com>
Subject: Re: minicpan, CPAN/CPANPLUS and autobundles on linux
Message-Id: <231120071000207498%brian.d.foy@gmail.com>

In article <871wai9azh.fsf@kzsu.stanford.edu>, Joseph Brenner
<doom@kzsu.stanford.edu> wrote:


> I keep having funny problems with the CPAN shell trying to
> install the wrong version from the minicpan mirror, I gather
> because it's using an stale meta data from the ~/.cpan location
> rather than getting it from the mirror location.

Check your config to see if you've asked CPAN.pm to cache metadata,
and that you have the right list of URLs in the right order to check.

You probably want to see my talk about MyCPAN:

Perlcast audio: 
   http://perlcast.com/2007/09/29/making-my-own-cpan/

Slides:
   http://www252.pair.com/comdog/Talks/MyCPAN-LApm200709.pdf

Article:
   See issue 4.0 of The Perl Review: http://www.theperlreview.com


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

Date: Fri, 23 Nov 2007 10:53:53 -0500
From: Sherman Pendley <spamtrap@dot-app.org>
Subject: Re: packaging apps as a distributable exe
Message-Id: <m14pfdq7ou.fsf@dot-app.org>

aardvarkman@gmail.com writes:

> This app looks surprisingly mature

What app?

> anyone used it to distribute a perl app as an exe?

Used *what* to distribute a Perl app as an exe?

sherm--

-- 
WV News, Blogging, and Discussion: http://wv-www.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


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

Date: Fri, 23 Nov 2007 10:47:50 -0500
From: Sherman Pendley <spamtrap@dot-app.org>
Subject: Re: Script to disconnect Linksys WRT54G wireless router on Windows
Message-Id: <m1ejehq7yx.fsf@dot-app.org>

Wilson <davewilson69@sbcglobal.net> writes:

> On Fri, 23 Nov 2007 00:43:08 GMT, Wilson wrote:
>
>> The good news is I'm still the Internet for that as yet unknown method to
>> press a button on an https web page as I digest my holiday turkey. :)
>
> Oooooooops. I'm still *searching* the Internet for a method that works.
> So, I haven't given up; I've just concluded Perl isn't able to perform the
> three-step task of
> * going to a certain web page 
> * logging into an https connection
> * press a particular button

Are you being deliberately argumentative?

WWW::Mechanize is *very* capable of all of the above. You need to be a Perl
programmer to know how to use it, of course. If you're not, that means that
*you* aren't able to perform the above tasks in Perl - it's a limitation of
your own skill set, not a limitation of Perl.

> That's the one thing you should have told me but I don't blame you. If it
> can't be done, only the ones who have tried would know. Right?

Wrong. The above steps *can* be done in the general sense, by anyone with a
little bit of Perl skill.

If someone *with* that skill tried to do so with this particular router, and
posted a specific reason why the attempt failed - then I would agree with the
assessment that it can't be done.

When someone who admits to not having any Perl knowledge, and to not being a
programmer at all, claims to know what Perl is capable of, I have serious
doubts about that claim. Someone who's at that level simply doesn't have the
experience or background to reasonably assess Perl's capabilities.

sherm--

-- 
WV News, Blogging, and Discussion: http://wv-www.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1054
***************************************


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