[30364] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 1607 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 3 14:09:44 2008

Date: Tue, 3 Jun 2008 11:09:13 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 3 Jun 2008     Volume: 11 Number: 1607

Today's topics:
        advice on good perl idiom <mhunter@lusArs.net>
    Re: advice on good perl idiom <1usa@llenroc.ude.invalid>
    Re: advice on good perl idiom <jurgenex@hotmail.com>
    Re: advice on good perl idiom <ben@morrow.me.uk>
    Re: advice on good perl idiom <wahab@chemie.uni-halle.de>
    Re: Counting lines in big number of files - in parallel <jurgenex@hotmail.com>
    Re: Counting lines in big number of files - in parallel xhoster@gmail.com
        formatting question <xiaoxia2005a@yahoo.com>
    Re: HTML::Tokerparser / parsing <p>,<br> <1usa@llenroc.ude.invalid>
    Re: HTML::Tokerparser / parsing <p>,<br> <mislam@nospam.ciuc.edu>
        MD5 and SHA1 fingerprint from SSL certificates <toralf.foerster@gmx.de>
    Re: Perl grep and Perl 4 fourfour2@gmail.com
    Re: Perl grep and Perl 4 <1usa@llenroc.ude.invalid>
    Re: Perl grep and Perl 4 <glex_no-spam@qwest-spam-no.invalid>
    Re: Perl grep and Perl 4 fourfour2@gmail.com
    Re: Perl grep and Perl 4 <glennj@ncf.ca>
    Re: Perl grep and Perl 4 fourfour2@gmail.com
    Re: XML::Parser Tree Style <1usa@llenroc.ude.invalid>
    Re: XML::Parser Tree Style xhoster@gmail.com
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 03 Jun 2008 11:08:30 -0400
From: Mike Hunter <mhunter@lusArs.net>
Subject: advice on good perl idiom
Message-Id: <slrng4anje.lun.mhunter@fortytwo.lusars.net>

Hey folks,

Recently I was coding and wanted to see which of 4 strings had the
largest result when passed to a function.  This is what I ened up
writing:

while (...)
{
   
    my @roundArr;
    push @roundArr, [$A, check($goodPrefix.$A)];
    push @roundArr, [$C, check($goodPrefix.$C)];
    push @roundArr, [$T, check($goodPrefix.$T)];
    push @roundArr, [$G, check($goodPrefix.$G)];

    @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
    print $roundArr[0]->[0]." wins!\n";

}

The code works fine, but I can't help but think there's a better idiom
for what I'm trying to do.  I get a little bit of a creepy feeling by
using an array, I feel like I should be doing something with a hash.  I
guess there's no way of getting around having to associate the given
check() result with the particular input.

Any thoughts on some awesome one-liner that I can't see?

Thanks,

Mike


------------------------------

Date: Tue, 03 Jun 2008 15:53:06 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: advice on good perl idiom
Message-Id: <Xns9AB278E438662asu1cornelledu@127.0.0.1>

Mike Hunter <mhunter@lusArs.net> wrote in
news:slrng4anje.lun.mhunter@fortytwo.lusars.net: 

> Recently I was coding and wanted to see which of 4 strings had the
> largest result when passed to a function.  This is what I ened up
> writing:
> 
> while (...)
> {
>    
>     my @roundArr;
>     push @roundArr, [$A, check($goodPrefix.$A)];
>     push @roundArr, [$C, check($goodPrefix.$C)];
>     push @roundArr, [$T, check($goodPrefix.$T)];
>     push @roundArr, [$G, check($goodPrefix.$G)];
> 
>     @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
>     print $roundArr[0]->[0]." wins!\n";
> 
> }

I don't know about idioms but the question is do you need to store the
results to the check call? If not: 

#!/usr/bin/perl

my $goodPrefix = 'prefix';
my ( $A, $C, $T, $G ) = ('a', 'cc', 'ttt', 'gg' );

my $winner = $A;
my $winner_score = check( $goodPrefix.$A );

