Re: PERL VS MOD_PERL

2007-07-04 Thread Chas Owens

On 7/5/07, Dinesh kumar <[EMAIL PROTECTED]> wrote:

Hi all,

  can any one tell me what is the difference between perl and
mod_perl in detail ??


Four letters (mod_).  Seriously, mod_perl is a Perl interpreter built
into Apache to enable some efficiency hacks.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: PERL VS MOD_PERL

2007-07-04 Thread Jeff Pang

There are some difference details between them.If you really want to
know it,you may first need to see what's mod_perl and how it
works.Take a look at:

mod_perl official documents:
http://perl.apache.org/docs/index.html

Why mod_perl by Stas Bekman:
http://www.perl.com/pub/a/2002/02/26/whatismodperl.html


2007/7/5, Dinesh kumar <[EMAIL PROTECTED]>:

Hi all,

  can any one tell me what is the difference between perl and
mod_perl in detail ??


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




PERL VS MOD_PERL

2007-07-04 Thread Dinesh kumar
Hi all,

  can any one tell me what is the difference between perl and
mod_perl in detail ??


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: syntax error of some sort?

2007-07-04 Thread Prabu Ayyappan
@list = (Exchange,Filter,DNS,Domain);
sub stop_it {
$vm = $_[0];
print "$vm\n";
}
stop_it(@list)

  A semi-colon is missing in the list assignment.
   
  if you want to get the first value of the list then you have to use
   
  $_[0]
   
  Second Value means 
   
  $_[1] and so on...
   
  To get all the values from the list..
   
  sub stop_it {
(@vm) = @_;
print "@vm\n";
}
   
  get it from @_ and assign it to an array "@vm" .Then you can navigate through 
the array with foreach or some for loops
   
  @_ will have the arguments passed to the sub-routine.
   
   
  Hope this help you.
   
  Thanks,
  Prabu

"Joseph L. Casale" <[EMAIL PROTECTED]> wrote:
  Heh,

Clearly I need to sleep!
This doesn't even work either?


#!/usr/bin/perl -w
@list = (Exchange,Filter,DNS,Domain)

sub stop_it {
$vm = $_[0];
print "$vm\n";
}

stop_it(@list)



What is wrong here?
jlc







--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




 
-
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.

RE: syntax error of some sort?

2007-07-04 Thread Prabu Ayyappan
@list = (Exchange,Filter,DNS,Domain);
sub stop_it {
$vm = $_[0];
print "$vm\n";
}
stop_it(@list)

  A semi-colon is missing in the list assignment.
   
  if you want to get the first value of the list then you have to use
   
  $_[0]
   
  Second Value means 
   
  $_[1] and so on...
   
  To get all the values from the list..
   
  sub stop_it {
(@vm) = @_;
print "@vm\n";
}
   
  get it from @_ and assign it to an array "@vm" .Then you can navigate through 
the array with foreach or some for loops
   
  @_ will have the arguments passed to the sub-routine.
   
   
  Hope this help you.
   
  Thanks,
  Prabu

"Joseph L. Casale" <[EMAIL PROTECTED]> wrote:
  Heh,

Clearly I need to sleep!
This doesn't even work either?


#!/usr/bin/perl -w
@list = (Exchange,Filter,DNS,Domain)

sub stop_it {
$vm = $_[0];
print "$vm\n";
}

stop_it(@list)



What is wrong here?
jlc







--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




   
-
Looking for a deal? Find great prices on flights and hotels with Yahoo! 
FareChase.

Re: syntax error of some sort?

2007-07-04 Thread Chas Owens

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:

Heh, I am running out of hair:P
I get two errors to start, one is the warning that is better write:
my ($vm) = $_[0];
instead of
my ($vm) = @_[0];


The proper way to say this is

my ($vm) = @_;

And looking at your data I would suggest

my ($vm, @disks) = @_;

snip

@list = (Exchange,Filter,DNS,Domain)

snip

And there are unspecified errors at 9 and 13?
I cant see what else is wrong in the sub?

snip

You are missing quotes and a semicolon.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: syntax error of some sort?

2007-07-04 Thread Chas Owens

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:

Heh,

Clearly I need to sleep!
This doesn't even work either?


#!/usr/bin/perl -w
@list = (Exchange,Filter,DNS,Domain)

snip

You are missing your quotes.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: syntax error of some sort?

2007-07-04 Thread Joseph L. Casale
Heh,

Clearly I need to sleep!
This doesn't even work either?


#!/usr/bin/perl -w
@list = (Exchange,Filter,DNS,Domain)

sub stop_it {
$vm = $_[0];
print "$vm\n";
}

stop_it(@list)



What is wrong here?
jlc







--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: syntax error of some sort?

2007-07-04 Thread Joseph L. Casale
Heh, I am running out of hair:P
I get two errors to start, one is the warning that is better write:
my ($vm) = $_[0];
instead of
my ($vm) = @_[0];

And the other is about the use of the global @_ (huh) with "my"?

I have this now:

#!/usr/bin/perl -w
@Exchange = ("/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-EX (Win2003 
Ent x64)/AN-EX (Win2003 Ent 
x64).vmx","/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-EX (Win2003 Ent 
x64)/Disc-1.vmdk","/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-EX 
(Win2003 Ent x64)/Disk-2.vmdk");
@Filter = ("/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/ESVA 1.7.1.5/ESVA 
1.7.1.5.vmx","/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/ESVA 
1.7.1.5/ESVA 1.7.1.5.vmdk");
@DNS = ("/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & DNS (Win2003 
Std x32)/Web & DNS (Win2003 Std 
x32).vmx","/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & DNS (Win2003 
Std x32)/Disc1.vmdk","vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & 
DNS (Win2003 Std x32)/Disc2.vmdk");
@Domain = ("/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-DC (Win2003 
Ent x64)/AN-DC (Win2003 Ent 
x64).vmx","/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-DC (Win2003 Ent 
x64)/Disk-1.vmdk");
@list = (Exchange,Filter,DNS,Domain)

sub stop_it {
$vm = $_[0];
$state = `/usr/bin/vmware-cmd \""$vm"\" getstate -q`;
chomp($state);
print "$state\n";
}

stop_it(@DNS);

And there are unspecified errors at 9 and 13?
I cant see what else is wrong in the sub?
Thanks!
jlc




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: syntax error of some sort?

2007-07-04 Thread Joseph L. Casale
OK Chase,
I saw the missing quotes:)
I sniped out the later work using the other elements of the array as to much 
was broken! I do want to pass in an array and work with all of it.
So if I wanted to work with only the first indices to start, is this correct:
sub stop_it {
my ($vm) = @_[0];
my $state = `/usr/bin/vmware-cmd \""$vm"\" getstate -q`;
.
.
.
}
But, the system executed command needs to see "itself" that is, quotes around 
the value of $vm. Basically I need to pass the shell this:
Vmware-cmd "/path/to/file/name.vmx" getstate -q
So did I write that correct for both situations?





In your next email, you say this:
sub stop_it {
for my $vm (@_) {
my $state = `/usr/bin/vmware-cmd "$vm" getstate -q`;
.
.
.
}
}

