Re: Still pondering working with hashs

2010-05-11 Thread Harry Putnam
Shawn H Corey  writes:

Oh nice... thanks.  Hope I can get to try this out later tonight... I
have to go out for a while and can't get to it right now though.

The main `for loop' near the end, and really, all of it, looks to be
highly portable like the inversion code was I think that little
inversion sub function could be dropped in about anywhere in code
using hashs and just go work with no fuss.



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Still pondering working with hashs

2010-05-11 Thread Harry Putnam
Jim Gibson  writes:

Harry wrote:
>> Shawn, hoping to pester you once more about this topic.

Jim G responded:
> It is not fair to single out Shawn for help. Just post your question
> and hope for a response.

Just a manner of speaking, but you're right it does appear to be a
little off the wall.  It wasn't my intent to elicit responses from
only Shawn... It was just that he was the one who authored the nifty
inversion sub routine.

[...] 

Jim wrote:
> Using only "inverted" hashes (untested):
>
>   for my $file ( sort keys %inv_d1h ) {
> if( exists $inv_d2h{$file} ) {
>   print "Duplicate file names found: ", scalar @{$inv_d1h{$file}},
>" in $d1 and ", scalar @{$inv_d2h{$file}}, " in $d2\n";
>   print "\n$d1:\n", join("\n",@{inv_d1h{$file}}), "\n";
>   print "\n$d2:\n", join("\n",@{inv_d2h{$file}}), "\n";
> }
>   }

Haven't gotten to try this yet... but it made me wonder right off why
I'd want to do that.  I'm on my way out the door in a moment but
curious now... about your reasoning.

Also eager to put that into a real script and try it out. You've made
a neat job  of it.

Is there some gain in doing it with both hashes inverted?  Does it
simplify things in some way, or is it mainly an example of another way
to go at it?

It doesn't seem to take much time at all to do the inversion, seems
more or less instantaneous in fact.  That surprised me a bit, when one 
of those hashs has something like 4000 lines.

I noticed quite a marked gain comparing my original approach to the
code you were responding to in your message above.

Originally went something like this (not actual code):

  foreach my $keyd1 (keys %d1h){

 foreach my $keysd2 (keys %d2h){
if($d1h{ $keyd1 } eq $d2h{ $keyd2 } {
  push @matches, $d2h{ $keyd2 };
}
 }
 ## process @matches 
dispatch_table($dh1{ $keyd1 }, @matches);
  }

That really puts the whammy on resources since it marches some 4000
possible matches for each line of %d1h (which is a nearly 2000 lines)
through the gauntlet. 8,000,000 or so lines in the actual event.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Still pondering working with hashs

2010-05-11 Thread Shawn H Corey

On 10-05-11 04:52 PM, Harry Putnam wrote:

Some selective output first:

   [...]

   d1 ./dir1/etc/images/gnus/exit-summ.xpm
   d2(1)  ./dir2/etc/images/gnus/exit-summ.xpm

   d1 ./dir1/etc/images/gnus/reply.xpm
   d2(1)  ./dir2/etc/images/mail/reply.xpm
   d2(2)  ./dir2/etc/images/gnus/reply.xpm

   d1 ./dir1/etc/images/gnus/README
   d2(1)  ./dir2/src/m/README
   d2(2)  ./dir2/etc/e/README
 [...]
   d2(47)  ./dir2/doc/lispintro/README

   d1 ./dir1/lisp/gnus-util.el
   d2(1)  ./dir2/lisp/gnus/gnus-util.el

   [...]



If you want the output only in that format, construct your internal 
representation to make it easy to process.


#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

use File::Find;

my %Paths_for = ();
my $Base_dir;
my $Table_format = "%-20s  %10s  %s\n";

sub wanted {
  if( -f ){
push @{ $Paths_for{$_}{$Base_dir} }, $File::Find::name;
  }
}

for my $dir ( @ARGV ){
  $Base_dir = $dir;
  find( \&wanted, $Base_dir );
}

# print '%Paths_for: ', Dumper \%Paths_for;

for my $fname ( sort keys %Paths_for ){
  my $count = '';
  for my $base_dir ( sort keys %{ $Paths_for{$fname} } ){
for my $path ( @{ $Paths_for{$fname}{$base_dir} } ){
  my $enclosed_count = '';
  $enclosed_count = "($count)" if $count;
  printf $Table_format, $base_dir, $enclosed_count,  $path;
  $count ++;
}
  }
  print "\n";
}

__END__


--
Just my 0.0002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove and substitute character in a string with a regex

2010-05-11 Thread C.DeRykus
On May 10, 5:29 am, t.baetz...@bringe.com (Thomas Bätzler) wrote:
> Hi Shlomi,
>
> > use warnings is preferable to the -w flag.
>
> Not in my book. The command line switch turns on warnings globally, whereas 
> the "use warnings;" pragma only enables them in the current lexical scope. So 
> by using the command line switch, I get warnings about crappy third-party 
> code, too.
>
> The only case where "use warnings;" might be better is if you're using a 
> braindead shell/OS that doesn't look at the shebang line.
>

Actually,  perl helps the braindead too. At least,
as far as I can see from a quick read of perlrun:

The #! line is always examined for switches
as the line is being parsed. Thus, if you're on
a machine that allows only one argument with
the #line, or worse, doesn't even recognize the
#! line, you still can get consistent switch behavior
regardless of how Perl was invoked, even if -x
was used to find the beginning of the program.

There are a couple of gotcha's mentioned in perlrun's
subsequent paragraphs:  'perl' needs to be seen on the
shebang line; more than 32 char's historically could be
problematic; redundant switch traps, etc.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Still pondering working with hashs

2010-05-11 Thread Jim Gibson
On 5/11/10 Tue  May 11, 2010  1:52 PM, "Harry Putnam" 
scribbled:

> Shawn H Corey  writes:
> 
>> Harry Putnam wrote:
>>> But, is there an easier way?
>> 
>> Invert both hashes and find the keys in both inverses.
> 
> Shawn, hoping to pester you once more about this topic.

It is not fair to single out Shawn for help. Just post your question and
hope for a response.

> %d1h is made up like this
> 
>this is key  this is value
>   $File::Find::name  = $_

The use of the equal sign '=' in the above makes it look like you are
assigning a value to $File::Find::name. It is better to stick to Perl
syntax:

$d1h{$File::Find::name} = $_;

for example:

$d1h{'./dir1/sub/fname'} = 'fname';

> So cutting to the chase, I try to take advantage of your inversion
> code.  It seems to work well, but I'm not confident enough to know
> if there are possible hidden gotchas someone with more experience
> might see right off the bat.
>  
> ----   ---=---   -  
> Some selective output first:
> 
>   [...]
> 
>   d1 ./dir1/etc/images/gnus/exit-summ.xpm
>   d2(1)  ./dir2/etc/images/gnus/exit-summ.xpm
> 
>   d1 ./dir1/etc/images/gnus/reply.xpm
>   d2(1)  ./dir2/etc/images/mail/reply.xpm
>   d2(2)  ./dir2/etc/images/gnus/reply.xpm
> 
>   d1 ./dir1/etc/images/gnus/README
>   d2(1)  ./dir2/src/m/README
>   d2(2)  ./dir2/etc/e/README
> [...] 
>   d2(47)  ./dir2/doc/lispintro/README
> 
>   d1 ./dir1/lisp/gnus-util.el
>   d2(1)  ./dir2/lisp/gnus/gnus-util.el

You can use the Unix file command (if you are on Unix) to find the files
with a certain name:

find dir1 -name README

etc. to check the results of your program.

Your program below looks fine. I see no obvious defects. You can generate
the inverted hashes in the find routines (see below). You can use the
inverted hashes only (see below).

> #!/usr/local/bin/perl
> 
> use strict;
> use warnings;
> use File::Find;
> #use diagnostics;
> 
> my %d1h;
> my %d2h;
> my $d1tag = 'd1';
> my $d2tag = 'd2';
> 
> ## Make sure we are  feed two directory names
> ( my ( $d1, $d2 ) = @ARGV ) == 2
> or die "\nUsage: $0 ./dir1 ./dir2\n";
> 
>   ## Make sure incoming directory names exist
>   for ($d1, $d2 ){
>  ( -d ) or die "<$_> cannot be found on the file system";
>   }
> 
>   ## Build the hashs
> 

my( %inv_d1h, %inv_d2h );

>   find sub {
>   return unless -f;
>   $d1h{ $File::Find::name } = $_;

push( @{$inv_d1h{$_}}, $File::Find::name );

> },$d1;
> 
>   find sub {
>   return unless -f;
>   $d2h{ $File::Find::name } = $_;

push( @{$inv_d2h{$_}}, $File::Find::name );

> },$d2;
> 
> ## Invert 1 hash and it needs to be the second one on cmd line
> my %inv_d2h = invert( \%d2h );
> 
> sub invert {
>my $h = shift @_;
>my %inv = ();
> 
>while( my ( $k, $v ) = each %{ $h } ){
>  push @{ $inv{$v} }, $k;
>}
>return %inv;
> }
> 
> ## Could have used `values' of %d1h here for $value (values %d1h)
> ## and avoided things like `...@{ $inv_d2h{ $d1h{ $key } } }',
> ## which would then be `...@{ $inv_d2h{ $value } }'  but would
> ## not then have such ready access to the `keys' which are
> ## needed here too, and will be needed later on (not shown here)
> ## for now we just print to show how it works.
> 
> foreach my $key ( keys %d1h ){
>   if(exists $inv_d2h{ $d1h{ $key } }){
>  print "  $d1tag $key\n";
> 
>## separate counter to keep (my) confusion down
>my $matchcnt = 0;
>for ( @{ $inv_d2h{ $d1h{ $key } } } ) {
>print "  $d2tag(" . ++$matchcnt .")  $_\n";
>}
>print "\n";
>   }  
> }
>

