Re: while loop -> map

2005-05-24 Thread Offer Kaye
On 5/24/05, Robert Citek wrote:
> 
> I found a variation of this in the Perl Nutshell book:
> 
> $ perl -le '
>   $foo="fee fie foe foo" ;
>   while ($foo =~ m/e/g ) {
> push @bar, pos $foo ;
>   }
>   print join(":", @bar); '
> 2:3:7:11
> 
> Is there an equivalent way to do the same using map instead of an
> explicit while loop?  I'm guessing not, since map is expecting a list
> and not a scalar, which $foo is.
> 

This is Perl, these is always another way :)

my $foo = "fee fie foe foo"; 
my $sum;
my @bar = map {$sum+=1+length} split(/e/,$foo);
print join(":", @bar[0..$#bar-1]) . "\n";

It is however a bit forced. I would stick to the "while" - much more readable :)
Cheers,
-- 
Offer Kaye

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




Re: while loop -> map

2005-05-24 Thread Ing. Branislav Gerzo
Xavier Noria [XN], on Tuesday, May 24, 2005 at 22:12 (+0200) typed the
following:

XN>  my $i = 0;
XN>  my @bar = map $_->[1],   # take second component
XN>grep $_->[0] eq 'e',   # let 'e's pass
XN>map [$_, ++$i],# arrayref [char, index of char]
XN>split //, $foo;# split $foo in chars
XN> The while is better IMO.

while is more readable in this example, if I'll have this piece of
code in my script I spend 5 minutes to see what it is doing, and other
could spend the whole life ! :)
I think we should code more readable script(s), let Perl isn't ROL.

-- 

How do you protect mail on web? I use http://www.2pu.net

["What's the speed limit on Highway 69?"   "Lickety-split."]



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




Re: hash reference help

2005-05-24 Thread Ing. Branislav Gerzo
Offer Kaye [OK], on Wednesday, May 25, 2005 at 09:09 (+0300) typed the
following:

>> it was easy mistake, everything we should know about this is avoid
>> using keys() on %$hr in while loop, because it resets iterator. That's
>> all. 

OK> Well, that, and don't change the hash inside the while loop. In your
OK> original email you wrote:
OK> "and get_something() change the hash"

I was wrong here; get_something() actually doesn't change the hash (if
I'll do that I'll find my mistake sooner, it is also logical).

OK> That's a no-no according to "perldoc -f each": "If you add or delete
OK> elements of a hash while you're iterating over it, you may get entries
OK> skipped or duplicated, so don't."

Trap was using keys. But thanks for remembering this to me and other
members :)

-- 

How do you protect mail on web? I use http://www.2pu.net

[Promote that demon to the House of Lords, commanded Tom imperiously.]



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




Re: Can't use subscript in angle brackets

2005-05-24 Thread John W. Krahn

Tielman Koekemoer (TNE) wrote:

Hi all,


Hello,


Why can't I use a subscript in the angle brackets? e.g.

@files=`ls /app2/koekemtn/scripts/dbstats/test`;

chomp $files[0];

open ( $files[0] , "<$files[0]") || die "Cannot open $files[0]\n";

while (<$files[0]>) {
print "$_";
}


perldoc perlop
[snip]

   I/O Operators

[snip]

   If what the angle brackets contain is a simple scalar variable (e.g.,
   <$foo>), then that variable contains the name of the filehandle to
   input from, or its typeglob, or a reference to the same.  For example:

   $fh = \*STDIN;
   $line = <$fh>;

   If what's within the angle brackets is neither a filehandle nor a
   simple scalar variable containing a filehandle name, typeglob, or
   typeglob reference, it is interpreted as a filename pattern to be
   globbed, and either a list of filenames or the next filename in the
   list is returned, depending on context.  This distinction is determined
   on syntactic grounds alone.  That means "<$x>" is always a readline()
   from an indirect handle, but "<$hash{key}>" is always a glob().  That's
   because $x is a simple scalar variable, but $hash{key} is not--it's a
   hash element.



John
--
use Perl;
program
fulfillment

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




Can't use subscript in angle brackets

2005-05-24 Thread Tielman Koekemoer \(TNE\)

Hi all,

Why can't I use a subscript in the angle brackets? e.g.

@files=`ls /app2/koekemtn/scripts/dbstats/test`;

chomp $files[0];

open ( $files[0] , "<$files[0]") || die "Cannot open $files[0]\n";

while (<$files[0]>) {
print "$_";
}

TIA



Re: hash reference help

2005-05-24 Thread Offer Kaye
On 5/24/05, Ing. Branislav Gerzo wrote:
> 
> it was easy mistake, everything we should know about this is avoid
> using keys() on %$hr in while loop, because it resets iterator. That's
> all. 

Well, that, and don't change the hash inside the while loop. In your
original email you wrote:
"and get_something() change the hash"
That's a no-no according to "perldoc -f each": "If you add or delete
elements of a hash while you're iterating over it, you may get entries
skipped or duplicated, so don't."

Cheers,
-- 
Offer Kaye

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




Re: Count special character

2005-05-24 Thread Jeff 'japhy' Pinyan

On May 24, Joel Divekar said:


#!/usr/bin/perl -w

my $a = "a:b:c:d:e:f:\:↔:↔:";

