[28974] in Perl-Users-Digest
Perl-Users Digest, Issue: 218 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 12 21:09:57 2007
Date: Mon, 12 Mar 2007 18:09:08 -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 Mon, 12 Mar 2007 Volume: 11 Number: 218
Today's topics:
adding a data field to a subclass of HTML::Parser sinoslav@gmail.com
Re: adding a data field to a subclass of HTML::Parser <attn.steven.kuo@gmail.com>
Re: adding a data field to a subclass of HTML::Parser anno4000@radom.zrz.tu-berlin.de
Re: adding a data field to a subclass of HTML::Parser <attn.steven.kuo@gmail.com>
Re: adding a data field to a subclass of HTML::Parser <bik.mido@tiscalinet.it>
Re: Convert hex to bin <wahab-mail@gmx.de>
Re: Convert hex to bin <wahab-mail@gmx.de>
Crash in Perl_stashpv_hvname_match <markbuxbaum@hotmail.com>
Re: Crash in Perl_stashpv_hvname_match <jurgenex@hotmail.com>
Re: Find Missing Column and Extra Column <news@lawshouse.org>
Re: Find Missing Column and Extra Column <tadmc@augustmail.com>
Re: Graphing with Perl <jgibson@mail.arc.nasa.gov>
Llama book exercise <persep@gmail.com>
Re: Llama book exercise anno4000@radom.zrz.tu-berlin.de
Re: Llama book exercise <brian.d.foy@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 12 Mar 2007 12:06:30 -0700
From: sinoslav@gmail.com
Subject: adding a data field to a subclass of HTML::Parser
Message-Id: <1173726390.452553.193470@64g2000cwx.googlegroups.com>
I'm pretty new to Perl's object system and am trying to figure out how
to add a data field to my subclass of HTML::Parser so that the
subclass can collect information into that data field as it parses an
HTML document. For example, let's say I want to collect an array
@names of all the NAME attributes of my <A> tags, and add a method
that returns that array:
{
Package MyParser;
use base HTML::Parser;
sub start {
my ($self, $tag, $attr, $attrseq, $origtext) = @_;
if($tag eq "a") {
push @names, $attr->{name};
}
}
sub get_names {
return \@names;
}
...
}
How can I declare the @names array as a field of MyParser?
Many thanks!
Roger
------------------------------
Date: 12 Mar 2007 13:18:54 -0700
From: "attn.steven.kuo@gmail.com" <attn.steven.kuo@gmail.com>
Subject: Re: adding a data field to a subclass of HTML::Parser
Message-Id: <1173730734.393983.164060@v33g2000cwv.googlegroups.com>
On Mar 12, 12:06 pm, sinos...@gmail.com wrote:
> I'm pretty new to Perl's object system and am trying to figure out how
> to add a data field to my subclass of HTML::Parser so that the
> subclass can collect information into that data field as it parses an
> HTML document. For example, let's say I want to collect an array
> @names of all the NAME attributes of my <A> tags, and add a method
> that returns that array:
>
> {
> Package MyParser;
> use base HTML::Parser;
>
> sub start {
> my ($self, $tag, $attr, $attrseq, $origtext) = @_;
>
> if($tag eq "a") {
> push @names, $attr->{name};
> }
> }
>
> sub get_names {
> return \@names;
> }
> ...
>
> }
>
> How can I declare the @names array as a field of MyParser?
>
This follows the "InsideOut" approach:
package MyParser;
use base HTML::Parser;
use Scalar::Util qw/refaddr/;
my %names;
sub start {
my ($self, $tag, $attr, $attrseq, $origtext) = @_;
if ($tag eq 'a')
{
push @{$names{refaddr $self}}, $attr->{name};
}
$self->SUPER::start(@_);
}
sub get_names {
my $self = shift;
return $names{refaddr $self};
}
sub DESTROY {
my $self = shift;
delete $names{ refaddr $self };
}
1; # not thread-safe
package main;
use Data::Dumper;
my $p = MyParser->new();
$p->parse_file(*DATA);
my $aref = $p->get_names;
print Dumper $aref;
__DATA__
This is <a href="http://example.com" name="testing">a test</a>
--
Hope this helps,
Steven
------------------------------
Date: 12 Mar 2007 21:51:39 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: adding a data field to a subclass of HTML::Parser
Message-Id: <55m0bbF25j66sU1@mid.dfncis.de>
attn.steven.kuo@gmail.com <attn.steven.kuo@gmail.com> wrote in comp.lang.perl.misc:
> On Mar 12, 12:06 pm, sinos...@gmail.com wrote:
> > I'm pretty new to Perl's object system and am trying to figure out how
> > to add a data field to my subclass of HTML::Parser so that the
> > subclass can collect information into that data field as it parses an
> > HTML document. For example, let's say I want to collect an array
> > @names of all the NAME attributes of my <A> tags, and add a method
> > that returns that array:
> >
> > {
> > Package MyParser;
> > use base HTML::Parser;
> >
> > sub start {
> > my ($self, $tag, $attr, $attrseq, $origtext) = @_;
> >
> > if($tag eq "a") {
> > push @names, $attr->{name};
> > }
> > }
> >
> > sub get_names {
> > return \@names;
> > }
> > ...
> >
> > }
> >
> > How can I declare the @names array as a field of MyParser?
> >
>
>
> This follows the "InsideOut" approach:
Yes! It's the only way a class can painlessly add fields to
an existing class. I'd like to point out that your code takes
absolutely no note of the implementation of the base class
HTML::Parser.
With Perl 5.10 (and current bleadperl) you could declare your
hash %names a "FieldHash". That makes the hash garbage-collected
and thread-safe, so the DESTROY method could go, and the comment
at the end of your code too. As a minor convenience, you use
objects as hash keys directly. The refaddr()-action is built in.
I have indicated how your class could be built using
Hash::Util::FieldHash.
> package MyParser;
> use base HTML::Parser;
> use Scalar::Util qw/refaddr/;
Not needed. Instead
use Hash::Util::FieldHash;
> my %names;
Hash::Util::FieldHash::fieldhash my %names;
> sub start {
> my ($self, $tag, $attr, $attrseq, $origtext) = @_;
> if ($tag eq 'a')
> {
> push @{$names{refaddr $self}}, $attr->{name};
push @{$names{ $self}}, $attr->{name};
> }
> $self->SUPER::start(@_);
> }
>
> sub get_names {
> my $self = shift;
> return $names{refaddr $self};
return $names{ $self};
> }
>
> sub DESTROY {
> my $self = shift;
> delete $names{ refaddr $self };
> }
DESTROY is not needed.
> 1; # not thread-safe
The comment neither :)
> package main;
> use Data::Dumper;
>
> my $p = MyParser->new();
> $p->parse_file(*DATA);
> my $aref = $p->get_names;
> print Dumper $aref;
Nothing changes for the user of the class.
Anno
------------------------------
Date: 12 Mar 2007 15:43:38 -0700
From: "attn.steven.kuo@gmail.com" <attn.steven.kuo@gmail.com>
Subject: Re: adding a data field to a subclass of HTML::Parser
Message-Id: <1173739418.545477.248550@30g2000cwc.googlegroups.com>
On Mar 12, 2:51 pm, anno4...@radom.zrz.tu-berlin.de wrote:
> attn.steven....@gmail.com <attn.steven....@gmail.com> wrote in comp.lang.perl.misc:
>
>
>
> > On Mar 12, 12:06 pm, sinos...@gmail.com wrote:
> > > I'm pretty new to Perl's object system and am trying to figure out how
> > > to add a data field to my subclass of HTML::Parser so that the
> > > subclass can collect information into that data field as it parses an
> > > HTML document. For example, let's say I want to collect an array
> > > @names of all the NAME attributes of my <A> tags, and add a method
> > > that returns that array:
>
> > > {
> > > Package MyParser;
> > > use base HTML::Parser;
>
> > > sub start {
> > > my ($self, $tag, $attr, $attrseq, $origtext) = @_;
>
> > > if($tag eq "a") {
> > > push @names, $attr->{name};
> > > }
> > > }
>
> > > sub get_names {
> > > return \@names;
> > > }
> > > ...
>
> > > }
>
> > > How can I declare the @names array as a field of MyParser?
>
> > This follows the "InsideOut" approach:
>
> Yes! It's the only way a class can painlessly add fields to
> an existing class. I'd like to point out that your code takes
> absolutely no note of the implementation of the base class
> HTML::Parser.
>
> With Perl 5.10 (and current bleadperl) you could declare your
> hash %names a "FieldHash". That makes the hash garbage-collected
> and thread-safe, so the DESTROY method could go, and the comment
> at the end of your code too. As a minor convenience, you use
> objects as hash keys directly. The refaddr()-action is built in.
>
> I have indicated how your class could be built using
> Hash::Util::FieldHash.
(snipped)
Thanks for the tip, Anno. It's definitely time for me to upgrade:
I've been stuck on version 5.8.7 due to my own laziness (the bad
kind)
and time spent learning another programming language that begins with
"p"
(a requirement at work).
--
Regards,
Steven
------------------------------
Date: Tue, 13 Mar 2007 00:04:33 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: adding a data field to a subclass of HTML::Parser
Message-Id: <6vmbv216n3o16so4equ5e2egmds8jaq878@4ax.com>
On 12 Mar 2007 21:51:39 GMT, anno4000@radom.zrz.tu-berlin.de wrote:
> use Hash::Util::FieldHash;
[snip]
>> my %names;
>
> Hash::Util::FieldHash::fieldhash my %names;
This is all very cool, I only wonder whether it could be made into a
syntactically sweeter form. For example I'm not familiar with
attributes at all, so I don't know if this is plainly utter nonsense,
but how about something like
my %names : field; # ?
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
------------------------------
Date: Mon, 12 Mar 2007 18:04:46 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Convert hex to bin
Message-Id: <et4fig$sf6$3@mlucom4.urz.uni-halle.de>
Klaus Sulzberger wrote:
> sub hex2bin
> {
> my $str=unpack("B32",pack("N",hex$_[0]));
> $str=~s/^0+(?=\d)//;
> return($str);
> }
>
> The problem of the function is, that the result cuts the zeros.
> For examples:
> Hex: 7D
> Function result: 1111101
> Want to have: 01111101
> (for 1 Byte)
Aside from the fun part, you
can of course 'hand craft' such
a functionality *without any*
of Perl's appropriate builtins,
like:
...
sub hax2bin {
my $BS = {
0=>'OOOO', 1=>'OOOL', 2=>'OOLO', 3=>'OOLL' ,
4=>'OLOO', 5=>'OLOL', 6=>'OLLO', 7=>'OLLL' ,
8=>'LOOO', 9=>'LOOL', A=>'LOLO', B=>'LOLL' ,
C=>'LLOO', D=>'LLOL', E=>'LLLO', F=>'LLLL'};
join ':', map $BS->{ $_ }, split //, $1
if (shift) =~ / (?:0x)? (.+) /x
}
my $hnum = '0xA1A2';
my $bnum = hax2bin $hnum;
print $bnum;
...
if you really need 'very special' mappings.
Regards
Mirco
------------------------------
Date: Mon, 12 Mar 2007 18:49:18 +0100
From: Mirco Wahab <wahab-mail@gmx.de>
Subject: Re: Convert hex to bin
Message-Id: <et4lok$1k6$1@mlucom4.urz.uni-halle.de>
Mumia W. wrote:
>
> sub hex2bin {
> unpack 'B8', pack 'H2', $_[0];
> }
>
another possibility would be:
...
sub hex2bin {
base_convert (shift), 16, 2
}
...
(OK, I said "possibility") ...
Regards
Mirco
------------------------------
Date: 12 Mar 2007 17:25:29 -0700
From: "Mark" <markbuxbaum@hotmail.com>
Subject: Crash in Perl_stashpv_hvname_match
Message-Id: <1173745529.449688.174540@v33g2000cwv.googlegroups.com>
Hi Perl folks,
Please advise as to appropriate group to post this.
I am seeing an intermittent crash in function
Perl_stashpv_hvname_match.
System is ActivePerl version 5.8.8.820, on Windows Server 2003
Enterprise x64 Edition.
Two stack traces are below.
Please advise how best to proceed.
Thanks!
Mark
*----> State Dump for Thread Id 0xf14 <----*
eax=02894fbc ebx=00fffffb ecx=6d6a2b10 edx=0000000a esi=02894fb8
edi=00000000
eip=28085af7 esp=0240fb90 ebp=0240fbc4 iopl=0 nv up ei pl zr
na po nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b
efl=00010246
*** WARNING: Unable to verify checksum for C:\Perl\bin\perl58.dll
*** ERROR: Symbol file could not be found. Defaulted to export
symbols for C:\Perl\bin\perl58.dll -
function: perl58!Perl_stashpv_hvname_match
28085ae3 44 inc esp
28085ae4 240c and al,0xc
28085ae6 83c00c add eax,0xc
28085ae9 50 push eax
28085aea ff5634 call dword ptr [esi+0x34]
28085aed 8bf8 mov edi,eax
28085aef 8d4604 lea eax,[esi+0x4]
28085af2 59 pop ecx
28085af3 8b08 mov ecx,[eax]
28085af5 8938 mov [eax],edi
FAULT ->28085af7 894704 mov [edi+0x4],eax ds:002b:
00000004=????????
28085afa 890f mov [edi],ecx
28085afc 897708 mov [edi+0x8],esi
28085aff 897904 mov [ecx+0x4],edi
28085b02 8b06 mov eax,[esi]
28085b04 8bce mov ecx,esi
28085b06 ff5010 call dword ptr [eax+0x10]
28085b09 8d470c lea eax,[edi+0xc]
28085b0c 5f pop edi
28085b0d 5e pop esi
28085b0e c20400 ret 0x4
*----> Stack Back Trace <----*
*** WARNING: Unable to verify checksum for C:\Perl\lib\auto\threads
\shared\shared.dll
*** ERROR: Symbol file could not be found. Defaulted to export
symbols for C:\Perl\lib\auto\threads\shared\shared.dll -
*** WARNING: Unable to verify checksum for C:\Perl\bin\perl.exe
*** ERROR: Module load completed but symbols could not be loaded for
C:
\Perl\bin\perl.exe
*** ERROR: Symbol file could not be found. Defaulted to export
symbols for C:\WINDOWS\syswow64\kernel32.dll -
ChildEBP RetAddr Args to Child
WARNING: Stack unwind information not available. Following frames may
be wrong.
0240fbc4 28001971 02965564 00000000 007ffffc perl58!
Perl_stashpv_hvname_match+0x8a5
0240fbe4 28001e4a 02965564 02966384 007ffffc perl58!Perl_av_store
+0x1b8
0240fc04 003e241a 00000000 02966384 6d69cf64 perl58!Perl_av_push+0x1fa
0240fc48 28040019 0e6ecf78 0279d3f8 00000001 shared+0x241a
0240fc84 2805d49d 012a4394 002a4394 280246cf perl58!
Perl_sv_compile_2op
+0x7b9f
0240fd58 280241f5 002a4394 0e6ecf60 00000042 perl58!
Perl_runops_standard+0xc
0240fd6c 280323c6 002a4394 28095504 00000002 perl58!Perl_call_method
+0x2b
0240fd9c 2805d49d 0284e620 002a4394 28023fdf perl58!
Perl_Guse_safe_putenv_ptr+0xa248
0240fe24 28088a21 002a4394 00000000 00000000 perl58!
Perl_runops_standard+0xc
0240ff3c 00401012 00000003 002a3f28 002a2d38 perl58!RunPerl+0x86
0240ffc0 7d4e992a 00000000 00000000 7efdf000 perl+0x1012
0240fff0 00000000 00401016 00000000 00000000 kernel32!
BaseProcessInitPostImport+0x8d
*----> State Dump for Thread Id 0x1694 <----*
eax=002a277c ebx=00000018 ecx=7614bfb8 edx=0000000a esi=002a2778
edi=00000000
eip=28085af7 esp=0240fcdc ebp=0240fd10 iopl=0 nv up ei pl zr
na po nc
cs=0023 ss=002b ds=002b es=002b fs=0053 gs=002b
efl=00010246
*** WARNING: Unable to verify checksum for C:\Perl\bin\perl58.dll
*** ERROR: Symbol file could not be found. Defaulted to export
symbols for C:\Perl\bin\perl58.dll -
function: perl58!Perl_stashpv_hvname_match
28085ae3 44 inc esp
28085ae4 240c and al,0xc
28085ae6 83c00c add eax,0xc
28085ae9 50 push eax
28085aea ff5634 call dword ptr [esi+0x34]
28085aed 8bf8 mov edi,eax
28085aef 8d4604 lea eax,[esi+0x4]
28085af2 59 pop ecx
28085af3 8b08 mov ecx,[eax]
28085af5 8938 mov [eax],edi
FAULT ->28085af7 894704 mov [edi+0x4],eax ds:002b:
00000004=????????
28085afa 890f mov [edi],ecx
28085afc 897708 mov [edi+0x8],esi
28085aff 897904 mov [ecx+0x4],edi
28085b02 8b06 mov eax,[esi]
28085b04 8bce mov ecx,esi
28085b06 ff5010 call dword ptr [eax+0x10]
28085b09 8d470c lea eax,[edi+0xc]
28085b0c 5f pop edi
28085b0d 5e pop esi
28085b0e c20400 ret 0x4
*----> Stack Back Trace <----*
*** WARNING: Unable to verify checksum for C:\Perl\bin\perl.exe
*** ERROR: Module load completed but symbols could not be loaded for
C:
\Perl\bin\perl.exe
*** ERROR: Symbol file could not be found. Defaulted to export
symbols for C:\WINDOWS\syswow64\kernel32.dll -
ChildEBP RetAddr Args to Child
WARNING: Stack unwind information not available. Following frames may
be wrong.
0240fd10 280628b1 002a4394 7614abe4 00000018 perl58!
Perl_stashpv_hvname_match+0x8a5
0240fd30 28066325 04040804 00000000 7614aaf4 perl58!
Perl_sv_setsv_flags
+0x9fd
0240fd9c 2805d49d 0284e64c 002a4394 28023fdf perl58!Perl_newSVsv+0x7a
0240fe24 28088a21 002a4394 00000000 00000000 perl58!
Perl_runops_standard+0xc
0240ff3c 00401012 00000003 002a3f28 002a2d38 perl58!RunPerl+0x86
0240ffc0 7d4e992a 00000000 00000000 7efdf000 perl+0x1012
0240fff0 00000000 00401016 00000000 00000000 kernel32!
BaseProcessInitPostImport+0x8d
------------------------------
Date: Tue, 13 Mar 2007 00:43:45 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Crash in Perl_stashpv_hvname_match
Message-Id: <5tmJh.2015$vV3.18@trndny09>
Mark wrote:
> I am seeing an intermittent crash in function
> Perl_stashpv_hvname_match.
>
> System is ActivePerl version 5.8.8.820, on Windows Server 2003
> Enterprise x64 Edition.
>
> Two stack traces are below.
> Please advise how best to proceed.
See
perldoc perlbug
jue
------------------------------
Date: Mon, 12 Mar 2007 19:15:16 +0000
From: Henry Law <news@lawshouse.org>
Subject: Re: Find Missing Column and Extra Column
Message-Id: <1173726902.21321.0@iris.uk.clara.net>
Rahul wrote:
> Hi All,
>
> This XML file is 2 errors. My table is 3 columns. One is extra column
> and another is missing one columne. How i will find those error
> through perl script.
>
> XML file
> ------------
> <table>
> <tgroup cols="3">
> <colspec colnum="1" colname="col1"/>
...etc
Let me help you help yourself in future. If you must use Perl to do
this task then ask yourself how the language might help; when you
understand the answer to that question then you'll be much further on
and will be able to ask a question in this group which the experts here
(of whom I am not one) can answer.
If Perl is going to help then either
(1) Perl has XML table-parsing capability itself, built-in; or
(2) Perl has some additional stuff you can install which has that
capability; or
(3) Perl itself has no capability to do that but provides facilities
with which you can write the program yourself.
If (1) is true then a Perl manual or help-site will provide some
information. In fact, Perl has a help mode which is quite useful. Try
perldoc -h
at a command line. That will lead you to "perldoc -q XML", maybe, from
which you'll find that the Perl FAQ contains no FAQs to do with XML.
Doesn't look good for Perl having XML capability.
So maybe (2) is true. Have you tried Googling for (say) "Perl XML
parse"? You'll get lots of hits with entries talking about modules,
including XML::Simple, XML::Parser. "What's a module?" I hear you say.
Well perldoc will help you ...
perldoc -q module
... that should lead you to
perldoc XML::Simple
perldoc XML::Parser
At that point you will realise that (3) is in fact the one that's true:
Perl provides assistance in XML parsing but you need to write the
program. Start messing about (yes, write some Perl code!) with
XML::Simple or XML::Parser. You'll get totally confused, at which
point post here. We'll help. We just won't do your work for you.
(Except I just have - some of it at any rate).
But, as others have pointed out, Perl may not be the handiest way of
doing what you want; there are other XML-specific products that might be
more effective. But I smell homework ...
--
Henry Law Manchester, England
------------------------------
Date: Mon, 12 Mar 2007 18:25:21 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Find Missing Column and Extra Column
Message-Id: <slrnevbob1.udb.tadmc@tadmc30.august.net>
Rahul <sahoo.byomokesh@gmail.com> wrote:
> My table is 3 columns.
><tgroup cols="3">
><row>
><entry valign="top" align="left" namest="col1"
> nameend="col2"><p>centra</p></entry>
><!-- One row is missing here -->
I thought you said it was supposed to contain 3 columns?
There are *two* rows missing there then. Right?
You forgot yet again so show us what you have tried so far.
> Thanks for any help.
Thanks for ignoring the help provided to you earlier.
Have another fish along with your scorefile entry:
----------------------------
#!/usr/bin/perl
use warnings;
use strict;
use XML::Simple;
my $xml = '
<table>
<tgroup cols="3">
<colspec colnum="1" colname="col1"/>
<colspec colnum="2" colname="col2"/>
<colspec colnum="3" colname="col3"/>
<tbody>
<row>
<entry valign="top" align="left"><p>nada</p></entry>
<entry valign="top" align="left"><p>nothing</p></entry>
<entry valign="top" align="left"><p>nada.</p></entry>
<entry valign="top" align="left"><p></p></entry> <!-- One Row Extra Here -->
</row>
<row>
<entry valign="top" align="left" namest="col1" nameend="col2"><p>centra</p></entry>
<!-- One row is missing here -->
</row>
</tbody>
</tgroup>
</table>
';
my $ref = XMLin( $xml, ForceArray => ['entry'] );
my $cols = $ref->{tgroup}{cols};
my $row_cnt=0;
foreach my $row ( @{ $ref->{tgroup}{tbody}{row} } ) {
my $col_cnt = @{ $row->{entry} };
print "line ", ++$row_cnt, " has $col_cnt columns\n" unless $cols == $col_cnt;
}
----------------------------
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 12 Mar 2007 12:22:12 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Graphing with Perl
Message-Id: <120320071222120108%jgibson@mail.arc.nasa.gov>
In article <1173599030.090949.45650@c51g2000cwc.googlegroups.com>,
<cylurian@gmail.com> wrote:
> I've used Chart Plot to graph straight lines and parabolas. I've been
> using Chart Plot since 1998, using version .07. I want more
> functionality and control. What do people now use to graph data?
>
gnuplot (<www.gnuplot.info>)
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
------------------------------
Date: 12 Mar 2007 13:52:10 -0700
From: "PerseP" <persep@gmail.com>
Subject: Llama book exercise
Message-Id: <1173732730.489787.36820@p10g2000cwp.googlegroups.com>
Hi,
I'm learning Perl by reading the "llama book". It happens that on the
answer to exercise 3 from chapter 4 (page 234), the authors ask why
the control variable in the subroutine &above_average is named
$element instead of using Perl's default $_.
The code is:
#!/usr/bin/perl
use strict;
sub total {
my $sum;
foreach(@_) {
$sum+=$_;
}
$sum;
}
sub average {
if (@_ ==0) { return; }
my $count =@_;
my $sum=&total(@_);
$sum/$count;
}
sub above_average {
my $average=&average(@_);
my @list;
my $element;
foreach $element (@_) {
if($element > $average) {
push @list,$element;
}
}
@list;
}
my @fred=&above_average(1..10);
print "\@fred is @fred\n";
print "(Should be 6 7 8 9 10)\n";
my @barney=&above_average(100,1..10);
print "\@barney is @barney\n";
print "(should be just 100)\n";
I've run the code as it's given above and with my initial answer,using
$_ instead of $elements:
sub above_average {
my $average=&average(@_);
my @list;
my $element;
foreach (@_) {
if($_ > $average) {
push @list,$_;
}
}
@list;
}
Both give the same output. So is there any point in using $element
instead of $_ as the control variable?
Thanks
------------------------------
Date: 12 Mar 2007 21:11:14 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Llama book exercise
Message-Id: <55ltviF25ffh5U1@mid.dfncis.de>
PerseP <persep@gmail.com> wrote in comp.lang.perl.misc:
> Hi,
> I'm learning Perl by reading the "llama book". It happens that on the
> answer to exercise 3 from chapter 4 (page 234), the authors ask why
> the control variable in the subroutine &above_average is named
> $element instead of using Perl's default $_.
> The code is:
[...]
> sub above_average {
> my $average=&average(@_);
> my @list;
> my $element;
> foreach $element (@_) {
> if($element > $average) {
> push @list,$element;
> }
> }
> @list;
> }
[...]
> I've run the code as it's given above and with my initial answer,using
> $_ instead of $elements:
> sub above_average {
> my $average=&average(@_);
> my @list;
> my $element;
> foreach (@_) {
> if($_ > $average) {
> push @list,$_;
> }
> }
> @list;
> }
>
> Both give the same output. So is there any point in using $element
> instead of $_ as the control variable?
It must be a stylistic point the authors are making there. As you
have observed, technically it is quite possible to use $_ instead.
Anno
------------------------------
Date: Mon, 12 Mar 2007 14:11:06 -0700
From: brian d foy <brian.d.foy@gmail.com>
Subject: Re: Llama book exercise
Message-Id: <120320071411069057%brian.d.foy@gmail.com>
In article <1173732730.489787.36820@p10g2000cwp.googlegroups.com>,
PerseP <persep@gmail.com> wrote:
> I'm learning Perl by reading the "llama book". It happens that on the
> answer to exercise 3 from chapter 4 (page 234), the authors ask why
> the control variable in the subroutine &above_average is named
> $element instead of using Perl's default $_.
> Both give the same output. So is there any point in using $element
> instead of $_ as the control variable?
$_ is a global variable. Instead of getting into the bad habit of using
it when you don't need it, we suggest that you use your own variable
for the foreach. It might seem a bit silly for the simple exercise, but
it's never too early to start good coding practices. :)
--
Posted via a free Usenet account from http://www.teranews.com
------------------------------
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 218
**************************************