Using only "inverted" hashes (untested):

  for my $file ( sort keys %inv_d1h ) {
if( exists $inv_d2h{$file} ) {
  print "Duplicate file names found: ", scalar @{$inv_d1h{$file}},
   " in $d1 and ", scalar @{$inv_d2h{$file}}, " in $d2\n";
  print "\n$d1:\n", join("\n",@{inv_d1h{$file}}), "\n";
  print "\n$d2:\n", join("\n",@{inv_d2h{$file}}), "\n";
}
  }



-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Still pondering working with hashs

2010-05-11 Thread Harry Putnam
Shawn H Corey  writes:

> Harry Putnam wrote:
>> But, is there an easier way?
>
> Invert both hashes and find the keys in both inverses.

Shawn, hoping to pester you once more about this topic.

first:

  Hashes involved are built like this (Using File::Find nomenclature):

(NOT CODE... Just description)
use File::Find;

%d1h is made up like this

   this is key  this is value
  $File::Find::name  = $_ 
# ./dir1/sub/fname   = fname
 
%d2h is made up the same way:
   this is key  this is value
   $File::Find::name  = $_
# ./dir2/what/sub/fname   = fname

Also keeping in mind there are many thousands of lines in both hashs. 

There will be many many ways the path part of those names will differ,
and only some will actually match on the ends (the values in hash
terms) like the two above. Many more will be different, but the
objective here is to find those that matches.