for my $s ( $C, $T, $G ) {
    my $score = check( $goodPrefix.$s );
    if ( $score > $winner_score ) {
        $winner = $s;
        $winner_score = $score;
    }
}

print "$winner\t$winner_score\n";

sub check { length shift }

__END__

If you absolutely must use a hash:

#!/usr/bin/perl

use strict;
use warnings;

my $goodPrefix = 'prefix';

my ( $A, $C, $T, $G ) = ('a', 'cc', 'ttt', 'gg' );

my %scores = map { $_ => check( $goodPrefix.$_ ) } ( $A, $C, $T, $G );

my $winner = (sort { $scores{$b} <=> $scores{$a} } keys %scores)[0];

print "$winner\t$scores{$winner}\n";

sub check { length shift }

__END__

> Any thoughts on some awesome one-liner that I can't see?

If you have a large number of elements, sort is going to do far more 
comparisons than simply walking the elements.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


------------------------------

Date: Tue, 03 Jun 2008 15:59:09 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: advice on good perl idiom
Message-Id: <tfpa44hr9p2l6sd90lphh7hv74i12ct12s@4ax.com>

Mike Hunter <mhunter@lusArs.net> wrote:
>Hey folks,
>
>Recently I was coding and wanted to see which of 4 strings had the
>largest result when passed to a function.  This is what I ened up
>writing:
>
>while (...)
>{
>   
>    my @roundArr;
>    push @roundArr, [$A, check($goodPrefix.$A)];
>    push @roundArr, [$C, check($goodPrefix.$C)];
>    push @roundArr, [$T, check($goodPrefix.$T)];
>    push @roundArr, [$G, check($goodPrefix.$G)];

Ouch! Replace the preceeding 4 lines with
	for ($A, $C, $T, $G) {
		 push @roundArr, [$_, check($goodPrefix.$_)];
	}

>
>    @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
>    print $roundArr[0]->[0]." wins!\n";
>
>}
>
>The code works fine, but I can't help but think there's a better idiom
>for what I'm trying to do. 

Conceptually you just loop through the data set and remember, which
element produced the best result so far:
	$best = $a; #just as the default start value
	for ($b, $c, $d) {
		$best = $_ if check($_) > check($best)
	} 

However this has some redundancies like computing $best over and over
again. Therefore you want to store that result in an auxiliary variable
$bestV:

	$best = $a; #just as the default start value
	$bestV = check ($a);
	for ($b, $c, $d) {
		if (($tmp = check($_)) > $bestV) {
			$best = $_;
			$bestV = $tmp;
		}
	}

>I get a little bit of a creepy feeling by
>using an array, I feel like I should be doing something with a hash.  I
>guess there's no way of getting around having to associate the given
>check() result with the particular input.

The point is that you don't need to keep all of the temporary results
around, neither in a hash nor in an array. All you need to store is the
currently best candidate and for optimization its value.

jue


------------------------------

Date: Tue, 3 Jun 2008 17:20:08 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: advice on good perl idiom
Message-Id: <oi7gh5-8p7.ln1@osiris.mauzo.dyndns.org>


Quoth "A. Sinan Unur" <1usa@llenroc.ude.invalid>:
> 
> If you absolutely must use a hash:
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $goodPrefix = 'prefix';
> 
> my ( $A, $C, $T, $G ) = ('a', 'cc', 'ttt', 'gg' );
> 
> my %scores = map { $_ => check( $goodPrefix.$_ ) } ( $A, $C, $T, $G );
> 
> my $winner = (sort { $scores{$b} <=> $scores{$a} } keys %scores)[0];
> 
> print "$winner\t$scores{$winner}\n";
> 
> sub check { length shift }

The hash needs to be the other way round:

    use List::Util qw/max/;

    my %scores = map { check("$goodPrefix$_") => $_ } qw/a cc ttt gg/;
    my $winner = max keys %scores;
    print "$scores{$winner}\t$winner\n";

If there is a tie this will pick the last from your original list.

Ben

