Re: question on lexical declaration and submodule

2008-03-27 Thread Robert (bloo)

[EMAIL PROTECTED] wrote:


> Hi,
> 
> I am doing some studies on sub modules and Lexical variables (my).
> 
> With regards to diagram 1 below, what can I do so that the lexical $counter can count up to 4.
> 
> Of course, one way of doing this is to change the lexical $counter into a global variable as seen in diagram 2 but I dont think this is a good idea as the sub-module will then have a mixture of both lexical (my) and global variables.
> 
> So, are there any other ways of writing in diagram 1 so that the $counter can count up to 4?

> Thanks
> 
> 
>  
> ## Diagram 1    lexical $counter t###

> use strict;
> my $anything = 0;
> 
> while ($anything < 5){

>   $anything +=1;
>   &testing_module ;
> }
> 
> 
> sub testing_module {

>my $counter = ();
>if ($counter < 5){
>  $counter += 1;
>   }
> }
> 
> ###

> # Diagram 2    global $counter below 
##
> use strict;
> my $anything = 0;
> my $counter = (); #global counter
> 
> while ($anything < 5){

>   $anything +=1;
>   &testing_module ;
> }
> 
> 
> sub testing_module {

>$counter = ();
>if ($counter < 5){
>  $counter += 1;
>   }
> }
  

I'd reckon that a while/for loop would do just as well.

sub testing_module {
#You could assign counter to 0 here, but in a for loop not needed.
for(my $counter;$counter < 5; $counter++) {
# Do things with $counter..
}
# while($counter < 5) { $counter++ } # this also would work.
}

--START NOTE--
I didn't include the first loop because I didn't see why it'd make a difference 
:P
The counter was all you were asking about, so that's all I worried about.
--END NOTE--

TMTOWTDI,
bloo`


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




Re: Handling OLD files

2008-03-28 Thread Robert (bloo)

Sandy wrote:

On Mar 27, 1:40 pm, [EMAIL PROTECTED] (Anders) wrote:
  

I am looking for a way of moving files older than a date,
I am need this in a script for moving/erase old files from a
production machine



Try using File::Find
http://search.cpan.org/~nwclark/perl-5.8.7/lib/File/Find.pm

/sandy
http://myperlquiz.com/


  
Or, you could use the built in "stat" function. It can get a lot of 
information on a file.


perldoc -f stat

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




Re: hash of array does not return what I expect

2008-04-05 Thread Robert (bloo)


> The output of the following script:
> 
> #!/usr/bin/perl -w

> @array = [0, 1, 2, 3];
> $hash{0} = @array;
> print "array = @array\n";
> print "hash = ", $hash{0}, "\n";
> 
> is:
> 
> array = ARRAY(0x8d13c20)

> hash = 1
> 
> I expected the results to be the same.  Why aren't they?
  
First thing is, you're making an array to a reference to an anonymous 
array. That's almost pointless :P The assignment to the hash :


$hash{0} = @array;

is only going to give you the number of elements inside of the array 
(due to hashes always having scalar key/value pairs).



Here is a program for you to go over, hope it helps. :)


#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my $array = [1,2,3,4];
my %hash = @{$array};
print "Array Reference in Scalar: $array\n\tHash structure:\n";
for (sort keys %hash) {
   print "\t$_\t=>\t$hash{$_}\n";
}

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