I've found after many hours of tinkering and bugging the heck out of
patient posters here, what I think you were trying to tell me in this 
thread.  I wasn't capable yet of understanding it all.  I'm still not
but quite a lot more now has finally wormed into my pea brain.

So cutting to the chase, I try to take advantage of your inversion
code.  It seems to work well, but I'm not confident enough to know
if there are possible hidden gotchas someone with more experience
might see right off the bat.
 
----   ---=---   -   
Some selective output first:

  [...]

  d1 ./dir1/etc/images/gnus/exit-summ.xpm
  d2(1)  ./dir2/etc/images/gnus/exit-summ.xpm

  d1 ./dir1/etc/images/gnus/reply.xpm
  d2(1)  ./dir2/etc/images/mail/reply.xpm
  d2(2)  ./dir2/etc/images/gnus/reply.xpm

  d1 ./dir1/etc/images/gnus/README
  d2(1)  ./dir2/src/m/README
  d2(2)  ./dir2/etc/e/README
[...] 
  d2(47)  ./dir2/doc/lispintro/README   

  d1 ./dir1/lisp/gnus-util.el
  d2(1)  ./dir2/lisp/gnus/gnus-util.el

  [...]

--- 8< snip -- 8< snip -- 8 cannot be found on the file system";
  }

  ## Build the hashs

  find sub {
  return unless -f;
  $d1h{ $File::Find::name } = $_;
},$d1;

  find sub {
  return unless -f;
  $d2h{ $File::Find::name } = $_;
},$d2;