-- 
BEGIN{*(=sub{$,=*)=sub{print@_};local($#,$;,$/)=@_;for(keys%{ #ben@morrow.me.uk
$#}){/m/&&next;**=${$#}{$_};/(\w):/&&(&(($#.$_,$;.$+,$/),next);$/==\$*&&&)($;.$
_)}};*_=sub{for(@_){$|=(!$|||$_||&)(q) )));&((q:\:\::,q,,,\$_);$_&&&)("\n")}}}_
$J::u::s::t, $a::n::o::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $,


------------------------------

Date: Tue, 03 Jun 2008 18:55:22 +0200
From: Mirco Wahab <wahab@chemie.uni-halle.de>
Subject: Re: advice on good perl idiom
Message-Id: <g23t1q$1iqf$1@nserver.hrz.tu-freiberg.de>

Mike Hunter wrote:
> Recently I was coding and wanted to see which of 4 strings had the
> largest result when passed to a function.  This is what I ened up
> writing:
>     my @roundArr;
>     push @roundArr, [$A, check($goodPrefix.$A)];
>     push @roundArr, [$C, check($goodPrefix.$C)];
>     push @roundArr, [$T, check($goodPrefix.$T)];
>     push @roundArr, [$G, check($goodPrefix.$G)];
> 
>     @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
>     print $roundArr[0]->[0]." wins!\n";
> The code works fine, but I can't help but think there's a better idiom
> for what I'm trying to do.  I get a little bit of a creepy feeling by
> using an array, I feel like I should be doing something with a hash.  I
> guess there's no way of getting around having to associate the given
> check() result with the particular input.

This can be expressed as a "Schwartzian Transform":

     ...

     print +(
               map  $_->[0],
               sort {$b->[1] <=> $a->[1]}
               map  [$_, check($goodPrefix.$_)],
               $A,$C,$T,$G
           )[0] . " wins!\n"


     ...


Regards

Mirco


------------------------------

Date: Tue, 03 Jun 2008 15:28:47 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Counting lines in big number of files - in parallel.
Message-Id: <2boa445828e94uojpgmkriiiu9m4qb64lv@4ax.com>

hadzio@gmail.com wrote:
>I have the following issue. I have a directory with 25000 text files
>in it (about 1-10000 lines in each file). I have a perl script that
>generates some reports for me and this script needs to count the
>number of lines in each of these 25000 files 

>		open (WCCOUNT, "cat $file_to_read | wc -l |");

Instead of shelling out three new processes you could use the method,
that is recommended in the FAQ. Don't know if it will be faster, but at
least it will not start new processes which usually is expensive.

>But the above sequencial counting is very slow (2-3 hours). My server
>is quite powerfull (72 CPU and fast filesystems) 

I don't know what a 'fast filesystem' is, but whatever method you are
going to use it will be I/O bound unless you have something like a RAID5
or similar system with strongly distributed data over numerous physical
HDDs.

>so I would like to
>run the counting in parallel (eg. counting in 72 files at the same
>time). So the questions are:
>1) Is it possible to run the above command (cat ... | wc -l) in
>background & (the same way as in shell) and receive the returned
>results when it is finished.

Sure.

>2) Is it possible to implement 1) without threads?

Of course. Just fork() whatever additional processes you need.

>3) The above code I may write using system() instead of open(), but
>the same issue is: how to do it in parallel.

Just fork() new processes.

Personally I don't think it will help to speed up the counting unless
your files are distributed over numerous HDDs.

jue


------------------------------

Date: 03 Jun 2008 16:00:59 GMT
From: xhoster@gmail.com
Subject: Re: Counting lines in big number of files - in parallel.
Message-Id: <20080603120100.952$CK@newsreader.com>

hadzio@gmail.com wrote:
> Hi,
>
> I have the following issue. I have a directory with 25000 text files
> in it (about 1-10000 lines in each file).

How big are the files?  Every character needs to be inspected to see if it
is a newline, so it depends on the number of characters, not the number
of lines.