my $count = ($a =~ tr/\chr(29)//);


That doesn't work.  First of all, '↔' in Perl is just 7 characters 
in a row.  HTML entity codes are for HTML, not Perl.  If you want 
character #8596 in Perl, you have to use chr(8596) or a hex sequence like 
\x{2194}.  Next, tr/// doesn't know what "\chr(29)" means.  tr/// just 
takes normal characters, octal or hex escape sequences, and ranges (two 
characters with a - between them).


  my $count = ($a =~ tr/\x{2194}//);

works.

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




Re: assigning printf statement to an array

2005-05-24 Thread Jeff 'japhy' Pinyan

On May 24, [EMAIL PROTECTED] said:


1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files



  how does the perl compiler load this, from line 9 up or opendir down?


Well, Perl reads it left-to-right.  It just happens that it's a bunch of 
functions whose arguments are the return values of other functions.  It's 
the same as


  print get_output(
get_old_ones(
  make_age_filename(
exclude_dot_and_dotdot(
  sort(
readdir(DIR)
  )
)
  )
)
  );

where those functions do what you have above.


  And what is the correct way to read it from top down or bottom up?


Humans should probably read it from the bottom up.  The raw data are the 
arguments to the right-most function, and then it goes through many 
filters until it is in its final form and printed.



  what is the purpose of an array reference in this code and what is it
  advantages?


The array reference is just a way of holding two pieces of data at once.


  on pg 253 of the camel book it states "if the next thing after the -> is
  a bracket, then the left operand ($_ ) is treated as a reference to an
  array to be subscripted on the right [0] or [1] .  So is this array
  declared/created dynamically?  How?


The array reference is created in lines 4-7.


  on line 2 why is the period after [1] needed and what does it represent
  , string concatenation?  If so why are multiple ones needed on line 2?


That's the '.' operator, used for string concatenation.

  $_->[1] . ": " . $_->[0] . "\n"

could just be written as

  "$_->[1]: $_->[0]\n"


  so [1] contains the dir name and [0] contains the my days old
  calculation, correct?


Yes.


  how does map know to create this array b/c I thought map expects a list
  that is already created?


map() takes a list and returns a list.  The elements of the list it 
receives are filenames, the elements of the list it returns are array 
REFERENCES.



  from the previous question... is this list the opendir?  I guess am not
  totally grasping the map function.


Then you should read 'perldoc -f map'.  map() is like a for loop that 
returns a list.


  @out = map { ...code... } @in;

is like:

  @out = ();
  for (@in) {
push @out, ...code...;
  }

Here's a simple example:

  @even_numbers = map { $_ * 2 } (1 .. 10);

That creates @even_numbers = (2, 4, 6, 8, 10, 12, 14, 16, 18, 20).  The 
same result can be gotten with a for loop:


  @even_numbers = ();
  for (1 .. 10) {
push @even_numbers, $_ * 2;
  }


  in lines 5 and 6 $_ is the dir, but why is it needed at  the very end at
  line 6 after the comma?


Because lines 4-7 are creating an array reference.  The first element is 
the age of the file, and the second element is the file, $_.


  map { [ get_age($_), $_ ] } ...

The moral of the story is, you can do things all at once using map() and 
grep(), or you can do them piecemeal with for loops:



1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files


Could be:

  # line 9
  my @all_files = sort readdir DH;

  # line 8
  my @files;
  for (@all_files) { push @files, $_ if $_ ne "." and $_ ne ".." }

  # lines 4-7
  my @files_and_ages;
  for (@files) {
push @files_and_ages, [
  sprintf(...), $_
];
  }

  # line 3
  my @old_files;
  for (@files_and_ages) {
push @old_files, $_ if $_->[0] > 100;
  }

  # line 2
  my @strings;
  for (@old_files) {
push @strings, "$_->[1]: $_->[0]\n";
  }

  # line 1
  print @strings;

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




Re: why getstore can't work, especially for store xml file on freebsd

2005-05-24 Thread Franklin
 Sometimes it ok. But when you run it many times, say 1,000 times,
 there is serveral times that it return 200, but the stored file is 0
 bytes, titally empty.
 
> On 5/25/05, John Doe <[EMAIL PROTECTED]> wrote:
> > Am Dienstag, 24. Mai 2005 08.59 schrieb Franklin:
> > > Hi;
> > >
> > > I have a small program that uses getstore to fetch a xml webpahe and
> > > store it. It is:
> > >
> > > use strict;
> > > use warnings;
> > >
> > > use LWP::Simple;
> > >
> > > my
> > > $xmlurl="http://www.sec.gov/Archives/edgar/data/903404/000118143105029692/r
> > >rd79736.xml"; my $xmlfile="xml.xml";
> > > my $status=getstore($xmlurl,$xmlfile);
> > >
> > > if(is_success($status))
> > > {
> > > print "xmlfile store is ok\n";
> > > }
> > > else
> > > {
> > > print "xmlfile store isn't ok\n";
> > > }
> > >
> > >
> > > It works well on windows platform, but doesn't on freebsd. That is,
> > > every time the getstore function's response is "200 ok", but the
> > > stored xml file isn't correct, it means, it isn't as same as the xml
> > > webpage.
> >
> > What are the differences then? Is it another file? Or the right one 
> > truncated?
> > Wrong chars? Or...
> >
> > > What is wrong with it? And is there any solution or
> > > alternatives to this problems?
> >
> > Maybe?
> >
> > joe
> >
> > >
> > > Thank you very much!
> > >
> > > Best wishes
> > > Franklin
> > >
> > > T
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >  
> >
> >
> >
>

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




Re: assigning printf statement to an array

2005-05-24 Thread DBSMITH
operdir .

1> print
2> map {$_->[1].": ".$_->[0]."\n"} # create entity output
3> grep {$_->[0] > 100} # exclude age <= 100
4> map { [
5>  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
6>  $_
7> ] } # create aref [age, filename]
8> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
9> sort readdir DH; # get all files

closedir ...


In the block of code  :

   how does the perl compiler load this, from line 9 up or opendir down?
   And what is the correct way to read it from top down or bottom up?
   what is the purpose of an array reference in this code and what is it
   advantages?
   on pg 253 of the camel book it states "if the next thing after the -> is
   a bracket, then the left operand ($_ ) is treated as a reference to an
   array to be subscripted on the right [0] or [1] .  So is this array
   declared/created dynamically?  How?
   on line 2 why is the period after [1] needed and what does it represent
   , string concatenation?  If so why are multiple ones needed on line 2?
   so [1] contains the dir name and [0] contains the my days old
   calculation, correct?
   how does map know to create this array b/c I thought map expects a list
   that is already created?
   from the previous question... is this list the opendir?  I guess am not
   totally grasping the map function.
   in lines 5 and 6 $_ is the dir, but why is it needed at  the very end at
   line 6 after the comma?
   to the end, output of days old > 100 I am now trying to use my
   date_manip1 subroutine to pass it the days old to get "the past date
   dd/mm/yy" as such:
  line 3 turns into &date_manip1(grep {$_->[0] > 100} ) so I have
output as :

ABDELFATTAH__FAITH_M702515438:  101   dd/mm/yy
I am on the right track?

thank you,

derek


   
 John Doe  
   To 
   beginners@perl.org  
 05/24/2005 03:27   cc 
 PM
   Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Hi

Am Dienstag, 24. Mai 2005 18.05 schrieb [EMAIL PROTECTED]
with funny quoting:

[John:]
> my @a=
> map {$hdir.$_->[1]} # select filename
> grep {$_->[0] > 100} # exclude age <= 100
> map { [
>   sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
>   $_
>  ] } # create aref [age, filename]
> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
> sort readdir DH; # get all files
>
>
> The map-grep-map-construct is quite common for problems like yours
> ("But I cannot use $_ b/c $_ is the dir name.").


[Derek:]
> This is working but it not printing the calculation "days old" for each
> filename???

No, obviously not... I wanted to demonstrate the principle where
some kind of additional attributes (here: age) of a list of entities
(files)
is created on the fly and kept together (in an aref), and taken as base for

further treatment (selection). [ Hope this english is understandable :-) ]

> I want to print the aref.
> I verified that it is indeed only printing those files over 100 days old,
> but I need output such as,
>
> ABDELFATTAH__FAITH_M702515438:  101
>
> as opposed to
>
> ABDELFATTAH__FAITH_M702515438

Ok, then some minimal changes in the first two lines are necessary
(although
there are other possibilities to do what you want; my proposal is possibliy

not the most performant, but splits the problem into smaller parts that can

be chaned)
[also untested...]

print
map {$_->[1].": ".$_->[0]."\n"} # create entity output
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files

> I will also follow up with a line by line question of the code.

Just go on, questions are half of the answers :-)

