Re: @hash{} versus $hash{}

2006-04-03 Thread John W. Krahn
John Ackley wrote:
> Inherited code (from Verisign):   @[EMAIL PROTECTED] = split /\t/,$rec;
> which worked but really puzzled me.

What is happening is a hash slice assignment similar to:

@datafield{ 'a', 'b', 'c' } = ( 'd', 'e', 'f' );

Where the first element in @send is used as the key for the first value from
split() and the second element in @send is used as the key for the second
value from split(), etc.

> I assumed that it meant[EMAIL PROTECTED] = split /\t/,$rec;
> which worked also as I verified by testing both versions.

[EMAIL PROTECTED] = split /\t/,$rec;

Is short for:

[EMAIL PROTECTED] = @_ = split /\t/,$rec;

Where @_ is assigned the values from split() and since [EMAIL PROTECTED] is a
scalar both @_ and @send are evaluated in scalar context which means that the
number of elements in @send is used as the key and the number of elements in
@_ is used as the value.

> However [EMAIL PROTECTED] = split /\t/,$rec;
> gives the warning quote: Use of implicit split to @_ is deprecated at . . .
> 
> Could some please explain the error messages
> and the syntax of these lines of code?
> 
> I understand the right side produces a list from a string.

Yes.

> I do not understand the first @ on the left side.
> Does it produce a list of $datafield{one}, $datafield{two}, . . . ?
> assuming @send = ( one, two, . . .);