## Invert 1 hash and it needs to be the second one on cmd line
my %inv_d2h = invert( \%d2h );

sub invert {
   my $h = shift @_;
   my %inv = ();

   while( my ( $k, $v ) = each %{ $h } ){
 push @{ $inv{$v} }, $k;
   }
   return %inv;
}

## Could have used `values' of %d1h here for $value (values %d1h) 
## and avoided things like `...@{ $inv_d2h{ $d1h{ $key } } }',
## which would then be `...@{ $inv_d2h{ $value } }'  but would
## not then have such ready access to the `keys' which are
## needed here too, and will be needed later on (not shown here)
## for now we just print to show how it works.

foreach my $key ( keys %d1h ){
  if(exists $inv_d2h{ $d1h{ $key } }){
 print "  $d1tag $key\n";

   ## separate counter to keep (my) confusion down
   my $matchcnt = 0;
   for ( @{ $inv_d2h{ $d1h{ $key } } } ) {
   print "  $d2tag(" . ++$matchcnt .")  $_\n";
   }
   print "\n";
  }  
}




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Developing Complex Event Processing (CEP) applications in perl

2010-05-11 Thread Jeff Pang
2010/5/11 Amit Saxena :
> Hello all,
>
> Please let me know if anybody has any idea on the development of Complex
> Event Processing (CEP) applications in perl.
>

Maybe POE is your friend:
http://poe.perl.org/


-- 
Tech support agency in China
http://duxieweb.com/

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: fork, parallel and global values

2010-05-11 Thread C.DeRykus
On May 10, 7:07 am, raphael.j...@gmail.com ("raphael()") wrote:
> Hello,
>
> -- CODE --
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
> use Parallel::ForkManager;
>
> # Parallel::ForkManager
> my $pfm = Parallel::ForkManager->new(5);
>
> my %hash;
> for my $num ( qw/ 1 2 3 4 5 1 / ) {
>     $pfm->start and next;
>     $hash{$num}++;
>     $pfm->finish;}
>
> $pfm->wait_all_children;
>
> # doesn't print because %hash is undef
> print "$_ => $hash{$_}\n" for sort keys %hash;
>
> print "hash is undef\n" unless %hash;
>
> --- END ---
>
> I want to do work on all elements of an array simultaneously.
> I tried Parallel::ForkManager. It works well when you don't need to update
> global value like downloading
> multiple files at the same time. I want something like above that forks
> while updating global values.
> Are threads/fork what I am looking for?
>
> Any help would be *appreciated.*

If your host supports Sys V IPC, one possible solution:
IPC::Shareable

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Developing Complex Event Processing (CEP) applications in perl

2010-05-11 Thread Amit Saxena
Hello all,

Please let me know if anybody has any idea on the development of Complex
Event Processing (CEP) applications in perl.

I have been assigned a task for development of a complex event processing
(CEP) application and I would like to do the same in perl that is my
favorite language.

Thanks & Regards,
Amit Saxena


Re: Closing File Handles

2010-05-11 Thread Whacky™
On Apr 30, 9:30 pm, jcas...@activenetwerx.com ("Joseph L. Casale")
wrote:
> Is it required to manually close a file handle I used
> to write debugging info to when the Perl scripts exits?
>
> Seems like a waste of effort at the end of the script to
> test `if debug...` and close the fh after?
>
> Thanks!
> jlc

Closing a "File handle" is one good practice one should follow.. but
perl will not throw error if you don't


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove and substitute character in a string with a regex