joe


[...]
[maybe I should have snipped the rest?]
> Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> > Hi all.
> >
> > This is a continuation from my original question, but with a twist.  I
am
> > now using sprintf to assign to an 

Re: why getstore can't work, especially for store xml file on freebsd

2005-05-24 Thread John Doe
Am Dienstag, 24. Mai 2005 08.59 schrieb Franklin:
> Hi;
>
> I have a small program that uses getstore to fetch a xml webpahe and
> store it. It is:
>
> use strict;
> use warnings;
>
> use LWP::Simple;
>
> my
> $xmlurl="http://www.sec.gov/Archives/edgar/data/903404/000118143105029692/r
>rd79736.xml"; my $xmlfile="xml.xml";
> my $status=getstore($xmlurl,$xmlfile);
>
> if(is_success($status))
> {
> print "xmlfile store is ok\n";
> }
> else
> {
> print "xmlfile store isn't ok\n";
> }
>
>
> It works well on windows platform, but doesn't on freebsd. That is,
> every time the getstore function's response is "200 ok", but the
> stored xml file isn't correct, it means, it isn't as same as the xml
> webpage. 

What are the differences then? Is it another file? Or the right one truncated? 
Wrong chars? Or...

> What is wrong with it? And is there any solution or 
> alternatives to this problems?

Maybe?

joe

>
> Thank you very much!
>
> Best wishes
> Franklin
>
> T

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




Re: while loop -> map

2005-05-24 Thread John Doe
Am Dienstag, 24. Mai 2005 19.22 schrieb Robert Citek:
> I found a variation of this in the Perl Nutshell book:
>
> $ perl -le '
>   $foo="fee fie foe foo" ;
>   while ($foo =~ m/e/g ) {
> push @bar, pos $foo ;
>   }
>   print join(":", @bar); '
> 2:3:7:11
>
> Is there an equivalent way to do the same using map instead of an
> explicit while loop?  I'm guessing not, 

I guess the same (did nevertheless some tests without luck)

> since map is expecting a list and not a scalar, which $foo is.

but have a slightly different argument:

map expects a list that is already created, while pos() has to be applied 
after every step of creating the list.

But how knows? (Awaiting for the inconceivable of one of the list gurus)


joe

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




Re: assigning printf statement to an array

2005-05-24 Thread John Doe
Hi

Am Dienstag, 24. Mai 2005 18.05 schrieb [EMAIL PROTECTED] 
with funny quoting:

[John:]
> my @a=
> map {$hdir.$_->[1]} # select filename
> grep {$_->[0] > 100} # exclude age <= 100
> map { [
>   sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
>   $_
>  ] } # create aref [age, filename]
> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
> sort readdir DH; # get all files
>
>
> The map-grep-map-construct is quite common for problems like yours
> ("But I cannot use $_ b/c $_ is the dir name.").


[Derek:]
> This is working but it not printing the calculation "days old" for each
> filename???  

No, obviously not... I wanted to demonstrate the principle where 
some kind of additional attributes (here: age) of a list of entities (files) 
is created on the fly and kept together (in an aref), and taken as base for 
further treatment (selection). [ Hope this english is understandable :-) ]

> I want to print the aref. 
> I verified that it is indeed only printing those files over 100 days old,
> but I need output such as,
>
> ABDELFATTAH__FAITH_M702515438:  101
>
> as opposed to
>
> ABDELFATTAH__FAITH_M702515438

Ok, then some minimal changes in the first two lines are necessary (although 
there are other possibilities to do what you want; my proposal is possibliy 
not the most performant, but splits the problem into smaller parts that can 
be chaned)
[also untested...]

print
map {$_->[1].": ".$_->[0]."\n"} # create entity output
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files

> I will also follow up with a line by line question of the code.

Just go on, questions are half of the answers :-)

joe