But I don't want to pass all indices to each section in the sub, each section 
does something with either one, or a specific few of the indices, later I 
direct for example, 2 and 3 into a loop.

Thanks!
jlc



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: syntax error of some sort?

2007-07-04 Thread Chas Owens

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:
snip

Is it actually incorrect to run: stop_it(@DNS); aside from maybe looking 
strange?

snip

It doesn't look strange, but your code only works with the first
element of @DNS so it is pointless.  If you want to say stop_it(@DNS)
and actually do something with the the other values in @DNS you will
need a loop inside the sub:

sub stop_it {
   for my $vm (@_) {
   my $state = `/usr/bin/vmware-cmd "$vm" getstate -q`;
   .
   .
   .
   }
}

If you really only want to work with the first value in @DNS then you should say

stop_it($DNS[0]);

to avoid confusing people.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: syntax error of some sort?

2007-07-04 Thread Joseph L. Casale
Thanks for the help. The while loop move makes sense! I knew this looked silly, 
but with the rest of the code, I am looping in many arrays. In this section I 
am only working with the first indice (the .vmx one).

Is it actually incorrect to run: stop_it(@DNS); aside from maybe looking 
strange?

So the `/usr/bin/vmware-cmd \"$_[0]\" gettoolslastactive -q`; is correct where 
the var gets inserted?

Thanks!
jlc

-Original Message-
From: Chas Owens [mailto:[EMAIL PROTECTED]
Sent: Wednesday, July 04, 2007 8:15 PM
To: Joseph L. Casale
Cc: beginners@perl.org
Subject: Re: syntax error of some sort?

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:
snip
> if ($state == 'on') {
> 'vmware-cmd $_[0] stop soft';
> my $tools = `/usr/bin/vmware-cmd \"$_[0]\" 
> gettoolslastactive -q`;
> chomp($tools);
> }
> while (($state == 'on') && ($tools == 1)) {
> sleep 30;
> my $tools = `/usr/bin/vmware-cmd \"$_[0]\" 
> gettoolslastactive -q`;
> chomp($tools);
> }
> }
snip
> I am getting two errors I think, and I don't understand them.
> The first is the second use of my $tools, how can I do this
> without causing an error? Can I just start using $tools right after without 
> my?

The while belongs inside of the if like this:

   if ($state == 'on') {
   'vmware-cmd $_[0] stop soft';
   my $tools = `/usr/bin/vmware-cmd
\"$_[0]\" gettoolslastactive -q`;
   chomp($tools);

   while ($tools == 1) {
  sleep 30;
  $tools =
`/usr/bin/vmware-cmd \"$_[0]\" gettoolslastactive -q`;
  chomp($tools);
  }
   }

snip
> The second I am sure is the darn () in the file names I am feeding
> into the function. Can someone shed some light :) I need to call the
> first indices of many arrays that get passed into this function, so I
> was thinking $_[0] was what I need. Problem is that the execution
> of vmware-cmd needs the value of $_[0] passed to it inside quotes.
snip

In your example $_[0] is
"/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & DNS (Win2003
Std x32)/Web & DNS (Win2003 Std x32).vmx"

If you want to perform stop_it on or with many values you will need to
put a loop outside or inside the sub.  Given the way your code is
currently written this should work:

stop_it($_) for @DNS;





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: syntax error of some sort?

2007-07-04 Thread Chas Owens

On 7/4/07, Chas Owens <[EMAIL PROTECTED]> wrote:
snip

snip
> The second I am sure is the darn () in the file names I am feeding
> into the function. Can someone shed some light :) I need to call the
> first indices of many arrays that get passed into this function, so I
> was thinking $_[0] was what I need. Problem is that the execution
> of vmware-cmd needs the value of $_[0] passed to it inside quotes.
snip

snip

also,   'vmware-cmd $_[0] stop soft'; is missng the quotes around $_[0].

And while I am thinking about it, Don't use $_[0].  It is good form to
handle assigning arguments to lexical variables early in a subroutine:

sub stop_it {
   my ($vm) = @_;
   my $state = `/usr/bin/vmware-cmd "$vm" getstate -q`;
   .
   .
   .
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: syntax error of some sort?

2007-07-04 Thread Chas Owens

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:
snip

if ($state == 'on') {
'vmware-cmd $_[0] stop soft';
my $tools = `/usr/bin/vmware-cmd \"$_[0]\" 
gettoolslastactive -q`;
chomp($tools);
}
while (($state == 'on') && ($tools == 1)) {
sleep 30;
my $tools = `/usr/bin/vmware-cmd \"$_[0]\" 
gettoolslastactive -q`;
chomp($tools);
}
}

snip

I am getting two errors I think, and I don't understand them.
The first is the second use of my $tools, how can I do this
without causing an error? Can I just start using $tools right after without my?


The while belongs inside of the if like this:

  if ($state == 'on') {
  'vmware-cmd $_[0] stop soft';
  my $tools = `/usr/bin/vmware-cmd
\"$_[0]\" gettoolslastactive -q`;
  chomp($tools);

  while ($tools == 1) {
 sleep 30;
 $tools =
`/usr/bin/vmware-cmd \"$_[0]\" gettoolslastactive -q`;
 chomp($tools);
 }
  }

snip

The second I am sure is the darn () in the file names I am feeding
into the function. Can someone shed some light :) I need to call the
first indices of many arrays that get passed into this function, so I
was thinking $_[0] was what I need. Problem is that the execution
of vmware-cmd needs the value of $_[0] passed to it inside quotes.

snip

In your example $_[0] is
"/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & DNS (Win2003
Std x32)/Web & DNS (Win2003 Std x32).vmx"

If you want to perform stop_it on or with many values you will need to
put a loop outside or inside the sub.  Given the way your code is
currently written this should work:

stop_it($_) for @DNS;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




syntax error of some sort?

2007-07-04 Thread Joseph L. Casale
I have the following code (it's a snippet so it may seem silly, but altogether 
it would make sense as I am forcing one array into it for a test):

#!/usr/bin/perl -w
@DNS = ("/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & DNS (Win2003 
Std x32)/Web & DNS (Win2003 Std 
x32).vmx","/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & DNS (Win2003 
Std x32)/Disc1.vmdk","vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/Web & 
DNS (Win2003 Std x32)/Disc2.vmdk");

sub stop_it
{
my $state = `/usr/bin/vmware-cmd \"$_[0]\" getstate -q`;
chomp($state);
if ($state == 'off') {
exit 1;
}
if ($state == 'paused') {
print "VM Paused, aborting backup for this vm";
$error_level = '1';
exit 1;
}
if ($state == 'on') {
'vmware-cmd $_[0] stop soft';
my $tools = `/usr/bin/vmware-cmd \"$_[0]\" 
gettoolslastactive -q`;
chomp($tools);
}
while (($state == 'on') && ($tools == 1)) {
sleep 30;
my $tools = `/usr/bin/vmware-cmd \"$_[0]\" 
gettoolslastactive -q`;
chomp($tools);
}
}

stop_it(@DNS);