2010-05-11 Thread C.DeRykus
On May 9, 10:03 am, blog.h...@gmail.com (Finalfire) wrote:
> Hello guys! I'm skilling regex using Perl and i've some trouble about
> a simple try:
> i've a string like:
>
> $string = "HELLAAABB";
>
> and i want to manipulate in that way: HELL4O3ABB4C;You can simply
> notice that when i have 3 or more occurrences of a character, i want
> to substitute all the occurrences and write "nC" where n is how times
> the character C is found on a string.
>
> So, in regex (i think there are so many way to do it but i wish to do
> with regex, just skilling...) i write:
>
> $string =~ s/(.)\1\1+/$1/;
>
> but how can i get the number of the occurrences in the string of that
> pattern?
>

$string =~ s{  (   # capture group
   (.) # capture char.
   \2#  char. again
   \2+  #   char. 1/more
 )   # end group
 }
 {
 length($1) . $2
 }gex;

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: AW: AW: Remove and substitute character in a string with a regex

2010-05-11 Thread Dr.Ruud

Thomas Bätzler wrote:
> (attribution fixed) Shlomi Fish:


use warnings is preferable to the -w flag.


Not in my book. The command line switch turns on warnings globally, whereas the "use 
warnings;" pragma only enables them in the current lexical scope. So by using the 
command line switch, I get warnings about crappy third-party code, too.



I strongly prefer "-w" as well, also in the shebang line.

Even knowing the shortcomings of the Perl warnings logic,
that Abigail has written interesting things about:
http://www.google.co.uk/#q=abigail+"no+warnings+'syntax'"

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Remove and substitute character in a string with a regex

2010-05-11 Thread C.DeRykus
On May 10, 3:53 am, shlo...@iglu.org.il (Shlomi Fish) wrote:
> Hi Thomas,
>
> a few comments on your code.
>
> On Monday 10 May 2010 13:45:53 Thomas Bätzler wrote:
>
>
>
> > Finalfire  asked:
> > > Hello guys! I'm skilling regex using Perl and i've some trouble about
> > > a simple try:
> > > i've a string like:
>
> > > $string = "HELLAAABB";
>
> > > and i want to manipulate in that way: HELL4O3ABB4C;You can simply
> > > notice that when i have 3 or more occurrences of a character, i want
> > > to substitute all the occurrences and write "nC" where n is how times
> > > the character C is found on a string.
>
> > > So, in regex (i think there are so many way to do it but i wish to do
> > > with regex, just skilling...) i write:
>
> > > $string =~ s/(.)\1\1+/$1/;
>
> > > but how can i get the number of the occurrences in the string of that
> > > pattern?
>
> > #!/usr/bin/perl -w
>
> use warnings is preferable to the -w flag.
>
> > use strict;
>
> > my $string = "HELLAAABB";
>
> > print "$string\n";
>
> > $string =~ s/(.)\1{2,}/length($&).$1/eg;
>
> Please don't use $& as it slows down all subsequent regular expression matches
> considerably. See the warning about it on perldoc perlvar. Instead wrap up the
> entire match in an extra parentheses, like Shawn and I demonstrated.
>

Only a problem, if you're doing tons of regexes within the program
though.
Even better, as of 5.10, the /p switch and  ${^PREMATCH}, ${^MATCH},
and ${^POSTMATCH} eliminate the penalty:

As a workaround for this problem, Perl 5.10.0 introduces "$
{^PREMATCH}",
"${^MATCH}" and "${^POSTMATCH}", which are equivalent to $`, $&
and $',
except that they are only guaranteed to be defined after a
successful
match that was executed with the "/p" (preserve) modifier. The use
of
these variables incurs no global performance penalty, unlike their
punctuation char equivalents, however at the trade-off that you
have to
tell perl when you want to use them.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: sort hash with an array as the value

2010-05-11 Thread Shlomi Fish
Hi Owen,

On Tuesday 11 May 2010 14:10:49 Owen wrote:
> I have this statement;
> 
> foreach my $key ( keys %filehash ) {
> print "$key $filehash{$key}->[0]\t\t\t$filehash{$key}->[3]\t
> $filehash{$key}->[2]\t $filehash{$key}->[1]\n";
> }
> 
> but wish to sort the output by $filehash{$key}->[0]. Is it possible? How
> do I do this?

Use http://perldoc.perl.org/functions/sort.html (perldoc -f sort). Untested:

[code]
foreach my $key ( 
sort { $filehash{$a}->[0] cmp $filehash{$b}->[0] } 
keys(%filehash)
)
{
# Do stuff with $key.
.
}
[/code]

Replace "cmp" with "<=>" if you want a numerical sort instead of a string-wise 
(= lexicographical) sort.

perldoc -f sort accepts any arbitrary expression for the comparison. Normally 
it should be symmetric between EXPR($a) COMPARE EXPR($b). Also see the "||" 
operator for chaining comparisons (e.g: << ($a->[0] cmp $b->[0]) || ($a->[1] 
<=> $b->[1]) >>).

Regards,

Shlomi Fish
> 
> I can't make a separate hash of the value because of multiple values of
> the value
> 
> http://perldoc.perl.org/perlfaq4.html#How-do-I-sort-a-hash-(optionally-by-v
> alue-instead-of-key)%3f ( http://goo.gl/Ol31 )
> only shows the sorting where the hash has only a scalar.
> 
> 
> 
> TIA
> 
> 
> Owen

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




AW: sort hash with an array as the value

2010-05-11 Thread Thomas Bätzler
Owen  asked:
> I have this statement;
> 
> foreach my $key ( keys %filehash ) {
> print "$key $filehash{$key}->[0]\t\t\t$filehash{$key}->[3]\t
> $filehash{$key}->[2]\t $filehash{$key}->[1]\n";
> }
> 
> but wish to sort the output by $filehash{$key}->[0]. Is it possible?
> How do I do this?

foreach my $key ( sort { $filehash{$a}->[0] cmp $filehash{$b}->[0] } keys 
%filehash ){ ... }

HTH,
Thomas


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




sort hash with an array as the value

2010-05-11 Thread Owen

I have this statement;

foreach my $key ( keys %filehash ) {
print "$key $filehash{$key}->[0]\t\t\t$filehash{$key}->[3]\t
$filehash{$key}->[2]\t $filehash{$key}->[1]\n";
}

but wish to sort the output by $filehash{$key}->[0]. Is it possible? How
do I do this?

I can't make a separate hash of the value because of multiple values of
the value

http://perldoc.perl.org/perlfaq4.html#How-do-I-sort-a-hash-(optionally-by-value-instead-of-key)%3f
( http://goo.gl/Ol31 )
only shows the sorting where the hash has only a scalar.



TIA


Owen





-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




UTF-8 and Internal Representation of (Latin1) Characters

2010-05-11 Thread Peter Daum
Because I had little need for it I had tried to just ignore Perl's
Unicode support as long as possible. Now it looks like I can't do that
anymore, so I started looking through the various docs.

One thing that confused me: several sources mention Perl using 8-bit
characters as long as possible, which seems to contradict some observations.

"perluniintro" for example says:
  "if all code points in the string are 0xFF or less, Perl uses the
  native eight-bit character set.  Otherwise, it uses UTF-8."

and the documentation for "Encode":
   · When you decode, the resulting UTF8 flag is on unless you can
 unambiguously represent data.  Here is the definition of dis-
 ambiguity.
  After "$utf8 = decode('foo', $octet);",
When $octet is...   The UTF8 flag in $utf8 is
   -
   In ASCII only (or EBCDIC only)OFF
   In ISO-8859-1  ON
   In any other Encoding  ON
   -

But when I look at it with Devel::Peek, it seems like after "decoding"
- the UTF8 flag is always on
- only ASCII characters are stored as bytes,
  everything else is converted to utf-8

> perl -MDevel::Peek -MEncode -e 'Dump(decode latin1 => "\x41")'
SV = PV(0x603e58) at 0x62d620
  REFCNT = 1
  FLAGS = (TEMP,POK,pPOK,UTF8)
  PV = 0x6b3d90 "A"\0 [UTF8 "A"]

> perl -MDevel::Peek -MEncode -e 'Dump("\xf6")'
SV = PV(0x70bda8) at 0x606f10
  REFCNT = 1
  FLAGS = (PADTMP,POK,READONLY,pPOK)
  PV = 0x61ded0 "\366"\0

> perl -MDevel::Peek -MEncode -e 'Dump(decode latin1 => "\xf6")'
SV = PV(0x603e58) at 0x62d620
  REFCNT = 1
  FLAGS = (TEMP,POK,pPOK,UTF8)
  PV = 0x6b3d90 "\303\266"\0 [UTF8 "\x{f6}"]

So, which is true? Is the Unicode documentation obsolete and the
internal representation changed (I know, I should not worry about
internals ;-) or is the output of Devel::Peek::Dump misleading?

Regards,
  Peter


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to use the module: XML::Parser

2010-05-11 Thread Shlomi Fish
Hi Parag,

On Tuesday 11 May 2010 08:50:21 Parag Kalra wrote:
> Hey All,
> 
> I am trying to design some scripts using the module - XML::Parser
> 
> To start  learning I have a very basic scenario. Suppose I have following
> XML file:
> 
> 
> My Tag1
> My Tag2
> My Tag3
> 
> 
> I want to save the the tags as the keys of a Hash and respective content as
> the  value of that hash
> 
> So for the above XML file using the module XML::Parser, I would like to
> create a hash having following key/value pair:
> 
> my %my_hash = (
> tag1 => 'My Tag1',
> tag2 => 'My Tag2',
> tag3 => 'My Tag3',
> );
> 
> Is that possible?
> 

It is possible with XML::Parser, but you really should be using one of the 
many XML-LibXML interfaces instead. If you can afford to read the entire file 
into memory as DOM, then you can use XPath and DOM functions to easily get 
everything:

* http://www.zvon.org/xxl/XPathTutorial/General/examples.html

* http://www.xml.com/pub/a/2001/11/14/xml-libxml.html

If your XML is too large to be used with DOM (and you should verify it first, 
because you shouldn't optimise prematurely), then you can use the XML-LibXML 
pull parser.
 
Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://shlom.in/sw-quality

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




AW: fork, read from child process and timeout

2010-05-11 Thread Thomas Bätzler
Weizhong Dai  asked:
> -
> $pid = open(README, "program arguments |")  or die "Couldn't fork:
> $!\n";
> while () {
> # ...
> }
> close(README)
> --
> 
> my problem is: I read from README, but if  waiting for next input is
> timeout, end reading. Is there any timer or method I could use for
> this purpose?

This code is probably better written as

my $program = ...;
my @arguments = ...;

my $pid = open( my $readme, '-|', $program, @arguments) or die "Can't fork 
'$program': $!";

while( <$readme> ){
  ...
}

close( $readme );

(code OTTOH, please excuse any typos)

i.e. 
- use lexical variables 
- don't use a typeglob for your filehandle, use a lexical variable instead
- use three-argument open for increased safety

As for the timeout question, I assume what you want to do is this:
- read input from $program
- if there's no input, then stop reading after suitable timeout period

Something like that is usually done using select() as shown in the second usage 
example in http://perldoc.perl.org/functions/select.html

HTH,
Thomas



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: IMAP email client: style & security

2010-05-11 Thread Eitan Adler
> perl style is to just use boolean tests and not check for equality to
> some constants. and this means not using FALSE and TRUE constants. so
> you should drop this habit as you won't see it much in perl.
>

Alright - changed. This is exactly why I sent this email to the list - to
learn perl style. If you see anything else that is not often used in perl or
could be improved please let me know.
As I said in the OP this is my first perl program and I'm looking to improve
my style.

>
> uri
>
> --
> Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com--
> -  Perl Code Review , Architecture, Development, Training, Support
> --
> -  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com-
>


Re: fork, read from child process and timeout

2010-05-11 Thread Jeff Pang
2010/5/11 Weizhong Dai :
> Hi all,
>
> -
> $pid = open(README, "program arguments |")  or die "Couldn't fork: $!\n";
> while () {
>    # ...
> }
> close(README)
> --
>
> my problem is: I read from README, but if  waiting for next input is
> timeout, end reading. Is there any timer or method I could use for
> this purpose?
>

Just a reference:
http://www.perlmonks.org/?node_id=575284


-- 
Tech support agency in China
http://duxieweb.com/

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How to use the module: XML::Parser

2010-05-11 Thread Jeff Pang
2010/5/11 Parag Kalra :
> Hey All,
>
> I am trying to design some scripts using the module - XML::Parser
>
> To start  learning I have a very basic scenario. Suppose I have following
> XML file:
>
> 
> My Tag1
> My Tag2
> My Tag3
> 
>
> I want to save the the tags as the keys of a Hash and respective content as
> the  value of that hash
>
> So for the above XML file using the module XML::Parser, I would like to
> create a hash having following key/value pair:
>
> my %my_hash = (
>        tag1 => 'My Tag1',
>        tag2 => 'My Tag2',
>        tag3 => 'My Tag3',
>    );
>
> Is that possible?
>

Sure it's possible.

$ cat xml.pl
use strict;
use XML::Simple;
use Data::Dumper;

my $xml=<
My Tag1
My Tag2
My Tag3

EOF

my $re = XMLin($xml);
print Dumper $re;


$ perl xml.pl
$VAR1 = {
  'tag3' => 'My Tag3',
  'tag1' => 'My Tag1',
  'tag2' => 'My Tag2'
};


-- 
Tech support agency in China
http://duxieweb.com/

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




fork, read from child process and timeout

2010-05-11 Thread Weizhong Dai
Hi all,

-
$pid = open(README, "program arguments |")  or die "Couldn't fork: $!\n";
while () {
# ...
}
close(README)
--

my problem is: I read from README, but if  waiting for next input is
timeout, end reading. Is there any timer or method I could use for
this purpose?

thanks.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: IMAP email client: style & security

2010-05-11 Thread Uri Guttman
> "EA" == Eitan Adler  writes:

  >> Constants are usually written in all uppercase to distinguish them from
  >> keywords, functions, operators and subroutines.  How did you choose the
  >> arbitrary values 0 and 1 for false and true instead of using other values?
  >> Why did you name them false and true instead of zero and one?
  >> 

  EA> I changed them to uppercase. I call them TRUE and FALSE in order
  EA> to get the appearance of a Boolean construct.  In my opinion $var
  EA> = TRUE; is easier to read than $var = 1; I could have used any
  EA> arbitrary number that evaluates to true or false.

do you realize perl has 5 false values and rarely do you see constants
like TRUE??

these are all false:

0, 0.0, '0', '' and undef.

so defining a FALSE makes little sense if you could possibly get any of
those. also checking for a FALSE constant can be tricky. do you do $bool
== FALSE or eq FALSE? you need to know the type of FALSE for that.

perl style is to just use boolean tests and not check for equality to
some constants. and this means not using FALSE and TRUE constants. so
you should drop this habit as you won't see it much in perl. 

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: IMAP email client: style & security

2010-05-11 Thread Eitan Adler
 I made the changes below and I'd like to know if there is anything else I
could do to improve the quality of my code.

use constant false => 0;
>>use constant true  => 1;
>>
>
> Constants are usually written in all uppercase to distinguish them from
> keywords, functions, operators and subroutines.  How did you choose the
> arbitrary values 0 and 1 for false and true instead of using other values?
>  Why did you name them false and true instead of zero and one?
>
I changed them to uppercase. I call them TRUE and FALSE in order to get the
appearance of a Boolean construct.
In my opinion $var = TRUE; is easier to read than $var = 1; I could have
used any arbitrary number that evaluates to true or false.

But why do you need to define those keys at all when autovivification can
> usually take care it for you?
>
a) readability (it tells you right away what possible keys I will use)
b) I didn't know about autovivification


>
>
> You don't have a default {} block so you are saying that $coml[0] will only
> ever contain 'command' or 'reply'?  The only thing you do with $command{
> reply } is set it to 0 at the beginning and set it to 1 here so why do you
> even have this variable?
>

Its for an unfinished feature in which is it would either email the output
back to me or save it as a file.

Thanks for your reply and time.

John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity.   -- Damian Conway
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>