[...]
[maybe I should have snipped the rest?]
> Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> > Hi all.
> >
> > This is a continuation from my original question, but with a twist.  I am
> > now using sprintf to assign to an array, but I only want to assign to
>
> this
>
> > array of I find a number over 100 from my calculation.  In the array/hash
> > also needs to be the directory name.  Here is my code and the sample
> > output:
> > So in the end my goal is to store the data in this array if the number is
> > over 100 and if it is over 100 store the directory name and its
>
> associative
>
> > "days old calc."
> > I have tried my @a =(); my $e =0;
> > $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> > ONE_DAY ) if ??? > 100;
> >
> > But I cannot use $_ b/c $_ is the dir name.
> >
> > thank you,
> >
> > derek
> >
> >
> >
> >
> > use strict;
> > use warnings;
> > $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
> >
> > my $hdir   = qq(/heartlab/db studies/);
> > #my $fdirs =
> > qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> > i/clinical/RMH/);
> > $^T=time;
> > our $now = time();
> > use constant ONE_DAY => 86400;
> >
> >
> > sub date_manip
> > {
> > my ($month, $day, $year) = (localtime)[4,3,5];
> > sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
>
> 100));
>
> > }
> >
> > my $dm = &date_manip;
> >
> > sub date_manip1
> > {
> > my $days = (shift);
> > my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
> > [3..5];
> > sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
> > }
> >
> > #my $dmm = &date_manip1(1);
> > #chomp $dm;
> > #chomp $dmm;
> >
> >
> >
> > opendir (DH, $hdir) or die "unable to open directory: $hdir $!";
> >
> > my @a =();
> > my $e =0;
> > foreach my $file (sort readdir (DH))
> > {
> > next if $file eq "." or $file eq "..";
> > print "\n";
> > print "Days old of HL image files are:\t",$file,":\t";
> > #printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9]
>
> )
>
> > / ONE_DAY );
> > $a[$e++] = sprintf ("%.0f",( $now - ( stat(
> > "${hdir}${file}" ) )[9] ) / ONE_DAY );
> > #date_manip1($1);
> > }
>
> instead of the foreach loop you could use a single statement [untested]:
> (read from bottom to top)
>
> my @a=
> map {$hdir.$_->[1]} # select filename
> grep {$_->[0] > 100} # exclude age <= 100
> map { [
>   sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
>   $_
>  ] } # create aref [age, filename]
> grep {$_ ne "." and $_ ne ".."} # exclude unwanted
> sort readdir DH; # get all files
>
>
> The map-grep-map-construct is quite common for problems like yours
> ("But I cannot use $_ b/c $_ is the dir name.").
>
> (Schwarz'sche Transformation in german).
>
>
> hth, joe
>
> > closedir (DH) or warn "unable to close DH: $hdir $!;"
> >
> > ___END CODE___
> >
> > ___BEGIN_DATA__

Re: while loop -> map

2005-05-24 Thread Xavier Noria

On May 24, 2005, at 19:22, Robert Citek wrote:


I found a variation of this in the Perl Nutshell book:

$ perl -le '
 $foo="fee fie foe foo" ;
 while ($foo =~ m/e/g ) {
   push @bar, pos $foo ;
 }
 print join(":", @bar); '
2:3:7:11

Is there an equivalent way to do the same using map instead of an  
explicit while loop?  I'm guessing not, since map is expecting a  
list and not a scalar, which $foo is.


The difficulty comes from the need to figure out the indices, a  
possible approach would be:


my $i = 0;
my @bar = map $_->[1],   # take second component
  grep $_->[0] eq 'e',   # let 'e's pass
  map [$_, ++$i],# arrayref [char, index of char]
  split //, $foo;# split $foo in chars

The while is better IMO.

-- fxn

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




Using $# in a splice with split

2005-05-24 Thread Larsen, Errin M HMMA/IT
Hi Perl buddies,

  Can I do something like this:

 my $line = 'One Two Three Four Five Six';
 my( $first, $last ) = (split(' ', $line))[0,$#(split(' ', $line))];

This does not work.  What I want to do is to find the index of the last
element of a list created by split, and use it in a slice on the same
line.  Wow, that sounds garbled when I re-read it.  With the above
example, I want the two variables to contain the following:

  $first = 'One';
  $last = 'Six';

I realize I can do this in two steps:

  my $line = 'One Two Three Four Five Six';
  my @line = split(' ', $line);
  my($first, $last) = @line[0,$#line];

But I was wondering if it can be done on one line.

--Errin

PS    oops ... I just remembered negative indexing.  This works:

  my $line = 'One Two Three Four Five Six';
  my( $first, $last ) = (split(' ', $line )[0, -1];

We!

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




Fw: assigning printf statement to an array

2005-05-24 Thread DBSMITH
- Forwarded by Derek Smith/Staff/OhioHealth on 05/24/2005 03:47 PM
-
   
 Derek 
 Smith/Staff/OhioH 
 ealth  To 
   John Doe
 05/24/2005 12:05  <[EMAIL PROTECTED]>  
 PM cc 
   beginners@perl.org  
   Subject 
   Re: assigning printf statement to   
   an array(Document link: Derek   
   Bellner Smith)  
   
   
   
   
   
   



my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files


The map-grep-map-construct is quite common for problems like yours
("But I cannot use $_ b/c $_ is the dir name.").



This is working but it not printing the calculation "days old" for each
filename???  I want to print the aref.
I verified that it is indeed only printing those files over 100 days old,
but I need output such as,

ABDELFATTAH__FAITH_M702515438:  101

as opposed to

ABDELFATTAH__FAITH_M702515438

I will also follow up with a line by line question of the code.
thanks again!
derek




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




   
 John Doe  
   To 
   beginners@perl.org  
 05/24/2005 01:34   cc 
 AM
   Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> Hi all.
>
> This is a continuation from my original question, but with a twist.  I am
> now using sprintf to assign to an array, but I only want to assign to
this
> array of I find a number over 100 from my calculation.  In the array/hash
> also needs to be the directory name.  Here is my code and the sample
> output:
> So in the end my goal is to store the data in this array if the number is
> over 100 and if it is over 100 store the directory name and its
associative
> "days old calc."
> I have tried my @a =(); my $e =0;
> $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> ONE_DAY ) if ??? > 100;
>
> But I cannot use $_ b/c $_ is the dir name.
>
> thank you,
>
> derek
>
>
>
>
> use strict;
> use warnings;
> $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
>
> my $hdir   = qq(/heartlab/db studies/);
> #my $fdirs =
> qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> i/clinical/RMH/);
> $^T=time;
> our $now = time();
> use constant ONE_DAY => 86400;
>
>
> sub date_manip
> {
> my ($month, $day, $year) = (localtime)[4,3,5];
> sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
100));
> }
>
> my $dm = &date_manip;
>
> sub date_manip1
> {
> my $days = (shift);
> my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
> [3..5];
> sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
> }
>
> #my $dmm =

Re: efficiency of hash of hashes/lists

2005-05-24 Thread Dave Gray
> > # access test for 2d
> > ($su, $ss) = times;
> > for my $i (0 .. $hashsize-1) {
> > $oned{$l1[$i]}{$l2[$i]}++
> 
> I think you should be operating on %twod here.

LOL, thanks. Original poster take note:

generating hashes..!
base  0.03  0.00   0.03
  1D  0.24  0.00   0.24
  2D  0.22  0.00   0.22

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




Re: efficiency of hash of hashes/lists

2005-05-24 Thread Paul Johnson
On Tue, May 24, 2005 at 02:13:49PM -0400, Dave Gray wrote:

> On 5/23/05, Peter Rabbitson <[EMAIL PROTECTED]> wrote:
> > On Mon, May 23, 2005 at 01:40:08PM -0400, Zhenhai Duan wrote:
> > > I tried hash (where the members of a group are joined with ":"), and hash
> > > of hash. It happended that hash of hash is slower than single hash.
> > >
> > > Hash:
> > > $groups{$g1} = "$member1:$member2";
> > >
> > > Hash of hash
> > > $groups{$g1}{$member1} = 1;
> > >
> > > Method 1 is faster, even I need to do a split to get the members.
> > 
> > Can you post some code? Without it the above statement is not very credible
> > to say the least.
> 
> The 1D approach seems to be approximately 3 times as fast (on x86
> Linux). Anyone get different results?
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my @ra = ('a' .. 'z', 'A' .. 'Z');
> my ($seqlen, $hashsize) = (4, 7);
> 
> my (%oned, %twod) = ((),());
> my (@l1, @l2) = ((),());
> 
> $|++;
> print "generating hashes";
> for my $i (1 .. $hashsize) {
> my $key1 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]);
> push @l1, $key1;
> my $key2 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]);
> push @l2, $key2;
> $oned{"$key1:$key2"}++;
> $twod{$key1}{$key2}++;
> print '.' if not $i % int($hashsize/10);
> }
> print "!\n";
> 
> # baseline
> my ($su, $ss) = times;
> for (1 .. $hashsize) { }
> my ($eu, $es) = times;
> my ($tu, $ts) = ($eu - $su, $es - $ss);
> my $tt = $tu + $ts;
> printf "%20s %5.2f %5.2f %6.2f\n", 'base', $tu, $ts, $tt;
> 
> # access test for 1d
> ($su, $ss) = times;
> for my $i (0 .. $hashsize-1) {
> $oned{"$l1[$i]:$l2[$i]"}++
> }
> ($eu, $es) = times;
> ($tu, $ts) = ($eu - $su, $es - $ss);
> $tt = $tu + $ts;
> printf "%20s %5.2f %5.2f %6.2f\n", '1D', $tu, $ts, $tt;
> 
> # access test for 2d
> ($su, $ss) = times;
> for my $i (0 .. $hashsize-1) {
> $oned{$l1[$i]}{$l2[$i]}++

I think you should be operating on %twod here.

> }
> ($eu, $es) = times;
> ($tu, $ts) = ($eu - $su, $es - $ss);
> $tt = $tu + $ts;
> printf "%20s %5.2f %5.2f %6.2f\n", '2D', $tu, $ts, $tt;