Yes.  @[EMAIL PROTECTED] is short for ( $datafield{$send[0]},
$datafield{$send[1]}, $datafield{$send[2]}, $datafield{$send[3]}, ...,
$datafield{$send[$#send]} )

> Can someone point out documentation that explains
> this use of @?  I learned that @ flags an array.

perldoc -q 'What is the difference between \$array\[1] and @array\[1]'

And see the "Slices" section in perldata and perllol:

perldoc perldata
perldoc perllol



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: simple profiling?

2006-04-03 Thread Chas Owens
On 4/1/06, Bryan Harris <[EMAIL PROTECTED]> wrote:
snip
> This looks very interesting...  I downloaded it, but I have no idea how to
> install it, though.  I'm a modules-idiot.  I tried putting the .pm file in
> the current directory and putting "use TimeTick.pm;" at the beginning of my
> code, but it doesn't work.  I'd like to be able to just haul the .pm file
> around with my script and not have to "install" it on each machine I use...
>
> Thanks!
>
> - Bryan

Steps to install on UNIX style machines

The automated way

perl -MCPAN -e install Time::TimeTick

The manual way

1. Download the tar.gz file (in this case
http://search.cpan.org/CPAN/authors/id/P/PJ/PJS/Time-TimeTick-0.04.tar.gz)
2. uncompress and untar the file
3. change directory into the new directory
4. type "perl Makefile.PL"
5. type "make"
6. type "make test", this is optional
7. type "make install"

Now include the module in your code

#!/usr/bin/perl

use strict;
use Time::TimeTick;

END { timetick("Ending") }

timetick("Starting phase 1");
#phase 1
timetick("Starting phase 2");
#phase 2

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Simple Issues with Pod (on verbose and help page without param).

2006-04-03 Thread Edward WIJAYA

Dear all,

Here is a code which use Getopt::Long to produce a help instruction.
As can be seen below, I also want my code to print out the help message
when no argument is passed.

My question are:

1. How can I make the code below return man page when I simpy do:
   $ perl mycode.pl

   Namely no param is passed here.

2. Verbose 1 status below doesn't return anything when I do:
   $ perl mycode.pl -help.

   What's wrong with it?



=== mycode.pl ==
!/usr/bin/perl -w
use strict;
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;


my $some_param = 5; #Default value
my $help;
my $man;

GetOptions(
"someparam|s=s" => \$some_param,
"help"  => \$help,
"man"   => \$man,
  )
  or pod2usage( -verbose => 1 ) && exit;



pod2usage(-verbose => 1) && exit if defined $help;
pod2usage(-verbose => 2) && exit if defined $man;

# Do sth with param
print "PARAM IS: $some_param\n";

__END__
=head1 NAME

MYCODE - do something

=head1 SYNOPSIS

perl mycode.pl [options] [file ...]

 Options:
   -helpbrief help message
   -man full documentation

=head1 OPTIONS

=over 8

=item B<-help>

Print a brief help message and exits.

=item B<-man>

Prints the manual page and exits.

=back

=head1 DESCRIPTION

B will read the given input file(s) and do someting
useful with the contents thereof.

=cut
--
Regards,
Edward WIJAYA
SINGAPORE

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Example of IO::Tee use

2006-04-03 Thread Edi STOJICEVIC
Hi,

I've never used IO::Tee module and I would like to have the same thing
like tee -a on Unix system... 

Do you have some example of how to use it ? 

Regards,

-- 
. ''`.  (\___/) E d i   S T O J I C E V I C 
: :'  : (='.'=) http://www.debianworld.org  
`. `~'  (")_(") GPG: C360 FCF0 AB3A 2AB0 52E7 044F 1B3D 2109 1237 B032  
  `- 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: simple profiling?

2006-04-03 Thread brian d foy
In article <[EMAIL PROTECTED]>, Bryan Harris
<[EMAIL PROTECTED]> wrote:

> I have a script that takes ~5 seconds to run, but I'd like to get it down to
> <1 sec.  My problem is I don't know which part is the slow part.  So given
> something like this:

You don't have to modify your code to profile it. You can use one of
the profiling modules in the Devel:: namespace.

http://search.cpan.org/dist/Devel-SmallProf/ and my Dr. Dobbs article
on it: http://www.ddj.com/documents/s=1498/ddj0104pl/

http://search.cpan.org/dist/Devel-DProfPP/

I'm also working on a chapter on Profiling for Mastering Perl:
http://www.pair.com/comdog/mastering_perl/Chapters/profiling.html

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: simple profiling?

2006-04-03 Thread Peter Scott
On Sun, 02 Apr 2006 09:11:13 -0700, Bryan Harris wrote:
>> On Sat, 01 Apr 2006 09:29:47 -0700, Bryan Harris wrote:
>>> This looks very interesting...  I downloaded it, but I have no idea how
>>> to install it, though.  I'm a modules-idiot.  I tried putting the .pm
>>> file in the current directory and putting "use TimeTick.pm;" at the
>>> beginning of my code, but it doesn't work.  I'd like to be able to just
>>> haul the .pm file around with my script and not have to "install" it on
>>> each machine I use...
>> 
>> Make that
>> 
>>   use TimeTick; 
> Okay, now I get errors:

I didn't check my own module after seeing your first attempt.  It's
actually

  use Time::TimeTick;

as in the synopsis at
http://search.cpan.org/~pjs/Time-TimeTick-0.04/lib/Time/TimeTick.pm#SYNOPSIS .

> ps.  I have the Alpaca book, I have the whole Perl Bookshelf CD (best
> $60 I ever spent) -- but even after reading it the whole "modules"
> process seems confusing to me, I think there's a magic key that my brain
> doesn't have yet. (Also, my code has to be portable, lots of people use
> my scripts and I can't install modules on their machines.)

You can package the modules with the scripts.  I was just installing
TWiki, for instance, which is written in Perl and uses many CPAN modules. 
It comes with the ones that it needs, unpacks them into its own directory,
and directs its scripts to look in that directory.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: simple profiling?

2006-04-03 Thread Jay Savage
On 3/31/06, John W. Krahn <[EMAIL PROTECTED]> wrote:
> Mr. Shawn H. Corey wrote:
> > On Fri, 2006-31-03 at 14:41 -0700, Bryan Harris wrote:
> >>I have a script that takes ~5 seconds to run, but I'd like to get it down to
> >><1 sec.  My problem is I don't know which part is the slow part.  So given
> >>something like this:
> >>
> >>**
> >>#! /usr/bin/perl -w
> >>
> >
> > my $start_time = time;
>
> Or you could just use Perl's built-in $^T variable.
>

[snip]

That all depends what you're trying to do. $^T is initialized as soon
as the script starts, and unless you establish some sort of baseline,
the startup time will be added to your first chunk of code and skew
the results. On my system, for instance:

#!/usr/bin/perl

use warnings;
use strict;

print time - $^T;

Prints anywhere from 2-8. That's a significant portion of the total
time for a short script. Hopefully OP has a better machine, but he'll
still want to check that startup time, both because it'll be
interesting its own right, and because he'll want to subtract it out
if he uses $^T to benchmark his code.

I'd also recommend OP take a look at Time::HiRes. Shaving fractions of
seconds off blocks of code is going to be difficult to with a timer
that only has a resolution of a second.

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!


Re: Simple Issues with Pod (on verbose and help page without param).

2006-04-03 Thread Tom Phoenix
On 4/3/06, Edward WIJAYA <[EMAIL PROTECTED]> wrote:

> 1. How can I make the code below return man page when I simpy do:
> $ perl mycode.pl
>
> Namely no param is passed here.

unless (@ARGV) {
# no command-line args
pod2usage(-verbose => 2);
exit;
}

> 2. Verbose 1 status below doesn't return anything when I do:
> $ perl mycode.pl -help.
>
> What's wrong with it?

I have no idea what the phrase "Verbose 1 status below" means. Your
code does what I think it should when the '-help' (or '-help.' with a
period) is used. Have you tried the debug option of GetOpt::Long?

> === mycode.pl ==
> !/usr/bin/perl -w

You seem to have lost the initial '#' character.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Example of IO::Tee use

2006-04-03 Thread Tom Phoenix
On 4/3/06, Edi STOJICEVIC <[EMAIL PROTECTED]> wrote:

> I've never used IO::Tee module and I would like to have the same thing
> like tee -a on Unix system...
>
> Do you have some example of how to use it ?

Do you need more of an example than what's in the documentation?

http://search.cpan.org/~kenshan/IO-Tee-0.64/Tee.pm

If you're seeking "append" functionality, simply open the filehandles
for append before you pass them to IO::Tee. See the perlopentut
manpage. Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Which structure to handle data

2006-04-03 Thread Andrej Kastrin

Dear Perl community,

I need to re-sort a set of data. I think that the below example is self 
explained; so which Perl structure should I use to handle this dataset?


Thanks in advance for any suggestion, Andre


2;John;Apple;Banana
3;Andrew;Pear;Apple;Melon;Orange
8;Susan;Pear;Melon

2;John;Apple
2;John;Banana
3;Andrew;Pear
3;Andrew;Apple
3;Andrew;Melon
3;Andrew;Orange
8;Susan;Pear
8;Susan;Melon

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Which structure to handle data

2006-04-03 Thread Tom Phoenix
On 4/3/06, Andrej Kastrin <[EMAIL PROTECTED]> wrote:

> I need to re-sort a set of data. I think that the below example is self
> explained; so which Perl structure should I use to handle this dataset?

Perl has two data structures. Arrays hold items in order, while hashes
keep track of their data through keys, which have no implied order.
So, if you want to keep data items in order, use an array.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Net::SMTP module

2006-04-03 Thread Sonika Sachdeva
What is the correct method to authenticate for sending mails , I found
auth(). But its not working...


RE: How to manage around 1000 entries which will constantly bechanging

2006-04-03 Thread Wagner, David --- Senior Programmer Analyst --- WGO
Mr. Shawn H. Corey wrote:
> On Sat, 2006-01-04 at 09:57 -0500, Frank Bax wrote:
>> I'm not the OP, but I have a script with a similar problem.  The
>> script has some logic that generates many (thousands of billions) of
>> combinations from a little bit of data and only the best 100 combos
>> are output. For each combination produced by script, a value is
>> calculated.  I maintain an array of "best" results so far; if size
>> of this array is <100, then a "new" result is automatically added. 
>> If there are already 100 items, I find the "worst" entry in the
>> array.  If the "new" entry is better than the "worst", the "new" one
>> replaces the "worst". 
>> 
>> I am not sure how your reference to "fail early" and "succeed early"
>> apply to this situation. 
>> 
>> At the moment, the array is left unsorted.  If I use a sorted array,
>> it needs to resorted every time a "worst" entry is replaced by a
>> "new" 
>> entry.  Can I avoid sorting the array every iteration?  How else to
>> find the "worst" entry?  If I replace "worst" with a "new" entry,
>> doesn't the array need to be resorted?  How does the cost of
>> searching an unsorted array compare to resorting on every update and
>> searching a sorted array. 
>> 
>> Tom's idea about growing to 200, then chopping back to 100 also
>> sounds interesting. 
>> 
>> Frank
> 
> Below is the algorithm I was talking about.
> 
I appreciate the input from the list. I am looking at the replies and 
trying to get a handle on what is being suggested. My situation is similar to 
Frank Bax and have the same 'billions of combinations' of which I am looking 
for the best 1000 to pass on to the business partner. They come in random order 
and have a set of data assoicated with them which I will need to maintain, but 
was thinking of just tying the number  and a key to the data so I am not moving 
the key/data constantly(600 to 800 bytes for each key).

Still trying to a grasp of what is being suggested. Also any time the 
keys are equal I do not want( or need to keep ) the data.

Again I thank the list for the input and now just need to see what I 
will attempt to do.

Wags ;)

> Let's say you want to keep the k best items of a list of size n, where
> best means the highest or lowest of them.
> 
> If n <= k: sort.
> 
> If k < n <= k * log2( k ): sort, discard excess.
> 
> If n > k *log2( k ): use below.
> 
> If k = 1_000 and n = 1_000_000_000, then 999_999_000 items are not
> going to be in the best list. Therefore, you want your algorithm to
> discard them as quickly as possible, hopefully, with just one test.
> This is what I mean by fail early; if most of the items are going to
> fail, design the algorithm so that failure is detected as early as
> possible. 
> 
> Similarly, succeed early means to design the algorithm to detect
> success as early as possible.
> 
> The script below uses a linear search to insert an item into the best
> list. You could use a binary search or a heap instead. I would not
> use a binary tree or a balanced binary tree. These structures work
> best when you are doing more searches than insertions and in this
> case we are doing an insertion every time.
> 
> 
> #!/usr/bin/perl
> # --
> # best_k -- Find the best k items of a set.
> 
> # --
> # Pragmas
> use strict;
> use warnings;
> 
> # --
> # Modules
> use Data::Dumper;
> use Getopt::Long;
> use POSIX;
> 
> # --
> # Configuration Parameters
> 
> my $K = 10; # top ten
> 
> $Data::Dumper::Sortkeys = 1;
> $Data::Dumper::Indent   = 1;
> $Data::Dumper::Maxdepth = 0;
> 
> # --
> # Globals Variables
> 
> my $Lowest = 0;
> 
> # --
> # Subroutines
> 
> # --
> # help();
> #   Print help via pod2text.
> # --
> sub help {
>   system( "pod2text -t $0" );
>   exit 0;
> }
> 
> # --
> # usage();
> #   Print usage via pod2usage.
> # --
> sub usage {
>   system( "pod2usage $0" );
>   exit 1;
> }
> 
> # --
> # init();
> #   Do things only possible when running.
> # --
> sub init {
>   unless( GetOptions(
>   help => \&help,
>   'top|highest' => sub { $Lowest = 0; },
>   'worst|lowest' => sub { $Lowest = 1; },
>   'k=i' => \$K,
>   )){
> usage();
>   }
> 
>   die "bad k $K\n" if $K <= 1;
> }
> 
> # --
> # Main
> {
>   my @items = ();
>   my %item = ();
> 
>   init;
> 
>   # Get the best k
>   while( <> ){
> /^\D*(\d+)/;
> my $nbr = $1;
> %item = (
>   nbr => $nbr,
>   orig => $_,
> );
> 
> if( @items == 0 ){
>   push @items, { %item };
>   next;
> }
> 
> if( @items < $K
>

Re: Net::SMTP module

2006-04-03 Thread JupiterHost.Net



Sonika Sachdeva wrote:

What is the correct method to authenticate for sending mails , I found
auth(). But its not working...


Try

  Mail::Sender::Easy

It handles SMTP authentication quite nicely :)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: How to manage around 1000 entries which will constantly be changing

2006-04-03 Thread Jay Savage
On 4/1/06, Frank Bax <[EMAIL PROTECTED]> wrote:
> At 06:59 PM 3/31/06, Mr. Shawn H. Corey wrote:
>
> >On Fri, 2006-31-03 at 15:45 -0800, Tom Phoenix wrote:
> > > You should loop over the input, pushing each item on to an array. If
> > > at any time you have 2000 items in the array, sort them and discard
> > > any you don't want to keep.
> > >
> > > $#data = 999 if $#data > 999;# OBperl: one way to discard
> > elements
> > >
> > > When you finish looping, sort-and-discard again. You'll never need
> > > more than 2N items in memory at any given time. Does that algorithm
> > > work for your needs?
> >
> >Sorting is not necessary. If he keeps an array of the best, that means
> >lowest, records then all he has to do is compare every new entry with
> >the highest. This is called "fail early." This means, if it's going to
> >fail, it should at the earliest opportunity. If it succeeds then it
> >searches down thru the list, to find its place. This is called "succeed
> >early." Given that the procedure can flip between these two methods, it
> >is faster than any sort.
>
>
> I'm not the OP, but I have a script with a similar problem.  The script has
> some logic that generates many (thousands of billions) of combinations from
> a little bit of data and only the best 100 combos are output. For each
> combination produced by script, a value is calculated.  I maintain an array
> of "best" results so far; if size of this array is <100, then a "new"
> result is automatically added.  If there are already 100 items, I find the
> "worst" entry in the array.  If the "new" entry is better than the "worst",
> the "new" one replaces the "worst".
>
> I am not sure how your reference to "fail early" and "succeed early" apply
> to this situation.
>
> At the moment, the array is left unsorted.  If I use a sorted array, it
> needs to resorted every time a "worst" entry is replaced by a "new"
> entry.  Can I avoid sorting the array every iteration?  How else to find
> the "worst" entry?  If I replace "worst" with a "new" entry, doesn't the
> array need to be resorted?  How does the cost of searching an unsorted
> array compare to resorting on every update and searching a sorted array.
>
> Tom's idea about growing to 200, then chopping back to 100 also sounds
> interesting.
>

Sorting is expensive, but keeping a sorted array isn't always,
especially if you know that the majority of your data will fall
outside your window, preferrably high. I'd probably do something like
the following:

#!/usr/bin/perl

use warnings;
use strict;

my $keeprecs = 10; # the number of records you want to keep
my @data = (10,24,5,32,4,9,8,7,6,1,2,0,3);
my @array;

while (@data) { # probably while <> in production
my $number = pop @data ;
if ($number > $array[-1]) {
next if @array >= $keeprecs;
# fail early if the number is high and we
# already have $keeprecs records saved
push @array, $number;
} elsif ($number < $array[0]) {
unshift @array, $number;
} else {
my($i, $idx);
for ($i = 0; $i < @array; $i++) {
next if $i == 0;
if (($number < $array[$i]) and ($number > $array[$i-1])) {
splice @array,$i,0,$number;
last;
}
}
}
pop @array if @array > $keeprecs;
}

print "@array\n";

The efficiency here will depend on the distribution of the data. If
every new piece of data is > $array[-2] and < $array[-1], then it's
something on the order of O($keeprecs * N). In the best case scenario,
with all records falling above the current max, it's O(N). In the real
world, who knows, but but it should be reasonably fast.

In any case, the key to a project like this is to figure out quickly
whether you want to keep a particular peice of data, and spend as
little time as possible on the out of bounds data which, if you want
1000 records out of 1,000,000 should be the vast majority of it.

HTH

-- jay
--
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!


Re: Which structure to handle data

2006-04-03 Thread John W. Krahn
Andrej Kastrin wrote:
> Dear Perl community,

Hello,

> I need to re-sort a set of data. I think that the below example is self
> explained; so which Perl structure should I use to handle this dataset?
> 
> Thanks in advance for any suggestion, Andre
> 
> 
> 2;John;Apple;Banana
> 3;Andrew;Pear;Apple;Melon;Orange
> 8;Susan;Pear;Melon
> 
> 2;John;Apple
> 2;John;Banana
> 3;Andrew;Pear
> 3;Andrew;Apple
> 3;Andrew;Melon
> 3;Andrew;Orange
> 8;Susan;Pear
> 8;Susan;Melon

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

print
sort
map {
chomp;
my ( $number, $name, @fruit ) = split /;/;
map "$number;$name;$_\n", @fruit;
}
;

__DATA__
2;John;Apple;Banana
3;Andrew;Pear;Apple;Melon;Orange
8;Susan;Pear;Melon




John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Which structure to handle data

2006-04-03 Thread Dr.Ruud
Andrej Kastrin schreef:

> I need to re-sort a set of data. I think that the below example is
> self explained; so which Perl structure should I use to handle this
> dataset?
>
> Thanks in advance for any suggestion, Andre
>
>
> 2;John;Apple;Banana
> 3;Andrew;Pear;Apple;Melon;Orange
> 8;Susan;Pear;Melon
>
> 2;John;Apple
> 2;John;Banana
> 3;Andrew;Pear
> 3;Andrew;Apple
> 3;Andrew;Melon
> 3;Andrew;Orange
> 8;Susan;Pear
> 8;Susan;Melon

This looks like a normalization step. I assume the first shape is in a
csv-like-file?

See `perldoc perldsc`.

The Persons could be put in their own table: 2 => John, 3 => Andrew,
etc.
The Fruits too, it all depends on how deep you want (or need) to go.
The data structure for your 2nd shape could be an array of arrays.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @fruit= qw( Apple Banana Pear Melon Orange );

my @person   = ();
$person[ 2 ] = 'John';
$person[ 3 ] = 'Andrew';
$person[ 8 ] = 'Susan';

my @data = ();
$data[ 0 ]   = [ $person[2], [ $fruit[0]
 , $fruit[1]
 ]
   ];
$data[ 1 ]   = [ $person[3], [ $fruit[2]
 , $fruit[0]
 , $fruit[3]
 , $fruit[4]
 ]
   ];
$data[ 2 ]   = [ $person[8], [ $fruit[2]
 , $fruit[4]
 ]
   ];

print Data::Dumper->Dump( [[EMAIL PROTECTED]   ]
, [ qw( *data ) ]
);

my @new = ();

$new[0] = [ 2, 0 ];
$new[1] = [ 2, 1 ];
$new[2] = [ 3, 2 ];
# etc.

print Data::Dumper->Dump( [ map { [ $$_[0]
  , $person[$$_[0]]
  , $fruit [$$_[1]]
  ]
}
@new
  ]
, [ qw( *new ) ]
);


(I couldn't get that last Dump right, it says 'VAR2' where it should
continue 'new'.)

Do you need to answer questions like: What Fruit does Andrew have? Which
Persons have a Pear? For that you could use a key on Person, and a key
on Fruit, which means Hashes. The best structure is the one that
produces your answers.

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




which subroutine

2006-04-03 Thread The Ghost
based upon the string in a variable, I want to run a particular  
subroutine:


my $var='cat';



$var='fish';

&$var; # I want to run fish if $var is a fish or cat if $var is a cat...

sub cat {  };
sub dog {  };

sub fish {  };

For some reason I think I'll be told this is a bad idea.  So what are  
my options?


Ryan

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: which subroutine

2006-04-03 Thread Tom Phoenix
On 4/3/06, The Ghost <[EMAIL PROTECTED]> wrote:

> based upon the string in a variable, I want to run a particular
> subroutine:

One way to do this (without the dreaded soft reference) is to have a
lookup table:

my %table = (
cat => \&cat,
jackalope => \&jackalope,
yeti => \&yeti,
);

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: simple profiling?

2006-04-03 Thread Bryan R Harris


> On 4/1/06, Bryan Harris <[EMAIL PROTECTED]> wrote:
> snip
>> This looks very interesting...  I downloaded it, but I have no idea how to
>> install it, though.  I'm a modules-idiot.  I tried putting the .pm file in
>> the current directory and putting "use TimeTick.pm;" at the beginning of my
>> code, but it doesn't work.  I'd like to be able to just haul the .pm file
>> around with my script and not have to "install" it on each machine I use...
>> 
>> Thanks!
>> 
>> - Bryan
> 
> Steps to install on UNIX style machines
> 
> The automated way
> 
> perl -MCPAN -e install Time::TimeTick


Brian, any ideas why this doesn't work?

**
% perl -MCPAN -e install Devel::SmallProf
% perl -d:SmallProf ./pma base_2.pmai
Can't locate Devel/SmallProf.pm in @INC (@INC contains: /sw/lib/perl5
/sw/lib/perl5/darwin /System/Library/Perl/5.8.1/darwin-thread-multi-2level
/System/Library/Perl/5.8.1 /Library/Perl/5.8.1/darwin-thread-multi-2level
/Library/Perl/5.8.1 /Library/Perl
/Network/Library/Perl/5.8.1/darwin-thread-multi-2level
/Network/Library/Perl/5.8.1 /Network/Library/Perl .).
BEGIN failed--compilation aborted.
**

- B



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: which subroutine

2006-04-03 Thread Mr. Shawn H. Corey
On Mon, 2006-03-04 at 15:41 -0500, The Ghost wrote:
> based upon the string in a variable, I want to run a particular  
> subroutine:
> 
> my $var='cat';
> 
> 
> 
> $var='fish';
> 
> &$var; # I want to run fish if $var is a fish or cat if $var is a cat...
> 
> sub cat {  };
> sub dog {  };
> 
> sub fish {  };
> 
> For some reason I think I'll be told this is a bad idea.  So what are  
> my options?
> 
> Ryan
> 

Yes, it is a bad idea. If $var comes from user input, it is tainted (see
`perldoc perlsec` for details). That means the user can run *ANY*
subroutine. If, for example, you had a subroutine that create SQL Select
statements, the user could abduct it to read any table in your database.
Not good.

You could do as Tom Phoenix suggested and create a dispatch hash. Or you
could do this:

  if( $var eq 'cat' ){
cat( @some_args );
  }elsif( $var eq 'dog' ){
dog( @other_args );
pooper_scooper();
  }else{
die "unknown life-form $var\n";
  }

This would allow you to send arguments to the subroutines. It also
allows you to call more than one subroutine for a single $var.


-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Automatically scheduling the execution of a sub-routine

2006-04-03 Thread James Turnbull

Hi

Anyone know of a way to create a loop (or something similar) that 
automatically schedules the execution of a sub-routine periodically from 
within a program, for example execute check() every 600 seconds or the 
like?  The program would be running as a daemon on the host.


Thanks

James Turnbull

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread James Turnbull

James Turnbull wrote:

Hi

Anyone know of a way to create a loop (or something similar) that 
automatically schedules the execution of a sub-routine periodically 
from within a program, for example execute check() every 600 seconds 
or the like?  The program would be running as a daemon on the host.



Sorry - probably didn't make that clear:

The mainline program is monitoring something - every x seconds I wish to 
execute a subroutine from within the mainline and return to the mainline 
after executing the subroutine to continue the monitoring. 


Regards

James Turnbull



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread Omega -1911
Your best bet is to use a cron job for this. Otherwise, you'd waste server
resources. What happens when the process is killed or the server restarted?

On 4/3/06, James Turnbull <[EMAIL PROTECTED]> wrote:
>
> Hi
>
> Anyone know of a way to create a loop (or something similar) that
> automatically schedules the execution of a sub-routine periodically from
> within a program, for example execute check() every 600 seconds or the
> like?  The program would be running as a daemon on the host.
>
> Thanks
>
> James Turnbull
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
>
>
>


Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread Tom Phoenix
On 4/3/06, James Turnbull <[EMAIL PROTECTED]> wrote:

> The mainline program is monitoring something - every x seconds I wish to
> execute a subroutine from within the mainline and return to the mainline
> after executing the subroutine to continue the monitoring.

So, it sounds as if you want your mainline program to have an event
loop. One way might be like this:

  while (1) {
&monitor_something();
&call_sub_if_x_seconds_have_passed();
  }

But from the way you're writing about it, I wonder whether something
like POE might be the way to go. (It doesn't sound as if you need
fork/threads, so I won't mention those.)

http://search.cpan.org/~rcaputo/POE-0.3301/lib/POE.pm

Also, CPAN has a section for "Server Daemon Utilities", although I'm
not sure whether this is what you're looking for:

http://search.cpan.org/modlist/Server_Daemon_Utilities

I hope this gives you something to go on. Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread Mr. Shawn H. Corey
On Tue, 2006-04-04 at 12:52 +1000, James Turnbull wrote:
> James Turnbull wrote:
> > Hi
> >
> > Anyone know of a way to create a loop (or something similar) that 
> > automatically schedules the execution of a sub-routine periodically 
> > from within a program, for example execute check() every 600 seconds 
> > or the like?  The program would be running as a daemon on the host.
> >
> Sorry - probably didn't make that clear:
> 
> The mainline program is monitoring something - every x seconds I wish to 
> execute a subroutine from within the mainline and return to the mainline 
> after executing the subroutine to continue the monitoring. 
> 
> Regards
> 
> James Turnbull

First question: are you running under M$ Windows or UNIX?

Second question: does this periodic function relying on data of the main
process?


-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread James Turnbull

Mr. Shawn H. Corey wrote:

First question: are you running under M$ Windows or UNIX?
  

Unix - Linux or BSD generally

Second question: does this periodic function relying on data of the main
process?
  

Yes - it uses a hash defined in the mainline.

Regards

James Turnbull


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




hashref slices

2006-04-03 Thread Ryan Perry

I wanted to use a hash slice, but I'm using a hashref.  Can I do both?

my @current_Flags=(  $hormone . 'DoseCycle', anotherVar,  
somethingElse );
$flags->[EMAIL PROTECTED]>selectrow_array(qq{$SQLstmt});  
#returns an array, $flags is my hashref


Thanks!


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: which subroutine

2006-04-03 Thread Dr.Ruud
"Tom Phoenix" schreef:

> my %table = (
> cat => \&cat,
> jackalope => \&jackalope,
> yeti => \&yeti,
> );


An alternative is to build that with eval:

my %table;
eval '$table{' . $_ . '} = \&' . $_ 
   for qw(cat jackalope yeti);

-- 
Affijn, Ruud

"Gewoon is een tijger."

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: hashref slices

2006-04-03 Thread John W. Krahn
Ryan Perry wrote:
> I wanted to use a hash slice, but I'm using a hashref.  Can I do both?

Yes.

> my @current_Flags=(  $hormone . 'DoseCycle', anotherVar,  somethingElse );
> $flags->[EMAIL PROTECTED]>selectrow_array(qq{$SQLstmt});  #returns

@{ $flags }{ @current_Flags } = $dbh->selectrow_array->( $SQLstmt );


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread John W. Krahn
James Turnbull wrote:
> Hi

Hello,

> Anyone know of a way to create a loop (or something similar) that
> automatically schedules the execution of a sub-routine periodically from
> within a program, for example execute check() every 600 seconds or the
> like?  The program would be running as a daemon on the host.

perldoc -f alarm


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: which subroutine

2006-04-03 Thread Tom Phoenix
On 4/3/06, Dr.Ruud <[EMAIL PROTECTED]> wrote:

> my %table;
> eval '$table{' . $_ . '} = \&' . $_
>for qw(cat jackalope yeti);

myeyesmyeyesthegogglesdoNOTHING

When I posted my code, I said that I wrote it that way to avoid using
"the dreaded soft reference". But using a soft reference is far better
than using the abominable and truly wretched text eval.

Why is text eval so bad? Because it can accidentally turn data into
code. Beginners, especially, should be cautioned against text eval.
It's worse than using goto: goto can only branch to a label, but text
eval can do anything Perl can do.

If you truly must use the text eval, minimize it:

  $table{$_} = eval '\&' . $_
for qw{ cat jackalope yeti };

But using the soft reference is a big step up, because it can't
accidentally turn data into code:

  {  no strict 'refs';
$table{$_} = \&$_ for qw{ cat jackalope yeti };
  }

In the end, yes, your code works, for sufficiently small values of
"works". But the text eval is a dangerous and powerful beast, not
easily tamed. Avoid avoid avoid.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: simple profiling?

2006-04-03 Thread Owen
Bryan R Harris wrote:
> 
>>On 4/1/06, Bryan Harris <[EMAIL PROTECTED]> wrote:
>>snip
>>
>>>This looks very interesting...  I downloaded it, but I have no idea how to
>>>install it, though.  I'm a modules-idiot.  I tried putting the .pm file in
>>>the current directory and putting "use TimeTick.pm;" at the beginning of my
>>>code, but it doesn't work.  I'd like to be able to just haul the .pm file
>>>around with my script and not have to "install" it on each machine I use...
>>>
>>>Thanks!
>>>
>>>- Bryan
>>
>>Steps to install on UNIX style machines
>>
>>The automated way
>>
>>perl -MCPAN -e install Time::TimeTick
> 
> 
> 
> Brian, any ideas why this doesn't work?
> 
> **
> % perl -MCPAN -e install Devel::SmallProf
> % perl -d:SmallProf ./pma base_2.pmai
> Can't locate Devel/SmallProf.pm in @INC (@INC contains: /sw/lib/perl5
> /sw/lib/perl5/darwin /System/Library/Perl/5.8.1/darwin-thread-multi-2level
> /System/Library/Perl/5.8.1 /Library/Perl/5.8.1/darwin-thread-multi-2level
> /Library/Perl/5.8.1 /Library/Perl
> /Network/Library/Perl/5.8.1/darwin-thread-multi-2level
> /Network/Library/Perl/5.8.1 /Network/Library/Perl .).
> BEGIN failed--compilation aborted.
> **



you need to install Devel::SmallProf is my guess


Owen

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread Mr. Shawn H. Corey
On Tue, 2006-04-04 at 13:07 +1000, James Turnbull wrote:
> Mr. Shawn H. Corey wrote:
> > First question: are you running under M$ Windows or UNIX?
> >   
> Unix - Linux or BSD generally
> > Second question: does this periodic function relying on data of the main
> > process?
> >   
> Yes - it uses a hash defined in the mainline.
> 
> Regards
> 
> James Turnbull
> 

OK, this sounds like an ideal application of threads. I don't have much
experience with them, so I'll leave it to others to tell you how to use
them.

But if your Perl wasn't compiled with threads:

First, read `perldoc perlipc`.

I assume your mainline is waiting on input. Like:

  while( <> ){
...
  }

If you use alarm, then these waits will be interrupted by the alarm. So
you need a flag to indicate this:

  my $alarmed = 0;

  sub wakeup {
$alarmed = 1;
# the rest of wakeup
alarm( 600 );
  }

  ...

  alarm( 600 );
  while( <> ){
if( $alarmed ){
  $alarmed = 0;
  next;
}
...
  }

 

-- 
__END__

Just my 0.0002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them, we learn by doing them."
  Aristotle

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Automatically scheduling the execution of a sub-routine

2006-04-03 Thread nishanth ev

Hello,

Just create a daemon and then put an infinite loop.
Call the subroutines with a sleep set accordingly, so
that the subroutine will run only at specified
interval.
Below is an sample code you can use.

Program will run as a daemon and you can kill it using
kill -9 

Regards
Nishanth


#!/usr/bin/perl
use POSIX qw(setsid);

chdir '/';
umask 0;
open STDIN, '/dev/null';
#open STDOUT, '>/dev/null';
open STDERR, '>/dev/null';
defined(my $pid = fork);
setsid;

while(1) {

#Sleep for the amount of time it should not respond.

   sleep(5);
   &subroutine;
}
sub subroutine{
print "Test\n";
}

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: newbie sort question

2006-04-03 Thread Val Genova
thanks

this solved my problem

my @output_sorted = sort { (split /,/, $b)[0] <=>  (split /,/, $a)[0] }
@output;

thanks to all that helped
Jeff Pang <[EMAIL PROTECTED]> writes:
>
>>this is my friend's script
>>
>># collect all score
>>  my @output = ();
>>  my @old_output = ();
>>  foreach my $list (@bugkillers) {
>>my ($id,$name) = split(/,/, $list);
>>my $score =
>>$Bugs->getSCORE($showold,$id,$contest,$pContest,$groups);
>>push(@output,"$score,$id,$name");
>>  }
>>  # print result
>>  foreach my $result (sort {$b <=> $a} @output) {
>>my ($score,$id,$name) = split(/,/, $result);
>>$html.=<>
>
>Hi,you have some mistakes when sorting the array.Because your array
>@output include the mixed elements,not just the numbers,so you get the
>bad result when sorting them with '<=>'.
>
>I would suggest you change the style of @output as:
>
>put @output,[$score,$id,$name];
>
>Now you can sort them by the array's NO.1 element (which is score) via
>accessing array's ref:
>
>for (sort {$b->[0] <=> $a->[0]} @output){
>  ...
>}
>
>
>Hope it helps and warning for no test.
>
>
>
>
>--
>Jeff Pang
>NetEase AntiSpam Team
>http://corp.netease.com



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




anonymize function return

2006-04-03 Thread Vadim Kutchin
Hi!

I have some function, such as:

===
sub func1 {
my (%rez);

$rez{one} = 'one';
$rez{two} = 'two';
$rez{seven} = 'seven';

return %rez;
}
===

and I have such piece of code:

===
%rez = func1;
$val = $rez{two};

print $val;
===

I want to avoid using %rez in second piece of code, parse output of func1 in one
step.

How?

Thanks!


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: anonymize function return

2006-04-03 Thread Jeff Pang

>===
>sub func1 {
>my (%rez);
>
>$rez{one} = 'one';
>$rez{two} = 'two';
>$rez{seven} = 'seven';
>
>return %rez;
>}
>===
>
>and I have such piece of code:
>
>===
>%rez = func1;
>$val = $rez{two};
>
>print $val;
>===
>
>I want to avoid using %rez in second piece of code, parse output of func1 in 
>one
>step.
>
>How?
>


Hello.The simple way is using a package variable (which is global in all your 
code) for your %rez.
For example,you could declare your variable as:

our %rez;

or

use vars qw/%rez/;

Then you could access the %rez anywhere in your code.

But,use an unnecessary variable outside its score is not good programming 
practice.I would suggest you return a hash ref instead of a hash in your 
subroutine,then the code is more clear.

sub func1 {
my (%rez);

$rez{one} = 'one';
$rez{two} = 'two';
   $rez{seven} = 'seven';

   return \%rez;
}

Then you could access the hash via its ref:

my $hash_ref = func1;
print $hash_ref->{one};

Hope this helps.



--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]