> I have a perl script that
> generates some reports for me and this script needs to count the
> number of lines in each of these 25000 files (for each file I need a
> number of lines in it). And it is not so difficult, I iterate over the
> directory and count the number of lines using "wc -l" as follows:
>
>                 open (WCCOUNT, "cat $file_to_read | wc -l |");
>                 $file_number_of_lines = <WCCOUNT>;
>                 chomp($file_number_of_lines);
>                 close(WCCOUNT);
>
> But the above sequencial counting is very slow (2-3 hours). My server
> is quite powerfull (72 CPU and fast filesystems)

Unless your line lengths are > 1000 or so, this seems quite slow.  Maybe
your file system isn't as fast you think it is.  Can you verify the time
it would take to read all of this data independent of both Perl and wc
(e.g. catting it all to /dev/null).  I think that should be sorted out
before trying to go for parallelization.  If you have IO problems,
parallelization probably wouldn't help

Also, what kind of CPUs do you have?

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


------------------------------

Date: Tue, 3 Jun 2008 10:58:17 -0700 (PDT)
From: April <xiaoxia2005a@yahoo.com>
Subject: formatting question
Message-Id: <b9ab1a51-18cd-432a-a718-aa67076dc6dc@34g2000hsf.googlegroups.com>

sprintf( "%s%$Fmt%s", ("%$Fmt=|", $TestStr, "|"))

This is in Perl for Dummies, 4th ed, p160.

I'm trying to understand this ...