But your real gains will come when you want to do something which does
not require iterating through all the elements you have created.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

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




Re: efficiency of hash of hashes/lists

2005-05-24 Thread Philip M. Gollucci

Dave Gray wrote:


On 5/23/05, Peter Rabbitson <[EMAIL PROTECTED]> wrote:
 


On Mon, May 23, 2005 at 01:40:08PM -0400, Zhenhai Duan wrote:
   


I tried hash (where the members of a group are joined with ":"), and hash
of hash. It happended that hash of hash is slower than single hash.

Hash:
$groups{$g1} = "$member1:$member2";

Hash of hash
$groups{$g1}{$member1} = 1;

Method 1 is faster, even I need to do a split to get the members.
 


Can you post some code? Without it the above statement is not very credible
to say the least.
   



The 1D approach seems to be approximately 3 times as fast (on x86
Linux). Anyone get different results?
 


FreeBSD 6.x-current w/ perl5.9.3 and ithreads.

[ttyp0] [EMAIL PROTECTED] /usr/home/pgollucci rv=0 26 >perl test.pl
generating hashes..!
   base  0.02  0.00   0.02
 1D  0.14  0.00   0.14



--
END 
-

   What doesn't kill us, can only make us stronger.
  Nothing is impossible.

Philip M. Gollucci ([EMAIL PROTECTED]) 301.254.5198
Consultant / http://p6m7g8.net/Resume/resume.shtml
Senior Developer / Liquidity Services, Inc.  
http://www.liquidityservicesinc.com



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




Return CD-ROM/RW Creation Date

2005-05-24 Thread ahuber
Platform: Windows
Version: Active State v5.8.36 build number 811

Is there a way to find out the creation date (or burn date) of a cd-rw
disc? I tried using things like $burndate = (stat("."))[9], but on windows
there is no current directory entry for CD media, it always returns an old
date like 1980-1-1.

I've also looked into Win32::DriveInfo, but it doesn't have any way to
report the date a CD was created either.

I have only found one command line tool that shows the cd creation date
but it doesn't use standard output so I can't redirect its output to perl
for processing.

I am trying to read the creation date off of CDs burned in the Joliet File
System.  Thank You - Aaron S. Huber ([EMAIL PROTECTED])




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




Re: efficiency of hash of hashes/lists

2005-05-24 Thread Dave Gray
On 5/23/05, Peter Rabbitson <[EMAIL PROTECTED]> wrote:
> On Mon, May 23, 2005 at 01:40:08PM -0400, Zhenhai Duan wrote:
> > I tried hash (where the members of a group are joined with ":"), and hash
> > of hash. It happended that hash of hash is slower than single hash.
> >
> > Hash:
> > $groups{$g1} = "$member1:$member2";
> >
> > Hash of hash
> > $groups{$g1}{$member1} = 1;
> >
> > Method 1 is faster, even I need to do a split to get the members.
> 
> Can you post some code? Without it the above statement is not very credible
> to say the least.

The 1D approach seems to be approximately 3 times as fast (on x86
Linux). Anyone get different results?

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

my @ra = ('a' .. 'z', 'A' .. 'Z');
my ($seqlen, $hashsize) = (4, 7);

my (%oned, %twod) = ((),());
my (@l1, @l2) = ((),());