I am getting two errors I think, and I don't understand them.
The first is the second use of my $tools, how can I do this without causing an 
error? Can I just start using $tools right after without my?
The second I am sure is the darn () in the file names I am feeding into the 
function. Can someone shed some light :) I need to call the first indices of 
many arrays that get passed into this function, so I was thinking $_[0] was 
what I need. Problem is that the execution of vmware-cmd needs the value of 
$_[0] passed to it inside quotes.

Thanks!
jlc
jlc




Re: foreach broken in my script

2007-07-04 Thread Randal L. Schwartz
> ""Jeff" == "Jeff Pang" <[EMAIL PROTECTED]> writes:

"Jeff> May you need eval?Like,

No.  Wrong direction for a solution.  Don't suggest things like this.  Plenty
of proper answers elsewhere in the thread, so I won't repeat them.

DO NOT USE STRING EVAL.  EVER.

Until you understand why I said that. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: help needed!!

2007-07-04 Thread Tom Phoenix

On 7/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:


File1:example.txt

ProbeNames  Exp1Exp2Exp3Exp4Exp5
244901_at   24.346.437.419.559.6
244902_at   36.692.449.542.4  29.6
244903_at   46.419.4-   18.219.8
244904_at   56.229.478.437.8-



i want to find the average value for each Probe i.e each horizontal
line.For ex:Avg value for 244901_at is so and so,avg value for
244902_at is so and so..


It looks as if you're off to a good start; maybe you should use a
pattern match to describe your data?

 # untested code
 my $datum = qr{[\d\-\.]+};  # or something better for your data
 my $ident = qr{\w+};
 my $pattern = qr{
   ^($ident)# $1 is identifier at start of line
   \s+# whitespace
   (# $2 is list of data items
   (?:$datum\s+)*   # items before last item
   $datum# last item
   )
 }x;

 # later, when you're ready to read from your data file
 while ($line = ) {
   next unless $line =~ /\S/; # skip blank lines
   if ($line =~ /$pattern/) {
 my($ident, $data) = ($1, $2);
 next if $ident eq 'ProbeNames'; # skip headings
 my @data = split /\s+/, $data;
 my $average = &my_average(@data); # or whatever
 print "Probe $ident reports average $average\n";
 # on your own from here
   } else {
 warn "Unusable data found on line $. of file, continuing...\n";
   }
 }

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: help with syntax using an if statement