the first part, "%s%$Fmt%s", my understanding is the format part,
which specifies the formats for the second part, thelist part, ("%
$Fmt=|", $TestStr, "|"): %s for "%$Fmt=|", %$Fmt for $TestStr, and %s
for "|", respectively.  Is this correct?

Then what is %$Fmt, it seems a % for format and then a variable $Fmt,
the book did not mention any format string like this ...

Anyone can shed some light?  Thanks!


------------------------------

Date: Tue, 03 Jun 2008 15:19:36 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: HTML::Tokerparser / parsing <p>,<br>
Message-Id: <Xns9AB2733669387asu1cornelledu@127.0.0.1>

Sharif <mislam@nospam.ciuc.edu> wrote in
news:g23j8e$pog$2@news.acm.uiuc.edu: 

> Gunnar Hjalmarsson wrote:
>> Sharif wrote:
>>> I am using the Tokeparser module for a html file that contains movie
>>> title, description, library call number, year, duration etc. But the
>>> html files are marked so I have to rely on the the paragraph and
>>> line break.
>> 
>> There needs to be something else that identifies the start of a
>> movie, I think, or else I doubt that HTML::TokeParser is a suitable
>> tool for the task.
>> 
>> Do you have a URL?
>> 
> 
> http://www.afrst.uiuc.edu/libvideos1.htm
> 
> There's a number if front of the title, that's the only identifier.

Note that the script below fails for some records which have incomplete 
information.

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use LWP::Simple;
use HTML::TokeParser;

my $url = 'http://www.afrst.uiuc.edu/libvideos1.htm';
my $htmlfile = 'libvideos1.htm';

unless ( -e $htmlfile ) {
    my $rc = getstore $url => $htmlfile;
    unless ( $rc == 200 ) {
        die "Could not store '$url' in '$htmlfile'\n";
    }
}

my $parser = HTML::TokeParser->new( $htmlfile );
unless ( $parser ) {
    die "Cannot open '$htmlfile': $!";
}

my @movies;

while ( $parser->get_tag('p') ) {
  my $text = $parser->get_text('p');
  next unless $text =~ /^\d+- /;
  my @meta = split /\s+\n\s+/, $text;

  my %movie;
  @movie{ qw( id title )     } = ( $meta[0] =~ /^(\d+)- (.+)$/ );
  @movie{ qw( place length ) } = ( $meta[1] =~ /^(.+), (\d+) minutes$/);
  @movie{ qw( company year ) } = ( $meta[2] =~ /^(.+), (\d{4})$/ );
  @movie{ qw( source )       } = $meta[3];

  if ( $parser->get_tag('p') ) {
     $movie{description} = $parser->get_trimmed_text('p');
  }

  push @movies, \%movie;
}

print Dumper \@movies;

__END__


-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


------------------------------

Date: Tue, 03 Jun 2008 11:18:28 -0500
From: Sharif <mislam@nospam.ciuc.edu>
Subject: Re: HTML::Tokerparser / parsing <p>,<br>
Message-Id: <g23qsj$rv9$1@news.acm.uiuc.edu>

A. Sinan Unur wrote:
> Sharif <mislam@nospam.ciuc.edu> wrote in
> news:g23j8e$pog$2@news.acm.uiuc.edu: 
> 
>> Gunnar Hjalmarsson wrote:
>>> Sharif wrote:
>>>> I am using the Tokeparser module for a html file that contains movie
>>>> title, description, library call number, year, duration etc. But the
>>>> html files are marked so I have to rely on the the paragraph and
>>>> line break.
>>> There needs to be something else that identifies the start of a
>>> movie, I think, or else I doubt that HTML::TokeParser is a suitable
>>> tool for the task.
>>>
>>> Do you have a URL?
>>>
>> http://www.afrst.uiuc.edu/libvideos1.htm
>>
>> There's a number if front of the title, that's the only identifier.
> 
> Note that the script below fails for some records which have incomplete 
> information.
> 
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> use Data::Dumper;
> use LWP::Simple;
> use HTML::TokeParser;
> 
> my $url = 'http://www.afrst.uiuc.edu/libvideos1.htm';
> my $htmlfile = 'libvideos1.htm';
> 
> unless ( -e $htmlfile ) {
>     my $rc = getstore $url => $htmlfile;
>     unless ( $rc == 200 ) {
>         die "Could not store '$url' in '$htmlfile'\n";
>     }
> }
> 
> my $parser = HTML::TokeParser->new( $htmlfile );
> unless ( $parser ) {
>     die "Cannot open '$htmlfile': $!";
> }
> 
> my @movies;
> 
> while ( $parser->get_tag('p') ) {
>   my $text = $parser->get_text('p');
>   next unless $text =~ /^\d+- /;
>   my @meta = split /\s+\n\s+/, $text;
> 
>   my %movie;
>   @movie{ qw( id title )     } = ( $meta[0] =~ /^(\d+)- (.+)$/ );
>   @movie{ qw( place length ) } = ( $meta[1] =~ /^(.+), (\d+) minutes$/);
>   @movie{ qw( company year ) } = ( $meta[2] =~ /^(.+), (\d{4})$/ );
>   @movie{ qw( source )       } = $meta[3];
> 
>   if ( $parser->get_tag('p') ) {
>      $movie{description} = $parser->get_trimmed_text('p');
>   }
> 
>   push @movies, \%movie;
> }
> 
> print Dumper \@movies;
> 
> __END__
> 
> 
Thanks, that was really helpful!

--s


------------------------------

Date: Tue, 3 Jun 2008 17:53:42 +0000 (UTC)
From: toralf <toralf.foerster@gmx.de>
Subject: MD5 and SHA1 fingerprint from SSL certificates
Message-Id: <g240f6$a3i$1@registered.motzarella.org>

I'm wondering,

who I can derive these 2 values from my local https daemon.
Currently I try it w/ IO::Socket::SSL, but do not know how to continue :

tfoerste@n22 ~ $ perl -we 'use IO::Socket::SSL; my $client = IO::Socket::SSL->new("n22:https"); print $client->dump_peer_certificate(), $client->get_cipher(), "\n", $client->peer_certificate(), "\n";'
Subject Name: /C=DE/ST=HAmburg/L=Hamburg/O=Apache HTTP Server/OU=Test Certificate/CN=n22/emailAddress=root@n22
Issuer  Name: /C=DE/ST=Hamburg/L=Hamburg/O=Apache HTTP Server/OU=For testing purposes only/CN=n22/emailAddress=root@localhost
DHE-RSA-AES256-SHA
137497664

-- 
MfG/Sincerely

Toralf Förster
pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3



------------------------------

Date: Tue, 3 Jun 2008 09:50:12 -0700 (PDT)
From: fourfour2@gmail.com
Subject: Re: Perl grep and Perl 4
Message-Id: <bf124f95-a27d-43aa-8076-11e9bb23bb9b@q27g2000prf.googlegroups.com>

On Jun 3, 12:59=A0am, Frank Seitz <devnull4...@web.de> wrote:
> fourfo...@gmail.com wrote:
>
> >>>>syntax error,next 2 tokens :grep {"
> [...]
> > #this works in perl 5, not perl 4
> > $potatoe=3D"thisisapotatoe(one)";
> > @listofpotatoes=3D("thisisapotatoe(one)", "thisisanoldpottit");
>
> > if ( !grep { $potatoe eq $_ } @listofpotatoes) {
> > =A0 =A0print "Not found in list....\n";
> > }
>
> The error message says that the block syntax is not
> allowed in Perl 4. Use grep(EXPR,LIST) instead.
>
> Frank
> --
> Dipl.-Inform. Frank Seitz;http://www.fseitz.de/
> Anwendungen f=FCr Ihr Internet und Intranet
> Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel


Since I got special characters in the string,  grep(EXPR,LIST) doesn't
handle these.

if (!grep(/^$potatoe$/, @listofpotatoes)) {
  print "$potatoe is not in list";
}

This prints: potatoe is not in list
But it is.
I think it's b/c of the parenthesis in the string.

Any suggestions?

Thanks for all your answers.


------------------------------

Date: Tue, 03 Jun 2008 17:01:57 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Perl grep and Perl 4
Message-Id: <Xns9AB2849077CCAasu1cornelledu@127.0.0.1>

fourfour2@gmail.com wrote in news:bf124f95-a27d-43aa-8076-
11e9bb23bb9b@q27g2000prf.googlegroups.com:

> if (!grep(/^$potatoe$/, @listofpotatoes)) {
>   print "$potatoe is not in list";

Could we please drop the Dan Quayle act?

http://en.wikipedia.org/wiki/Potatoe#Spelling

>> Frank
>> --
>> Dipl.-Inform. Frank Seitz;http://www.fseitz.de/
 ...

And please do *NOT* quote sigs.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


------------------------------

Date: Tue, 03 Jun 2008 12:06:24 -0500
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Perl grep and Perl 4
Message-Id: <48457a10$0$33221$815e3792@news.qwest.net>

fourfour2@gmail.com wrote:
> On Jun 3, 12:59 am, Frank Seitz <devnull4...@web.de> wrote:
>> fourfo...@gmail.com wrote:
>>
>>>>>> syntax error,next 2 tokens :grep {"
>> [...]
>>> #this works in perl 5, not perl 4
>>> $potatoe="thisisapotatoe(one)";
[...]
> Since I got special characters in the string,  grep(EXPR,LIST) doesn't
> handle these.
> 
> if (!grep(/^$potatoe$/, @listofpotatoes)) {
>   print "$potatoe is not in list";
> }
> 
> This prints: potatoe is not in list

Really?  I don't see how it's possible that it printed 'potatoe' there
when the value is 'thisisapotatoe(one)'. Maybe it's a Dan Quayle bug.


> But it is.
> I think it's b/c of the parenthesis in the string.
> 
> Any suggestions?

Ahhh.. You can always use a 'for' loop.


------------------------------

Date: Tue, 3 Jun 2008 10:13:17 -0700 (PDT)
From: fourfour2@gmail.com
Subject: Re: Perl grep and Perl 4
Message-Id: <ddbc191e-dc54-45e8-9845-89657e1c9b95@u6g2000prc.googlegroups.com>

On Jun 3, 10:06=A0am, "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
wrote:
> fourfour2@gmail.com wrote:
> > On Jun 3, 12:59 am, Frank Seitz <devnull4...@web.de> wrote:
> >> fourfo...@gmail.com wrote:
>
> >>>>>> syntax error,next 2 tokens :grep {"
> >> [...]
> >>> #this works in perl 5, not perl 4
> >>> $potatoe=3D"thisisapotatoe(one)";
> [...]
> > Since I got special characters in the string, =A0grep(EXPR,LIST) doesn't=

> > handle these.
>
> > if (!grep(/^$potatoe$/, @listofpotatoes)) {
> > =A0 print "$potatoe is not in list";
> > }
>
> > This prints: potatoe is not in list
>
> Really? =A0I don't see how it's possible that it printed 'potatoe' there
> when the value is 'thisisapotatoe(one)'. Maybe it's a Dan Quayle bug.

You're right. Sorry.

>
> > But it is.
> > I think it's b/c of the parenthesis in the string.
>
> > Any suggestions?
>
> Ahhh.. You can always use a 'for' loop.- Hide quoted text -

No. Don't want that.
Thanks.

> - Show quoted text -



------------------------------

Date: 3 Jun 2008 17:25:11 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: Perl grep and Perl 4
Message-Id: <slrng4avjo.1tn.glennj@smeagol.ncf.ca>

At 2008-06-03 12:50PM, "fourfour2@gmail.com" wrote:
>  On Jun 3, 12:59 am, Frank Seitz <devnull4...@web.de> wrote:
> > fourfo...@gmail.com wrote:
> >
> > >>>>syntax error,next 2 tokens :grep {"
> > [...]
> > > #this works in perl 5, not perl 4
> > > $potatoe="thisisapotatoe(one)";
> > > @listofpotatoes=("thisisapotatoe(one)", "thisisanoldpottit");
> >
> > > if ( !grep { $potatoe eq $_ } @listofpotatoes) {
> > >    print "Not found in list....\n";
> > > }
> >
> > The error message says that the block syntax is not
> > allowed in Perl 4. Use grep(EXPR,LIST) instead.
>  
>  Since I got special characters in the string,  grep(EXPR,LIST) doesn't
>  handle these.
>  
>  if (!grep(/^$potatoe$/, @listofpotatoes)) {
>    print "$potatoe is not in list";
>  }

see: perldoc -f quotemeta

    print "in list" if grep(/^\Q$potatoe\E$/, @listofpotatoes);

or, since you're just testing for equality:

    print "in list" if grep($_ eq $potatoe, @listofpotatoes)

(tested in perl 5.8.8)

-- 
Glenn Jackman
  "If there is anything the nonconformist hates worse than a conformist, 
   it's another nonconformist who doesn't conform to the prevailing 
   standard of nonconformity." -- Bill Vaughan 


------------------------------

Date: Tue, 3 Jun 2008 10:33:33 -0700 (PDT)
From: fourfour2@gmail.com
Subject: Re: Perl grep and Perl 4
Message-Id: <38d1a01b-c1cd-43ff-bb30-7b5d0a28159a@q24g2000prf.googlegroups.com>

On Jun 3, 10:25=A0am, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2008-06-03 12:50PM, "fourfo...@gmail.com" wrote:
>
>
>
>
>
> > =A0On Jun 3, 12:59=A0am, Frank Seitz <devnull4...@web.de> wrote:
> > > fourfo...@gmail.com wrote:
>
> > > >>>>syntax error,next 2 tokens :grep {"
> > > [...]
> > > > #this works in perl 5, not perl 4
> > > > $potatoe=3D"thisisapotatoe(one)";
> > > > @listofpotatoes=3D("thisisapotatoe(one)", "thisisanoldpottit");
>
> > > > if ( !grep { $potatoe eq $_ } @listofpotatoes) {
> > > > =A0 =A0print "Not found in list....\n";
> > > > }
>
> > > The error message says that the block syntax is not
> > > allowed in Perl 4. Use grep(EXPR,LIST) instead.
>
> > =A0Since I got special characters in the string, =A0grep(EXPR,LIST) does=
n't
> > =A0handle these.
>
> > =A0if (!grep(/^$potatoe$/, @listofpotatoes)) {
> > =A0 =A0print "$potatoe is not in list";
> > =A0}
>
> see: perldoc -f quotemeta
>
> =A0 =A0 print "in list" if grep(/^\Q$potatoe\E$/, @listofpotatoes);
>
> or, since you're just testing for equality:
>
> =A0 =A0 print "in list" if grep($_ eq $potatoe, @listofpotatoes)
>
> (tested in perl 5.8.8)
>
> --
> Glenn Jackman
> =A0 "If there is anything the nonconformist hates worse than a conformist,=

> =A0 =A0it's another nonconformist who doesn't conform to the prevailing
> =A0 =A0standard of nonconformity." -- Bill Vaughan- Hide quoted text -
>
> - Show quoted text -

Yep - this works when using Perl 5.
Unfortunately not in Perl 4.035 though.

Thanks


------------------------------

Date: Tue, 03 Jun 2008 15:38:17 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: XML::Parser Tree Style
Message-Id: <Xns9AB27660FF888asu1cornelledu@127.0.0.1>

NiallBCarter <NiallBCarter@googlemail.com> wrote in
news:0a1c6382-4c2b-400a-afc0-9468a1413cf2@z66g2000hsc.googlegroups.com: 

> I get that $stuff and $tree are not arrays but rather refs to arrays
> (right?) and fair enough. I get what's going on and seem to do well so
> far. Until I get to:
> $stuff =~ s/\s//g;

$stuff holds the string produced by Dumper. The line above gets rid of 
all the spaces in that string.

> and:
> 
>> while ($stuff =~/firstname.*?(\w+)'\]/g) {

Matches stuff against a carefully constructed pattern that ends with the 
string q{']} preceeded by a bunch of 'word' characters. 

The .*? before (\w+) skips all the characters that come before the 
actual name. The parantheses around \w+ capture the sequence of word 
characters before the closing q{']}.

>>     print "$1\n";

Prints the captured string.

Clever, but confusing. Also, it is subject to the same pitfall that the 
whole XML document has to be replicated in memory twice: Once in tree 
form and once as the string representation of the tree form.

> Within my while loop there can I not just place the value in an array
> so that I can take that array off to another part of my perl script
> and make something of it?

It would require only a small modification to save all the names in an 
array. You ought to be able to figure out the required modification.

IMHO, you are still better off using the 'Stream' style if all you want 
are the contents of a single type of tag.

As for your KML etc stuff, I am not going to comment on that as there is 
not enough information to go on. I would have to make too many 
assumptions.

Sinan

-- 
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/


------------------------------

Date: 03 Jun 2008 16:15:51 GMT
From: xhoster@gmail.com
Subject: Re: XML::Parser Tree Style
Message-Id: <20080603121553.454$vc@newsreader.com>

NiallBCarter <NiallBCarter@googlemail.com> wrote:
> On Jun 3, 11:03 am, Frank Seitz <devnull4...@web.de> wrote:
> > NiallBCarter wrote:
> >
> > > The $tree - is it an array? a scalar? a variable? little confused
> > > there you see. If it is a scalar, how do I get access to it?
> >
> > Did you read the doc?
> >
> > http://search.cpan.org/~msergeant/XML-Parser-2.36/Parser.pm#Tree
>
> Many many times. So the tree parse is an array reference but does not
> describe how to get the information out of it.

It describes what the format of the data is.  It assumes you know how to
deal with it from there, as from there it is just a Perl data structure,
and the XML::Parser docs are a supplement to, not a replacement for, the
Perl docs.  You get the information out of it the way you get it out of any
complex Perl data structure.  That is documented in the perldocs for
perlref, perlreftut, perldsc, etc.  Doing this is not very easy.  That is
why there are modules that sit on top of XML::Parser, such as XML::Simple,
whose goal is to make it easier.


> I am very surprised by
> the lack of tutorials about this. Many of them use handlers and event
> (or stream) processing which I clearly want to stay away from.
> I have previously used XML::Simple and thought that I could use:
> my $var =3D $tree->{person}->{firstname}
> but that (i think I am correct in saying) is how you access a Hash?

Why don't you continue to use XML::Simple?

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 1607
***************************************


home help back first fref pref prev next nref lref last post