$|++;
print "generating hashes";
for my $i (1 .. $hashsize) {
my $key1 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]);
push @l1, $key1;
my $key2 = join('', @ra[map(int(rand($#ra))+1, 1 .. $seqlen)]);
push @l2, $key2;
$oned{"$key1:$key2"}++;
$twod{$key1}{$key2}++;
print '.' if not $i % int($hashsize/10);
}
print "!\n";

# baseline
my ($su, $ss) = times;
for (1 .. $hashsize) { }
my ($eu, $es) = times;
my ($tu, $ts) = ($eu - $su, $es - $ss);
my $tt = $tu + $ts;
printf "%20s %5.2f %5.2f %6.2f\n", 'base', $tu, $ts, $tt;

# access test for 1d
($su, $ss) = times;
for my $i (0 .. $hashsize-1) {
$oned{"$l1[$i]:$l2[$i]"}++
}
($eu, $es) = times;
($tu, $ts) = ($eu - $su, $es - $ss);
$tt = $tu + $ts;
printf "%20s %5.2f %5.2f %6.2f\n", '1D', $tu, $ts, $tt;

# access test for 2d
($su, $ss) = times;
for my $i (0 .. $hashsize-1) {
$oned{$l1[$i]}{$l2[$i]}++
}
($eu, $es) = times;
($tu, $ts) = ($eu - $su, $es - $ss);
$tt = $tu + $ts;
printf "%20s %5.2f %5.2f %6.2f\n", '2D', $tu, $ts, $tt;

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




while loop -> map

2005-05-24 Thread Robert Citek


I found a variation of this in the Perl Nutshell book:

$ perl -le '
 $foo="fee fie foe foo" ;
 while ($foo =~ m/e/g ) {
   push @bar, pos $foo ;
 }
 print join(":", @bar); '
2:3:7:11

Is there an equivalent way to do the same using map instead of an  
explicit while loop?  I'm guessing not, since map is expecting a list  
and not a scalar, which $foo is.


Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent




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




Re: assigning printf statement to an array

2005-05-24 Thread DBSMITH
my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files


The map-grep-map-construct is quite common for problems like yours
("But I cannot use $_ b/c $_ is the dir name.").



This is working but it not printing the calculation "days old" for each
filename???  I want to print the aref.
I verified that it is indeed only printing those files over 100 days old,
but I need output such as,

ABDELFATTAH__FAITH_M702515438:  101

as opposed to

ABDELFATTAH__FAITH_M702515438

I will also follow up with a line by line question of the code.
thanks again!
derek




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




   
 John Doe  
   To 
   beginners@perl.org  
 05/24/2005 01:34   cc 
 AM
   Subject 
   Re: assigning printf statement to   
   an array
   
   
   
   
   
   




Am Montag, 23. Mai 2005 20.05 schrieb [EMAIL PROTECTED]:
> Hi all.
>
> This is a continuation from my original question, but with a twist.  I am
> now using sprintf to assign to an array, but I only want to assign to
this
> array of I find a number over 100 from my calculation.  In the array/hash
> also needs to be the directory name.  Here is my code and the sample
> output:
> So in the end my goal is to store the data in this array if the number is
> over 100 and if it is over 100 store the directory name and its
associative
> "days old calc."
> I have tried my @a =(); my $e =0;
> $a[$e++] = sprintf ("%.0f",( $now - ( stat( "${hdir}${file}" ) )[9] ) /
> ONE_DAY ) if ??? > 100;
>
> But I cannot use $_ b/c $_ is the dir name.
>
> thank you,
>
> derek
>
>
>
>
> use strict;
> use warnings;
> $ENV{"PATH"} = qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
>
> my $hdir   = qq(/heartlab/db studies/);
> #my $fdirs =
> qq(/fuji/original/RMH/,/fuji/original/GMC/,/fuji/clinical/GMC/,/fuj
> i/clinical/RMH/);
> $^T=time;
> our $now = time();
> use constant ONE_DAY => 86400;
>
>
> sub date_manip
> {
> my ($month, $day, $year) = (localtime)[4,3,5];
> sprintf ("%02d/%02d/%02d\n", $month+1,$day,($year %
100));
> }
>
> my $dm = &date_manip;
>
> sub date_manip1
> {
> my $days = (shift);
> my ($d, $m, $y) = (localtime($now - $days * ONE_DAY))
> [3..5];
> sprintf ("%02d/%02d/%02d", $m+1,$d,($y % 100));
> }
>
> #my $dmm = &date_manip1(1);
> #chomp $dm;
> #chomp $dmm;
>
>
>
> opendir (DH, $hdir) or die "unable to open directory: $hdir $!";
>
> my @a =();
> my $e =0;
> foreach my $file (sort readdir (DH))
> {
> next if $file eq "." or $file eq "..";
> print "\n";
> print "Days old of HL image files are:\t",$file,":\t";
> #printf ("%.0f", ( $now - ( stat( "${hdir}${file}" ) )[9]
)
> / ONE_DAY );
> $a[$e++] = sprintf ("%.0f",( $now - ( stat(
> "${hdir}${file}" ) )[9] ) / ONE_DAY );
> #date_manip1($1);
> }


instead of the foreach loop you could use a single statement [untested]:
(read from bottom to top)

my @a=
map {$hdir.$_->[1]} # select filename
grep {$_->[0] > 100} # exclude age <= 100
map { [
  sprintf ("%.0f",( $now - ( stat("${hdir}$_" ) )[9] ) / ONE_DAY ),
  $_
 ] } # create aref [age, filename]
grep {$_ ne "." and $_ ne ".."} # exclude unwanted
sort readdir DH; # get all files


The map-grep-map-construct is quite common for problems like yours
("But I cannot use $_ b/c $_ is the dir name.").

(Schwarz'sche Transformation in german).


hth, joe


> closedir (DH) or warn "unable to close DH: $hdir $!;"
>
> ___END CODE___
>
> ___BEGIN_DATA___
>
> Days old of HL image files are: 0_LEARY_ALANRA002848:   68
> Days old of HL image files are: AARON_YAZAWA_CHRISTEHD00

Re: a question about print in array

2005-05-24 Thread Jeff 'japhy' Pinyan

On May 24, Frank said:


print OUTPUT "@items\n";


When you place an array inside quotes, it's the same as saying

  join($", @array)

where $" is the "list separator" variable, whose default value is " " (a 
single space).


Thus, if @array is ("this", "that", "those"), then "@array" is "this that 
those".  However, if @array is ("this\n", "that\n", "those\n"), then 
"@array" is "this

 that
 those
".

Don't put the array in quotes when you're printing it to a file.

  print OUTPUT @items;

--
Jeff "japhy" Pinyan %  How can we ever be the sold short or
RPI Acacia Brother #734 %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %-- Meister Eckhart

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




a question about print in array

2005-05-24 Thread Frank

I met an interesting problem recently and am expecting your kind advice.

my input file (for_test)  is like as follows.   I wish add a ">"  to the 
first line (before the word blue) and remove ">" at the last line.


# ---begining of the file, this line is not included in the file---#
blue
sky
skirt
sea
>white
paper
flower
mine
>red
face
flower
>milk_white
milk
ivory
>green
grand
tree
>
#___END,this line is not included in the file__#

my program is as this :

# to add the > to the first line and remove > at last line

#!/usr/bin/perl -w
use strict;

my $infile = for_test;

open (INPUT, "$infile") || die " can not open $infile";
open OUTPUT, ">test.res" ||die "can not open test.res";

my @items = ;

my $item = ">$items[0]";
shift (@items);
unshift (@items, $item);
pop (@items);
chomp ($items[-1]);
print OUTPUT "@items\n";

close OUTPUT;
close INPUT;
#___END_#



After execute the program.  The result was not as what I expected since 
the ">" add a new column at the left side. 

Iin the first line ">blue" ,  ">" is added by the program.  But it also 
add a new column to the file at the left side.  This is not what I 
expected becuase this also add a space to  other element at the left 
side when print it. 



>blue
 sky
 skirt
 sea
 >white
 paper
 flower
 mine
 >red
 face
 flower
 >milk_white
 milk
 ivory
 >green
 grand
 tree





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




chr function on z/OS.

2005-05-24 Thread rajarshi das
Hi,

I have a basic doubt regarding unicode and z/OS
(ebcdic : ibm-1047).

$a = chr(0x00A1);

$b = chr(0xA1); 

Should $a and $b be equal or yield different results ?


$b is definitely the character "~". Is $a also the
same thing or is it the character equivalent to "\xAA"
? 

$a on linux gives me the char "inverted exclamation
mark" which is the character at codepoint "A1" on
codepage-819. 

Is the argument to the chr function for $a a unicode
scalar value ? 
If yes, $a on ascii and ebcdic should be the same
("A1" on ascii and "AA" on ebcdic) ? 

Thanks in advance,
Rajarshi.



__ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new Resources site
http://smallbusiness.yahoo.com/resources/

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




Re: hash reference help

2005-05-24 Thread Ing. Branislav Gerzo
Charles K. Clarkson [CKC], on Tuesday, May 24, 2005 at 08:16 (-0500)
thinks about:

CKC> It would be helpful to know what $hr looks like. Do this, and then
CKC> show us what it looks like. Show a partial dump if this is too long.
CKC> use Data::Dumper 'Dumper';
CKC> print Dumper $hr;

in select I have around 30 columns, I think every structure from 
$get->fetchall_hashref('id')
looks like this:

'10' => { 'foo' => 'bar',
  'bar' => 'foo'
}

so this will not help.

CKC> : while (my ($id, $value) = each(%$hr)) {
CKC> :   get_something();

CKC> If you want to operate on a particular piece of data in a subroutine,
CKC> then pass that data (or a reference to that data) to the subroutine.
CKC> get_something( $hr );

I know, this is more proper, but not in this case, $hr is global and
it is small example, but you have right, I should use _always_ this
type.

CKC> Catch the passed data structure. Avoid using variables in a subroutine
CKC> which are not defined or passed into that subroutine.
CKC> my $hr = shift;

you have right of course, but this don't change the behavior.

CKC> : foreach my $k ( reverse sort { $hr->{$a}{counter} <=>
CKC> : $hr->{$b}{counter} } (keys( %$hr )) ) {
CKC> : print $k;
CKC> : }

CKC>  Avoid one letter variable names and neatly wrap long lines.

$k was $key :) it was just only for test. I don't wrap a lot, I have
19" monitor, but when sending to list I should wrap, that's true.

CKC> Give us a working example. Something we can copy to an editor and
CKC> run to see your error. Help us help you find the problem.

it was easy mistake, everything we should know about this is avoid
using keys() on %$hr in while loop, because it resets iterator. That's
all. The solution ? I have to redesign my script.

-- 

How do you protect mail on web? I use http://www.2pu.net

[It's kind of fun doing the impossible.]



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




Re: Graphic files visual modofocation in Perl

2005-05-24 Thread Vladimir D Belousov

Chris Devers wrote:


On Tue, 24 May 2005, Vladimir D Belousov wrote:

 


Whether there is a modules for geometry transformation of the images?
In particular, I need rotate and change geometry (perspective,
resize)?
   



Yes.

ImageMagick is the most common way to do this. It's a toolkit for making
all kinds of image manipulations, and it has both command line tools and
interfaces for driving it from Perl.

   
   
   

Imager is a newer smaller, and more directed way to do graphics in Perl.
It can't do everything ImageMagick/PerlMagick can do, but it's a bit
easier to use, and should generally run faster for similar tasks.

   
   

The books _Graphics Programming in Perl_ and _Perl Graphics Programming_
get into all of this & more.

   
   

   
   

Hope this helps.
 


Yes! Thank you very much!



 




--
Vladimir D Belousov




Re: Graphic files visual modofocation in Perl

2005-05-24 Thread Chris Devers
On Tue, 24 May 2005, Vladimir D Belousov wrote:

> Whether there is a modules for geometry transformation of the images?
> In particular, I need rotate and change geometry (perspective,
> resize)?

Yes.

ImageMagick is the most common way to do this. It's a toolkit for making
all kinds of image manipulations, and it has both command line tools and
interfaces for driving it from Perl.





Imager is a newer smaller, and more directed way to do graphics in Perl.
It can't do everything ImageMagick/PerlMagick can do, but it's a bit
easier to use, and should generally run faster for similar tasks.




The books _Graphics Programming in Perl_ and _Perl Graphics Programming_
get into all of this & more.







Hope this helps.


-- 
Chris Devers

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




RE: hash reference help

2005-05-24 Thread Charles K. Clarkson
Ing. Branislav Gerzo  wrote:
: Hi beginners@perl.org,
: 
: I fetch results from table with fetchall_hashref, here is my snippet:
: 
: my $hr = $get->fetchall_hashref('id');


It would be helpful to know what $hr looks like. Do this, and then
show us what it looks like. Show a partial dump if this is too long.

use Data::Dumper 'Dumper';

print Dumper $hr;


: 
: while (my ($id, $value) = each(%$hr)) {
:   get_something();

If you want to operate on a particular piece of data in a subroutine,
then pass that data (or a reference to that data) to the subroutine.

get_something( $hr );


: }
: 
: sub get_something {

Catch the passed data structure. Avoid using variables in a subroutine
which are not defined or passed into that subroutine.

my $hr = shift;

: foreach my $k ( reverse sort { $hr->{$a}{counter} <=>
: $hr->{$b}{counter} } (keys( %$hr )) ) {
: print $k;
: }

 Avoid one letter variable names and neatly wrap long lines.

foreach my $key (
reverse
sort { $hr->{$a}{counter} <=> $hr->{$b}{counter} }
keys %$hr ) {

print "$key\n";
}





: }
: 
: The troubles goes in while loop. Of course, there are some more things
: inside, and get_something() change the hash - when I call
: it, it gives me strange results (unitialized value...) in while loop.

Give us a working example. Something we can copy to an editor and
run to see your error. Help us help you find the problem.

HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328



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




RE: To Thread or not?

2005-05-24 Thread Tielman Koekemoer \(TNE\)
Thanks, I had a look at fork() and system(). I think system() would
work better in my case.

Thanks!


-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED] 
Sent: 24 May 2005 02:34 PM
To: Tielman Koekemoer (TNE); Perl Beginners
Subject: RE: To Thread or not?

Tielman Koekemoer (TNE) wrote:
> Hi all,
> 
> I need to execute a shell command in a loop but the shell command 
> takes about 1 minute to complete. I want to execute the shell
command 
> but but then continue processing. Usually I would "background" the 
> process with & but this does not work as Perl waits for the process
to 
> exit before returning. Should I be using Thread to do this or is
there 
> another way?
> 
> I can write a shell script which then has the & included but that's 
> messy and I want to keep it clean.
> 
> E.g.
> 
> While (1) {
> 
> Some processing here..
> 
> ` execute some shell script/command here`; # takes about 60 seconds
to 
> complete
> 
> Would like to start processing as soon as the above command is 
> executed...
> 
> }

Use system("cmd &") instead of backticks and you can run the external
program in the background. Backticks imply that you want the output of
the external command, which would only be available by waiting for the
command to finish.

perldoc -q 'background'

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




Graphic files visual modofocation in Perl

2005-05-24 Thread Vladimir D Belousov

Hallo all!

I beg your pardon for this question - the question more theoretical, 
rather than practical.


Whether there is a modules for geometry transformation of the images?
In particular, I need rotate and change geometry (perspective, resize)?

Thanks a lot for any answers!

--
Vladimir D Belousov



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




RE: To Thread or not?

2005-05-24 Thread Bob Showalter
Tielman Koekemoer (TNE) wrote:
> Hi all,
> 
> I need to execute a shell command in a loop but the shell command
> takes about 1 minute to complete. I want to execute the shell command
> but but then continue processing. Usually I would "background" the
> process with & but this does not work as Perl waits for the process to
> exit before returning. Should I be using Thread to do this or is there
> another way?
> 
> I can write a shell script which then has the & included but that's
> messy and I want to keep it clean.
> 
> E.g.
> 
> While (1) {
> 
> Some processing here..
> 
> ` execute some shell script/command here`; # takes about 60 seconds to
> complete
> 
> Would like to start processing as soon as the above command is
> executed...
> 
> }

Use system("cmd &") instead of backticks and you can run the external
program in the background. Backticks imply that you want the output of the
external command, which would only be available by waiting for the command
to finish.

perldoc -q 'background'

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




hash reference help

2005-05-24 Thread Ing. Branislav Gerzo
Hi beginners@perl.org,

I fetch results from table with fetchall_hashref, here is my snippet:

my $hr = $get->fetchall_hashref('id');

while (my ($id, $value) = each(%$hr)) {
  get_something();
}

sub get_something {
foreach my $k ( reverse sort { $hr->{$a}{counter} <=> $hr->{$b}{counter} } 
(keys( %$hr )) ) {
print $k;
}
}

The troubles goes in while loop. Ofcourse there are some more things
inside, and get_something() change the hash - when I call
it, it gives me strange results (unitialized value...) in while loop.

Could someone give me advice how to do that ?

Thanks a lot.

--

 --. ,--  ,- ICQ: 7552083  \|||/`//EB: www.2ge.us
,--' |  - |--IRC: [2ge](. .),\\SN: 2ge!2ge_us
`+==+=+===~  ~=-o00-(_)-00o-~
"What the hell is that?" Chekov
 




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




RE: To Thread or not?

2005-05-24 Thread brian . barto
If you are using a unix system, the fork function may help you.

-Original Message-
From: Tielman Koekemoer (TNE) [mailto:[EMAIL PROTECTED]
Sent: Tuesday, May 24, 2005 6:14 AM
To: Perl Beginners
Subject: To Thread or not?



Hi all,

I need to execute a shell command in a loop but the shell command
takes about 1 minute to complete. I want to execute the shell command
but but then continue processing. Usually I would "background" the
process with & but this does not work as Perl waits for the process to
exit before returning. Should I be using Thread to do this or is there
another way? 

I can write a shell script which then has the & included but that's
messy and I want to keep it clean.

E.g.

While (1) {

Some processing here..

` execute some shell script/command here`; # takes about 60 seconds to
complete

Would like to start processing as soon as the above command is
executed...

}

TIA!

Tielman Koekemoer
IT Solution Delivery - NFMS
Office: 012-311 4039
Fax: 012-321 5302
Cell: 083-445 0019



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




RE: freebsd / ifconfig

2005-05-24 Thread Bob Showalter
Chris Knipe wrote:
> Hi,
> 
> Is there any modules available to manipulate devices (similar to
> ifconfig) in perl?  Basically, I need to give it a IP address, it
> must find out what interface the IP belogs to, and what pid opened
> the interface ??? 

I doubt it, because the process is so OS-specific. You might try hunting
around here.

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

You should probably just have your perl script make calls to ifconfig,
netstat, etc. to get the information you want.

HTH

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




Count special character

2005-05-24 Thread Joel Divekar
Hi All

I want to search a string for a special character and
count the occurance. 

My code is as follows :

--

#!/usr/bin/perl -w

my $a = "a:b:c:d:e:f:\:↔:↔:";

my $count = ($a =~ tr/\chr(29)//);

print $count;

--

Following code works but I prefer to use chr(29)
instead of ↔

my $count = ($a =~ tr/↔//);

Please advice.

Regards

Joel
Mumbai, India
9821421965



__ 
Do you Yahoo!? 
Make Yahoo! your home page 
http://www.yahoo.com/r/hs

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




To Thread or not?

2005-05-24 Thread Tielman Koekemoer \(TNE\)

Hi all,

I need to execute a shell command in a loop but the shell command
takes about 1 minute to complete. I want to execute the shell command
but but then continue processing. Usually I would "background" the
process with & but this does not work as Perl waits for the process to
exit before returning. Should I be using Thread to do this or is there
another way? 

I can write a shell script which then has the & included but that's
messy and I want to keep it clean.

E.g.

While (1) {

Some processing here..

` execute some shell script/command here`; # takes about 60 seconds to
complete

Would like to start processing as soon as the above command is
executed...

}

TIA!

Tielman Koekemoer
IT Solution Delivery - NFMS
Office: 012-311 4039
Fax: 012-321 5302
Cell: 083-445 0019




Re: undefined...

2005-05-24 Thread John W. Krahn

Ley, Chung wrote:


I didn't know the use of Data::Dumper, wish I had known it earlier...  
Basically,
I was trying to mimic what the Boxplot.pl was doing, and so I did a loop like 
this:
for (my $k; defined $data[0][1][$k]; $k++ )
{
print "data[0][1][$k] is defined\n";
		print "data[0][1][$k] = $data[0][1][$k];\n";  
}

I discovered that I have a number of elements that are defined but with no real
values

I have never used the "Devel-ebug" before, I will try to learn...

But in trying to "replicate" the same problem, I seem to find something that is
odd...  Basically, instead of "looping" thru all the data, I mimic the same 
place
where I am having the problem by "hard-coding" the data array for just ONE very
simple boxplot based on real data(see later).  The code works when I put it in 
its
own file - no warnings...  But it doesn't work when I am running it in my "main"
program!  The warnings are something like these:
Use of uninitialized value in array element at 
/usr/lib/perl5/site_perl/5.8.0/GD/Graph/boxplot.pm line 470.
Use of uninitialized value in array element at 
/usr/lib/perl5/site_perl/5.8.0/GD/Graph/boxplot.pm line 472.
Use of uninitialized value in numeric gt (>) at 
/usr/lib/perl5/site_perl/5.8.0/GD/Graph/boxplot.pm line 472.


The problem is that the initial value in $k is undef so you should change:

for (my $k; defined $data[0][1][$k]; $k++ )

To:

for (my $k = 0; defined $data[0][1][$k]; $k++ )


Perhaps what you really want to do is:

for my $k ( 0 .. $#{$data[0][1]} )
{
print "data[0][1][$k] is defined\n";
print "data[0][1][$k] = $data[0][1][$k];\n";
}



So, I was curious...  Basically, I then "move" this hardcoding to the "very
beginning" of my main program BEFORE even my variable declarations and etc, and 
it
still had the same warnings!!!  I am dumbfounded...  I am thinking that somehow
maybe the perl compiler/interpreter is doing some optimzation that is different
when the "size" of your perl program is at a different size



First Code 
sampe
#!/usr/bin/perl
$| = 1;   # AutoFlush

use strict;
use warnings;
use DBI;
use GD;
use Benchmark;
use POSIX;
use TSGPlot;

my (@data, $ret);
$data[0][0] = 'AMD133';
push (@{$data[1][0]}, 97.6);
push (@{$data[1][0]}, 97.5);
push (@{$data[1][0]}, 98.4);
push (@{$data[1][0]}, 96.4);
push (@{$data[1][0]}, 95.5);
push (@{$data[1][0]}, 97.1);
push (@{$data[1][0]}, 97.5);
push (@{$data[1][0]}, 100.0);
push (@{$data[1][0]}, 98.4);


Instead of pushing nine elements onto the array separately you can push the
whole list at once:

push @{$data[1][0]}, 97.6, 97.5, 98.4, 96.4, 95.5, 97.1, 97.5, 100.0, 
98.4;




John
--
use Perl;
program
fulfillment

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