2007-07-04 Thread Ken Foskey
On Wed, 2007-07-04 at 19:00 +0200, Martin Barth wrote:
> Hi
> 
> > if (($DeviceType eq "Switch") || ($DeviceType eq "Router") ||
> > ($DeviceType eq "Hub") || ($DeviceType eq "Access point"))
> > 
> > 
> > what i would like to do is check each device type with where the first
> > letter is uppercase or lowercase
> 
> this should help:
> 
> if( $DeviceType =~ m/^([Ss]witch|[Rr]outer|[Hh]ub|[Aa]ccess point)$/) { ...

Knowing systems:

if( $DeviceType =~ m/^(switch|router|hub|access point)$/i ) {

The i modifier is ignore case.

I think there is a word boundary \< and \> that might be useful rather
than ^$ as well.

-- 
Ken Foskey
FOSS developer


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




help needed!!

2007-07-04 Thread [EMAIL PROTECTED]
oops sorry for the last mail...i pressed the enter button
inadvertently.
neways here is the problem...

File1:example.txt

ProbeNames  Exp1Exp2Exp3Exp4Exp5
244901_at   24.346.437.419.559.6
244902_at   36.692.449.542.4  29.6
244903_at   46.419.4-   18.219.8
244904_at   56.229.478.437.8-

ok Exp is the experiment name..Probes are under the ProbeNames and the
digits are the signal values..

i want to find the average value for each Probe i.e each horizontal
line.For ex:Avg value for 244901_at is so and so,avg value for
244902_at is so and so..

so what i did is took out the first line and the first column and used
split on  the values.My code is as follows:



#!/usr/bin/perl -w

$file1="mini.txt";
$output1="output1.txt";

open(FILE,"<$file1")or die "Unable to open file";
open(OT1,">$output1")or die "Unable to open file";

$count=0;
$totalcount=0;
$total=0;
while($line=)   #reading each line in the file.
 {
  chomp($line);
  if(!$count==0)
 {
  $line =~ s/"//g;   #taking out the delimiters(Quotations)
  my @fields=split(/\t/,$line); #getting a list of values.
 foreach $fields(@fields)
   {
if ($fields=~ m/_at/){}
else
{
print OT1 "$fields\n";
}
   }
}
$count++;
}
close(FILE);
close(OT1);

---
what do i do next??


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: ccomplex problem...

2007-07-04 Thread Tom Phoenix

On 7/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:


Hi all,
ok.i will go straight to the problem but d bear with me a it's
a complex problem .

first the thing that is to be done..

i have this file which is in a tab delimited format.This is a huge
file with 3000 experiments and having 23000 probes with each
experiment having one signal value for each probe...eg:

File1:example.txt

ProbeNames  Exp1Exp2Exp3Exp4Exp5
244901_at   24.346.437.419.559.6
244902_at   36.692.449.519.329.6
244903_at   46.419.4-   18.219.8
244904_at   56.229.478.437.8-


Is the problem that your example data file sends messages before
you're finished writing them?

For the Perl end of things, maybe you want split. But I suspect that's
not where you're stuck.

Want to try asking again? Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: File::Spec->splitpath

2007-07-04 Thread Joseph L. Casale
Dang, you perl guys thought of everything!
Thanks!
jlc


From: Chas Owens [EMAIL PROTECTED]
Sent: Wednesday, July 04, 2007 3:21 PM
To: Joseph L. Casale
Cc: Tom Phoenix; beginners@perl.org
Subject: Re: File::Spec->splitpath

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:
> Just found that. I suppose I could use it, then get the size of the array and 
> use the last indices? Is there a cleaner way to do it?
> Thanks!
> jlc
snip

You can use negative indices to count from the end of the array:

my @a = (1, 2, 3, 4, 5, 6);
die unless $a[-1] == $a[5] == 6;
die unless $a[-2] == $a[4] == 5;
die unless $a[-3] == $a[3] == 4;
die unless $a[-4] == $a[2] == 3;
die unless $a[-5] == $a[1] == 2;
die unless $a[-6] == $a[0] == 1;



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




ccomplex problem...

2007-07-04 Thread [EMAIL PROTECTED]
Hi all,
ok.i will go straight to the problem but d bear with me a it's
a complex problem .

first the thing that is to be done..

i have this file which is in a tab delimited format.This is a huge
file with 3000 experiments and having 23000 probes with each
experiment having one signal value for each probe...eg:

File1:example.txt

ProbeNames  Exp1Exp2Exp3Exp4Exp5
244901_at   24.346.437.419.559.6
244902_at   36.692.449.519.329.6
244903_at   46.419.4-   18.219.8
244904_at   56.229.478.437.8-


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Howto decode an arp entry to hex

2007-07-04 Thread John W. Krahn

Bernard wrote:

Hi,


Hello,

When I try to get the arp table from any type of device (oid 
1.3.6.1.2.1.4.22.1.3) with the snmp_util module, it  returns the 6 
character, instead of the hex byte value.


How can I convert a 6 byte word into the hex equivalent like 
00:0a:8b:bd:62:8a.


$ perl -le'
my $data = "\x00\x0a\x8b\xbd\x62\x8a";
( my $new = unpack "H*", $data ) =~ s/(..)(?=..)/$1:/g;
print $new;
'
00:0a:8b:bd:62:8a



Any help greatly appreciated

#!/usr/bin/perl


You should let perl help you too!

use warnings;
use strict;



use SNMP_util;


my $IPADDR = $ARGV[0] || die "Oops, no ipaddr specified";
my $COMM = $ARGV[1] || die "Oops, no community specified";
my $OID = $ARGV[2] || die "Oops, no oid specified";


Although that *will* technically work, you are using the wrong logical 
operator.  The logical or operator (||) has higher precedence than the 
assignment operator (=).


The usual idiom is to use '||' for assignment:

my $var = $ARGV[0] || 'some default value';

And to use 'or' for flow control:

my $var = $ARGV[0] or die "Opps\n";



print("Getting info for $IPADDR with comm $COMM ($OID)\n");

$HOST = $COMM . "@" . $IPADDR;
$SNMPVER = "1";
$SNMPSTR = "$HOST" . ":" . "$SNMPVER";
print("Getting table.\n");

$length = length($OID);
$oid = $OID;
   $oid = substr($oid,0,$length);


Why not simply:

my $oid = substr $OID, 0, length $OID;

Which is just a long way of saying:

my $oid = $OID;



print "Trying 'getnext' on $host with $oid\n";
while ( substr($oid,0,$length) eq $OID ) {
 my @ret = &snmpgetnext($SNMPSTR, "$oid");


You shouldn't prefix a subroutine name with an ampersand.

perldoc perlsub



 foreach my $desc (@ret) {
   ($oid, $desc) = split(':', $desc, 2);
   $testStr = substr($oid,0,$length);
   if ( substr($oid,0,$length) eq $OID ) {
 print "$oid = $desc ($testStr)')\n";
   }
 }
}




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File::Spec->splitpath

2007-07-04 Thread Chas Owens

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:

Just found that. I suppose I could use it, then get the size of the array and 
use the last indices? Is there a cleaner way to do it?
Thanks!
jlc

snip

You can use negative indices to count from the end of the array:

my @a = (1, 2, 3, 4, 5, 6);
die unless $a[-1] == $a[5] == 6;
die unless $a[-2] == $a[4] == 5;
die unless $a[-3] == $a[3] == 4;
die unless $a[-4] == $a[2] == 3;
die unless $a[-5] == $a[1] == 2;
die unless $a[-6] == $a[0] == 1;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Howto decode an arp entry to hex

2007-07-04 Thread Bernard

Tom,

Many thanks, works great.

Bernard

Tom Phoenix wrote:

On 7/4/07, Bernard <[EMAIL PROTECTED]> wrote:


When I try to get the arp table from any type of device (oid
1.3.6.1.2.1.4.22.1.3) with the snmp_util module, it  returns the 6
character, instead of the hex byte value.

How can I convert a 6 byte word into the hex equivalent like
00:0a:8b:bd:62:8a.


Maybe you want something like this?

 sub make_hex_string {
   my $str = unpack "H*", shift @_;
   $str =~ s/(..)/:$1/g;
   $str =~ s/^://;
   $str;
 }

But if the module you're using doesn't supply the function you need,
maybe a different module would help.

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Howto decode an arp entry to hex

2007-07-04 Thread Tom Phoenix

On 7/4/07, Bernard <[EMAIL PROTECTED]> wrote:


When I try to get the arp table from any type of device (oid
1.3.6.1.2.1.4.22.1.3) with the snmp_util module, it  returns the 6
character, instead of the hex byte value.

How can I convert a 6 byte word into the hex equivalent like
00:0a:8b:bd:62:8a.


Maybe you want something like this?

 sub make_hex_string {
   my $str = unpack "H*", shift @_;
   $str =~ s/(..)/:$1/g;
   $str =~ s/^://;
   $str;
 }

But if the module you're using doesn't supply the function you need,
maybe a different module would help.

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File::Spec->splitpath

2007-07-04 Thread Tom Phoenix

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:


Just found that. I suppose I could use it, then get the size of the array and
use the last indices? Is there a cleaner way to do it?


pop?

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: File::Spec->splitpath

2007-07-04 Thread Joseph L. Casale
Just found that. I suppose I could use it, then get the size of the array and 
use the last indices? Is there a cleaner way to do it?
Thanks!
jlc

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tom Phoenix
Sent: Wednesday, July 04, 2007 1:38 PM
To: Joseph L. Casale
Cc: beginners@perl.org
Subject: Re: File::Spec->splitpath

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:

> ($volume,$directories,$file) = 
> File::Spec->splitpath( $path );
>
> I am leveraging this fine, but would like to shove the last directory from 
> the path in $directories into a new var.
> Is there a function from this module that can do this cleanly?

Do you mean splitdir?

Hope this helps!

--Tom Phoenx
Stonehenge Perl Training





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Howto decode an arp entry to hex

2007-07-04 Thread Bernard

Hi,

When I try to get the arp table from any type of device (oid 
1.3.6.1.2.1.4.22.1.3) with the snmp_util module, it  returns the 6 
character, instead of the hex byte value.


How can I convert a 6 byte word into the hex equivalent like 
00:0a:8b:bd:62:8a.


Any help greatly appreciated

Bernard

Below, a sample program to show the results.


#!/usr/bin/perl
use SNMP_util;


my $IPADDR = $ARGV[0] || die "Oops, no ipaddr specified";
my $COMM = $ARGV[1] || die "Oops, no community specified";
my $OID = $ARGV[2] || die "Oops, no oid specified";
print("Getting info for $IPADDR with comm $COMM ($OID)\n");

$HOST = $COMM . "@" . $IPADDR;
$SNMPVER = "1";
$SNMPSTR = "$HOST" . ":" . "$SNMPVER";
print("Getting table.\n");

$length = length($OID);
$oid = $OID;
   $oid = substr($oid,0,$length);
print "Trying 'getnext' on $host with $oid\n";
while ( substr($oid,0,$length) eq $OID ) {
 my @ret = &snmpgetnext($SNMPSTR, "$oid");
 foreach my $desc (@ret) {
   ($oid, $desc) = split(':', $desc, 2);
   $testStr = substr($oid,0,$length);
   if ( substr($oid,0,$length) eq $OID ) {
 print "$oid = $desc ($testStr)')\n";
   }
 }
}



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Need Help Installing Win32:SerialPort

2007-07-04 Thread [EMAIL PROTECTED]
>
> ppm> install c:\temp\Win32-API-0.41\API.pm
> Searching for 'c:\temp\Win32-API-0.41\API.pm' returned
> no results. Try a broader search first.
>
> Can anyone please advise on what I need to do next?
>
> Thanks in advance.
>
> Amad.

the package should contain a file with a ppd extension.  cd to the
directory where you unpacked the file (where the *.ppd) file is and
than try
ppm install  whatever.ppd

or if you have a straight connection to the internet you can do
something like

ppm install http://whatever.repo.com/packageX/X.ppd


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: File::Spec->splitpath

2007-07-04 Thread Tom Phoenix

On 7/4/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:


($volume,$directories,$file) = 
File::Spec->splitpath( $path );

I am leveraging this fine, but would like to shove the last directory from the 
path in $directories into a new var.
Is there a function from this module that can do this cleanly?


Do you mean splitdir?

Hope this helps!

--Tom Phoenx
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




File::Spec->splitpath

2007-07-04 Thread Joseph L. Casale
Hi,
I am trying to use the following:


($volume,$directories,$file) = 
File::Spec->splitpath( $path );

I am leveraging this fine, but would like to shove the last directory from the 
path in $directories into a new var.
Is there a function from this module that can do this cleanly?

Thanks!
jlc




Re: Check if file is being modified by another process

2007-07-04 Thread Chas Owens

On 7/4/07, Chas Owens <[EMAIL PROTECTED]> wrote:

On 7/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
snip
> What's the most efficient way of checking this? - one way is perhaps
> inifinite loop checking mmtime until it is stable for a certain amount
> of time?? I am not sure.
snip

Yep, that is about it.  I usually use size rather than mtime (mtime
checking can be broken by someone monkeying with the system time, it
happens).

snip

Oh, I forgot about signal files.  Sometimes (especially with remote
transfers) you can persuade people to send/write a second (very small)
file that contains meta data about the file being sent (md5sum, number
of records, etc).  The existence of the small file signals that the
large file has finished being written.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: help with syntax using an if statement

2007-07-04 Thread Paul Lalli
On Jul 4, 3:46 am, [EMAIL PROTECTED] (Daluk) wrote:

> I have a some code that reads in a file, and then i have some if
> statements.  The if statement i want to change is
>
> if (($DeviceType eq "Switch") || ($DeviceType eq "Router") ||
> ($DeviceType eq "Hub") || ($DeviceType eq "Access point"))
>
> what i would like to do is check each device type with where the first
> letter is uppercase or lowercase
>
> i have tried a few regex syntax but i'm not getting there :(

Why do you need regexps?

> any advice for a beginner please

perldoc -f ucfirst

if (  ucfirst($DeviceType) eq 'Switch'
   || ucfirst($DeviceType) eq 'Router'
   || ucfirst($DeviceType) eq 'Hub'
   || ucfirst($DeviceType) eq 'Access point' ) {
  #do stuff
}

(this is one of many different ways of accomplishing your goal.
TIMTOWTDI)

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Check if file is being modified by another process

2007-07-04 Thread Tom Phoenix

On 7/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:


I have an application that creates and writes to an output file I need
to process. I need to process the file when it is completely written
to. I do not initially know how big the file will be in the end.
Further, the application does NOT put a write lock on the file while
it is writing it. because of the buffering, the program wirtes to the
file in random chunks not continuously. And what is worse, the file
format itself could vary so there is nothing in the actual file that
signals the end of it. Everything is on a linux server.

What's the most efficient way of checking this? - one way is perhaps
inifinite loop checking mmtime until it is stable for a certain amount
of time?? I am not sure.


That's probably the right road to choose. Choose some time interval
that's long enough to be sure the file is done, but not so long that
it results in undue impatience in whoever is waiting for the end
results.

An alternative might be if there is some way to spy on the process
doing the writing. This requires new interactions between the two
programs and the OS, making everything more fragile. But if you can
determine that the other process has finished execution, or in any
other way has closed its equivalent of a filehandle, you can probably
be certain that the writing is finished. Probably.

Still, I'd prefer polling, using the longest interval I could justify.

Is there absolutely no clue available, though? For example, you speak
of buffering; if a file's size isn't a multiple of the buffer size,
does that mean that it's finished? It may be that your application
will be happiest with nearly all data at the earliest possible moment,
even though one file out of 8192 will be delayed by an extra hour to
be sure that it's really finished. But it would be bad, even fatal,
for some applications to get data out-of-order. (Or does it write just
one file at a time, so that you know the first is done when the
second is starting?)

One frill you could add would reduce impatience by increasing the
polling frequency. It would take a lot of trouble, and maybe cause a
lot of trouble, so it's probably not worth it. But you could set the
polling interval to be arbitrarily quick, adding some code that would
recognize when some file has been updated and announcing "This
supersedes file #42", or whatever. It all depends upon whether clients
will be more troubled by missing information that is slow to come in,
or incomplete information that is quick to be updated (although
possibly still incomplete).

One last alternative comes to mind: Determine the supplier of the
mystery application, and use any means necessary to have appropriate
file locking or equivalent behavior added to their source code. If
your clients are impatient, it's transitive: they're impatient with
this other software, really.

In the end, the road you take depends upon where your clients'
impatience drives you.

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Check if file is being modified by another process

2007-07-04 Thread Chas Owens

On 7/4/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
snip

What's the most efficient way of checking this? - one way is perhaps
inifinite loop checking mmtime until it is stable for a certain amount
of time?? I am not sure.

snip

Yep, that is about it.  I usually use size rather than mtime (mtime
checking can be broken by someone monkeying with the system time, it
happens).  Something like

my @same;
until (@same == $seconds_the_same) {
   my $size = (stat file)[7];
   if ($same[0] == $size) {
   push @same, $size;
   } else {
   @same = ($size);
   }
   sleep 1;
}

or

my $old;
my $same = 0;
until ($same == $seconds_the_same) {
   my $size = (stat file)[7];
   if ($old == $size) {
   $same++;
   } else {
   ($old, $same) = ($size, 0);
   }
   sleep 1;
}

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: help with syntax using an if statement

2007-07-04 Thread Martin Barth
Hi

> if (($DeviceType eq "Switch") || ($DeviceType eq "Router") ||
> ($DeviceType eq "Hub") || ($DeviceType eq "Access point"))
> 
> 
> what i would like to do is check each device type with where the first
> letter is uppercase or lowercase

this should help:

if( $DeviceType =~ m/^([Ss]witch|[Rr]outer|[Hh]ub|[Aa]ccess point)$/) { ...

Martin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Need Help Installing Win32:SerialPort

2007-07-04 Thread Chas Owens

On 7/3/07, CM Analyst <[EMAIL PROTECTED]> wrote:
snip

ppm> install c:\temp\Win32-API-0.41\API.pm
Searching for 'c:\temp\Win32-API-0.41\API.pm' returned
no results. Try a broader search first.

snip

Firstly, ppm installs ppd (Perl Package Description) files not pm
(Perl Module) files and secondly PPM will download the ppd for a given
module for you.  All you need to say is

ppm.bat install Win32-API

If the target machine cannot reach the Internet, then you can manually
download the ppd for a given module here
http://ppm.activestate.com/PPMPackages/zips/ and install it with

cd \tmp
unzip Win32-API-0.46.zip
ppm.bat install c:\tmp\Win32-API.ppd

The following url has detailed instructions on how to use the GUI
version of ppm.

http://aspn.activestate.com/ASPN/docs/ActivePerl/5.8/faq/ActivePerl-faq2.html

snip

P.S. FWIW, I am using the "Perl Black Book" to find my
way around...

snip

Try Learning Perl (the Llama) and Programming Perl (the Camel).

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Check if file is being modified by another process

2007-07-04 Thread kyle . halberstam
Hi,

I have an application that creates and writes to an output file I need
to process. I need to process the file when it is completely written
to. I do not initially know how big the file will be in the end.
Further, the application does NOT put a write lock on the file while
it is writing it. because of the buffering, the program wirtes to the
file in random chunks not continuously. And what is worse, the file
format itself could vary so there is nothing in the actual file that
signals the end of it. Everything is on a linux server.

What's the most efficient way of checking this? - one way is perhaps
inifinite loop checking mmtime until it is stable for a certain amount
of time?? I am not sure.

Any help will be greatly appreciated. Thanks


Kyle


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




help with syntax using an if statement

2007-07-04 Thread Daluk
Hi ppl,

I have a some code that reads in a file, and then i have some if
statements.  The if statement i want to change is

if (($DeviceType eq "Switch") || ($DeviceType eq "Router") ||
($DeviceType eq "Hub") || ($DeviceType eq "Access point"))


what i would like to do is check each device type with where the first
letter is uppercase or lowercase

i have tried a few regex syntax but i'm not getting there :(

any advice for a beginner please

thanks
Dal


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: hash comparison ideas

2007-07-04 Thread [EMAIL PROTECTED]
On Jul 4, 2:05 am, [EMAIL PROTECTED] (John W. Krahn) wrote:
>
> > I have  a script which contains 2 hashes of file names as the keys and
> > md5 sums as the values. I am looking for ideas on fast and efficient
> > ways to compare the 2 hashes in the manner of the pseudo code below
>
> Can you fill us in on the big picture?  Perhaps you need a hash of arrays
> where the keys are the md5 sums and the values are arrays of file names?

the base hash is an initial collection of current file names and check
sums.  the script is kind of a poor man's incremental backup. here is
a snippet. the files_db hash (base hash) comes from a file
--
my %files;
find sub {
$path = $File::Find::dir;
(my $tpath = $path) =~ s/^c:/$target/;
mkpath ($tpath, {verbose => 1} ) unless -e $tpath;
if ( -f ) {
   # copy any files that don't exists at all on target
  (my $tname = $name) =~ s/^c:/$target/;
  copy ($name, $tpath) and print "copying $name\n"  unless -e
$tname;

  open F,  $_ or do {
  warn "Cannot open '$File::Find::name' $!";
  return;
  };

 binmode(F);
 my $digest = Digest::MD5->new;
 $digest->addfile(*F);
 my $sum = $digest->hexdigest;

$files{$name} = $sum;
if ( exists $files_db{$name} and ( $files{$name} ==
$files_db{$name} ) ) {

# **compare sums and do stuff here**
   }
}

}, $dir;

--

i am not sure how hash of arrays will help as each key = 1 sum  and
vice versa
Thanks for your help and any suggestions
Jim



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Problem with runaway format

2007-07-04 Thread Tom Phoenix

On 7/4/07, Mathew Snyder <[EMAIL PROTECTED]> wrote:


>> foreach my $user (keys %tickets) {
>> foreach my $env (keys %{ $tickets{$user} }) {
>> foreach my $tikID (keys %{ $tickets{$user}{$env} }) {
>> foreach my $subject (keys %{
>> $tickets{$user}{$env}{$tikID} }) {
>
> You're doing the same dereferencing, just getting deeper each time.
> You'll save some time if you make some of those into temporary
> variables. That fourth nested loop could look more like this, but
> possibly with better variable names:
>
>my $subjects_hash = $envs_hash->{$env};
>foreach my $subject (keys %$subjects_hash) {
>

Can you explain the reason for skipping the {$id} portion and assigning the
{$env} portion to %subjects_hash?  The logic eludes me.


Maybe I've put the parts together wrong, or named them poorly. But the
general idea is that most references are copied to scalars, so that
you don't have to dereference $tickets{$user} as part of every
variable you access within four nested loops. That's probably going to
give you clearer code, but it will also be faster to execute.

The outer loop looks like this:

 foreach my $user (keys %tickets) {

Since there's just one value for $user each time through the loop,
$tickets{$user} will have just one value each time through the loop.
So instead of doing that hash lookup again and again, we'll keep the
result in a scalar.

   my $users_tix = $tickets{$user};

Since the scalar is declared just inside the loop, its scope ends at
the same place that $user's scope ends. So any line of code that used
to use $tickets{$user} can now use $users_tix just as well. (If their
scopes ended in different places, those two expressions wouldn't
always be interchangeable.)

Of course, the scalar could have been named $tickets_for_user or
$current_users_tickets, or something that does a better job of
matching what it's really about. I'm not clear on the nature of the
overall task or I'd have more confidence in my choice of name. I hope
that the technique is clear enough.

So, from now on, whenever we need $tickets{$user}, we can use
$users_tix. And we need it in the next nested loop, which used to look
like this:

   foreach my $env (keys %{ $tickets{$user} }) {

But now it looks like this:

   foreach my $env (keys %{ $users_tix }) {

Or, because the curly braces are optional around a just-plain-scalar,
many people prefer this:

   foreach my $env (keys %$users_tix) {

Using the same trick another level down, we get the easy way to write
$tickets{$user}{$env} , which could also be written
$tickets{$user}->{$env} :

 my $users_env = $users_tix->{$env};

The small arrow is optional in $tickets{$user}->{$env} because the
other punctuation is sufficient. If there are curly braces or square
brackets on both sides of the arrow, or one of each, the small arrow
may be omitted. But it reappears in the line of code above; it's
mandatory here because there's no longer a "closing" piece of
punctuation to its left.

And so on. By the time you get to the body of the fourth nested loop,
each variable access has become significantly shorter, so each line of
code is that much more readable and speedy to execute.

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Problem with runaway format

2007-07-04 Thread Mathew Snyder
Tom Phoenix wrote:
> On 7/2/07, Mathew <[EMAIL PROTECTED]> wrote:
> 
>> foreach my $date (@searchDate) {
>> while (my $ticket = $tix->Next) {
> 
> Seeing this worries me. I don't know enough about what's going on to
> tell whether it's wrong or not, but it looks wrong. When the outer
> loop goes on to the second iteration, what does $tix->Next yield? (You
> could use the debugger to find out.)
> 
>> # Format the time spent on each ticket as hh:mm
>> foreach my $user (keys %tickets) {
>> foreach my $env (keys %{ $tickets{$user} }) {
>> foreach my $tikID (keys %{ $tickets{$user}{$env} }) {
>> foreach my $subject (keys %{
>> $tickets{$user}{$env}{$tikID} }) {
> 
> You're doing the same dereferencing, just getting deeper each time.
> You'll save some time if you make some of those into temporary
> variables. That fourth nested loop could look more like this, but
> possibly with better variable names:
> 
>my $subjects_hash = $envs_hash->{$env};
>foreach my $subject (keys %$subjects_hash) {
> 
>> my @endTime;
>> #   my $temp  =
>> $tickets{$user}{$env}{$tikID};
>> my $temp  =
>> $tickets{$user}{$env}{$tikID}{$subject};
>> my $temp2 = $temp / 60;
>> my @temp  = split /\./, $temp2;
>> $endTime[0]   = $temp[0];
>> $endTime[1]   = $temp % 60;
>> #   $tickets{$user}{$env}{$tikID} =
>> sprintf '%d:%02d', @endTime[0,1];
>>
>> $tickets{$user}{$env}{$tikID}{$subject} = sprintf '%d:%02d',
>> @endTime[0,1];
> 
> That looks like a difficult way to do it. Maybe something like this?
> 
>  $subjects_hash->{$subject} =
> time_format($subjects_hash->{$subject});
> 
> sub time_format {
>my $arg = shift;
>return sprintf '%d:%02d', int($arg / 60), $arg % 60;
> }
> 
> That code assumes, as did the original, that the number to be
> formatted is a non-negative integer of seconds or minutes.
> 
> Good luck with it!
> 
> --Tom Phoenix
> Stonehenge Perl Training
> 

Tom,

Thanks.  Your suggestion to place the hashes in new references not only solved
the problem with the runaway format but also another problem that was causing
the subjects of the tickets to be printed only sporadically.

Mathew
-- 
Keep up with me and what I'm up to: http://theillien.blogspot.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: query about code

2007-07-04 Thread Jenda Krynicky
From: "Amichai Teumim" <[EMAIL PROTECTED]>

> Can someone explain to me what this script really does? I mean I see that it
> lists dir within dir. But what is the code doing? For example all the blue
> highlighted stuff, what is it doing?

There is no highlighting in a plain text email!
 
> #!/usr/bin/perl

missing

use strict;
use warnings;

> $startdir = "/lib";
> $level = 0;

Should be

my $startdir = "/lib";
my $level = 0;

> list_dirs($startdir,$level);
> 
> sub list_dirs(){
>   my $dir  = shift (@_);
>   my $lev = shift (@_);

Would be better written  

   my ( $dir, $lev) = @_;
 
>   opendir(TOP,$dir);
>   my @files = readdir(TOP);
>   closedir(TOP);
> 
>   shift(@files);
>   shift(@files);

Here it removes the '.' and '..' "directories" from the list.

>   foreach $file (@files){

foreach my $file (@files){

Without the "my", you are reusing the global variable $file, that all 
the instances of the list_dirs() subroutine that you call recursively 
to traverse the filesystem share the same variable. In this case it 
doesn't cause a visible error since the recursive call is the last 
thing you do in the loop, but still you should be careful with this! 
Unless you really really need to use a global you should declare all 
your variables with "my". Especially within recursive subroutines.

> if(-d "$dir/$file"){

"if $dir/$file is a directory"

> spaces($lev);

prints the $lev spaces. Could be written as

print ' ' x $lev;

> print "$file\n";
> list_dirs("$dir/$file",$lev+1);
> }
>   }
> 
> }

The script traverses the filesystem, starting in /lib and prints all 
directories and subdirectories and subsubdirectories etc.

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: query about code

2007-07-04 Thread Martin Barth
Hi,

it was hard to see the blue stuff.. :)


On Wed, 4 Jul 2007 16:14:47 +0300
"Amichai Teumim" <[EMAIL PROTECTED]> wrote:

> #!/usr/bin/perl
> 
> $startdir = "/lib";
> 
> $level = 0;
> 
> list_dirs($startdir,$level);

calls &list_dir with $startdir and $level.


> 
> sub list_dirs(){
>   my $dir  = shift (@_);
>   my $lev = shift (@_);

shift $dir and $level out of the stack.

> 
> 
>   opendir(TOP,$dir);
>   my @files = readdir(TOP);
>   closedir(TOP);

opens the directory and reads the content into @files;

> 
>   shift(@files);
>   shift(@files);

removes the first 2 elements: "." and ".."

> 
>   foreach $file (@files){

for each file in the directory

> if(-d "$dir/$file"){

is the file a dir?

> spaces($lev);
prints $lev spaces: print " " x $lev; #would do tha same
and print the filename ( directory name ).
> print "$file\n"; 


does the same stuff for the new direktory
since $level is increased by one whole output will be indented 
> list_dirs("$dir/$file",$lev+1);
> }
>   }
> 
> }
> 

same as print " " x $lev;
> sub spaces(){
>   my($num) = shift(@_);
>   for($i=0;$i<$num;$i++){
> print " ";
>   }
> }


HTH, Martin

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Problem with runaway format

2007-07-04 Thread Mathew Snyder
Tom Phoenix wrote:
> On 7/2/07, Mathew <[EMAIL PROTECTED]> wrote:
> 
>> foreach my $date (@searchDate) {
>> while (my $ticket = $tix->Next) {
> 
> Seeing this worries me. I don't know enough about what's going on to
> tell whether it's wrong or not, but it looks wrong. When the outer
> loop goes on to the second iteration, what does $tix->Next yield? (You
> could use the debugger to find out.)
> 
>> # Format the time spent on each ticket as hh:mm
>> foreach my $user (keys %tickets) {
>> foreach my $env (keys %{ $tickets{$user} }) {
>> foreach my $tikID (keys %{ $tickets{$user}{$env} }) {
>> foreach my $subject (keys %{
>> $tickets{$user}{$env}{$tikID} }) {
> 
> You're doing the same dereferencing, just getting deeper each time.
> You'll save some time if you make some of those into temporary
> variables. That fourth nested loop could look more like this, but
> possibly with better variable names:
> 
>my $subjects_hash = $envs_hash->{$env};
>foreach my $subject (keys %$subjects_hash) {
> 

Can you explain the reason for skipping the {$id} portion and assigning the
{$env} portion to %subjects_hash?  The logic eludes me.

>> my @endTime;
>> #   my $temp  =
>> $tickets{$user}{$env}{$tikID};
>> my $temp  =
>> $tickets{$user}{$env}{$tikID}{$subject};
>> my $temp2 = $temp / 60;
>> my @temp  = split /\./, $temp2;
>> $endTime[0]   = $temp[0];
>> $endTime[1]   = $temp % 60;
>> #   $tickets{$user}{$env}{$tikID} =
>> sprintf '%d:%02d', @endTime[0,1];
>>
>> $tickets{$user}{$env}{$tikID}{$subject} = sprintf '%d:%02d',
>> @endTime[0,1];
> 
> That looks like a difficult way to do it. Maybe something like this?
> 
>  $subjects_hash->{$subject} =
> time_format($subjects_hash->{$subject});
> 
> sub time_format {
>my $arg = shift;
>return sprintf '%d:%02d', int($arg / 60), $arg % 60;
> }
> 
> That code assumes, as did the original, that the number to be
> formatted is a non-negative integer of seconds or minutes.
> 
> Good luck with it!
> 
> --Tom Phoenix
> Stonehenge Perl Training
> 


Thanks
Mathew
-- 
Keep up with me and what I'm up to: http://theillien.blogspot.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: hash comparison ideas

2007-07-04 Thread Jenda Krynicky
From: "Jeff Pang" <[EMAIL PROTECTED]>
> A hash is an array actually in perl.
> So you may use Array::Diff module on CPAN:
> http://search.cpan.org/~typester/Array-Diff-0.04/lib/Array/Diff.pm

Beg tour pardon? Are we talking about the silly name "associative 
array" sometimes used form hashes? Or are you trying to say that 
hashes are stored behind the scenes the same way arrays are?

It's true that you can coerce a hash to a list and you can assign 
that list to an array, 

  @array = %hash;

but that does NOT mean that a hash is actually an array. It can be 
kinda converted to one, but since the order is unspecified, I don't 
think it'd make any good when the task at hand is comparing two 
hashes.


Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: hash comparison ideas

2007-07-04 Thread Jenda Krynicky
From: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
> Hi
> I have  a script which contains 2 hashes of file names as the keys and
> md5 sums as the values. I am looking for ideas on fast and efficient
> ways to compare the 2 hashes in the manner of the pseudo code below
> 
> --
> %base_hash
> %new_hash
> 
> for keys in %new_hash
>   if key in %new_hash exists in %base_hash
>   compare  values and do something
>   else
>   do something else
> --

Let's convert this to Perl:

for my $key (keys %new_hash) {
 if (exists $base_hash{$key}) {
  if ($new_hash{$key} > $base_hash{$key} {
   do_something($key);
  }
 } else {
  do_something_else($key);
 }
}

Looks quite close to you pseudocode to me.

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




query about code

2007-07-04 Thread Amichai Teumim

Can someone explain to me what this script really does? I mean I see that it
lists dir within dir. But what is the code doing? For example all the blue
highlighted stuff, what is it doing?

#!/usr/bin/perl

$startdir = "/lib";

$level = 0;

list_dirs($startdir,$level);

sub list_dirs(){
 my $dir  = shift (@_);
 my $lev = shift (@_);


 opendir(TOP,$dir);
 my @files = readdir(TOP);
 closedir(TOP);

 shift(@files);
 shift(@files);

 foreach $file (@files){
   if(-d "$dir/$file"){
   spaces($lev);
   print "$file\n";
   list_dirs("$dir/$file",$lev+1);
   }
 }

}

sub spaces(){
 my($num) = shift(@_);
 for($i=0;$i<$num;$i++){
   print " ";
 }
}


Re: foreach broken in my script

2007-07-04 Thread Rob Dixon

Joseph L. Casale wrote:


I  have a list:
@list = ('Exchange','Filter','DNS','Domain');
This is a list of arrays I also have of course to leverage this I am trying to 
. the @ symbol on it during use.

foreach $vm (@list) {
my_sub("@" . "$vm");
print "@" . "$vm\n";
}

The print likes this, but the my_sub doesn't? Why not?


What do you mean by 'likes'? As far as I can see both will work, but neither 
will
do what you expect of them.

The hash solution has been mentioned, but it may be more appropriate to use
array references without the containing hash:

foreach my $vm ([EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL 
PROTECTED]) {
 my_sub(@$vm);
 print "@$vm\n";
}

(I am calling the reference $vm because you did and it presumably means
something to you, unlike the rest of the world :)

HTH,

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: foreach broken in my script

2007-07-04 Thread Paul Lalli
On Jul 4, 2:29 am, [EMAIL PROTECTED] (Joseph L. Casale) wrote:
> I  have a list:
> @list = ('Exchange','Filter','DNS','Domain');
> This is a list of arrays I also have of course to leverage this I am trying 
> to . the @ symbol on it during use.
>
> foreach $vm (@list) {
> my_sub("@" . "$vm");
> print "@" . "$vm\n";
>
> }
>
> The print likes this, but the my_sub doesn't? Why not?

The print is printing out a string of characters that happens to start
with an @ symbol.  The subroutine is receiving a string of characters
that happen to start with an @ symbol as its argument.  Neither of
them are talking about the arrays @Exchange, @DNS, etc.

Please see: perldoc -q "variable name"

Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: formatting a string

2007-07-04 Thread Mumia W.

On 07/03/2007 08:32 PM, Joseph L. Casale wrote:

I have an array with the following data in it:

/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-DC (Win2003 Ent x64)/AN-DC 
(Win2003 Ent x64).vmx
/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-DC (Win2003 Ent x64)/Disc 
1.vmdk
/vmfs/volumes/467f06a5-7d59c067-35cb-0007e9153886/AN-DC (Win2003 Ent x64)/Disc 
2.vmdk

I always deal with indices' 1 through to the end in the function in question, so it's easy to get 
the second indices (First disc) and so on. I need to manipulate the path though now, I am wanting 
to search for *all* the text following the third "/" and before the fourth "/" 
and replace it with a string variable. So far, this is seeming to be way over my current capacity :)

Can anyone point me to the topic/method I should use so I may read up on build 
this myself?

Thanks!
jlc





File::Spec->splitdir will let you split the string into directories 
which you can manipulate individually.


Or you can just use the split function to split on "/".

Good luck.




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




RE: foreach broken in my script

2007-07-04 Thread Thomas Bätzler
Joseph L. Casale <[EMAIL PROTECTED]> wrote:

> I  have a list:
> @list = ('Exchange','Filter','DNS','Domain');
> This is a list of arrays I also have of course to leverage 
> this I am trying to . the @ symbol on it during use.
> 
> foreach $vm (@list) {
> my_sub("@" . "$vm");
> print "@" . "$vm\n";
> }
> 
> The print likes this, but the my_sub doesn't? Why not?

If you're trying to use symbolic references, well, please
don't. They are evil.

Instead, structure your data into a hash of lists, like

#!/usr/bin/perl

use strict;
use warnings;

# initialize a hash with "array names" as keys and
# anonymous hash references as their value.

my %host = (
'Exchange' => [ 'foo', 'baz', 'bar' ],
'Filter' => [ 'foo2', 'baz2', 'bar2' ],
'DNS' => [ 'foo3', 'baz3', 'bar3' ],
'Domain' => [ 'blerg' ],
);

# the argument passed to this sub is not an array, but
# an array reference.

sub my_sub {
  my $array_ref = shift;

  # array in scalar context == number of elements
  if( @$array_ref > 1 ){
# note -> syntax used to dereference the reference
print "the second element is " . $array_ref->[1] . "\n";
  } else {
print "the passed array has less than 2 elements\n";
  }
}

# keys %host is the most flexible way to specify a list of all
# key values. The order of the values is indeterminate, so you
# might have to sort to get them in the same order every time.
foreach my $vm ( keys %host ){
  print "values for $vm: " . join(', ', @{$host{$vm}} ) . "\n";
  my_sub( $host{$vm} );
}

__END__

HTH,
Thomas

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/