[31929] in Perl-Users-Digest
Perl-Users Digest, Issue: 3192 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 30 14:09:26 2010
Date: Sat, 30 Oct 2010 11:09:09 -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 Sat, 30 Oct 2010 Volume: 11 Number: 3192
Today's topics:
donate your car <muhammadsalman712@gmail.com>
grep example's use of $_ confusing me. <justin.1010@purestblue.com>
Re: grep example's use of $_ confusing me. <tadmc@seesig.invalid>
Re: grep example's use of $_ confusing me. <jimsgibson@gmail.com>
Re: Outer scope of a sub inside a sub <nospam-abuse@ilyaz.org>
perl parse (reg exp) <dontmewithme@got.it>
Re: perl parse (reg exp) <jurgenex@hotmail.com>
Re: perl parse (reg exp) <tadmc@seesig.invalid>
Sort and Print Multidimensional Hash <dontmewithme@got.it>
Re: Sort and Print Multidimensional Hash <jurgenex@hotmail.com>
Re: Sort and Print Multidimensional Hash <jurgenex@hotmail.com>
Re: Sort and Print Multidimensional Hash <tadmc@seesig.invalid>
Re: Sort and Print Multidimensional Hash <tadmc@seesig.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 29 Oct 2010 03:39:31 -0700 (PDT)
From: Muhammad Salman <muhammadsalman712@gmail.com>
Subject: donate your car
Message-Id: <74f78223-2599-45dd-a710-ccd2f888ca43@l17g2000yqe.googlegroups.com>
Check out my videos and photos.
http://www.flixya.com/video/3575944/Donate_Your_Car
------------------------------
Date: Fri, 29 Oct 2010 18:51:58 +0100
From: Justin C <justin.1010@purestblue.com>
Subject: grep example's use of $_ confusing me.
Message-Id: <u6erp7-t74.ln1@purestblue.com>
I'm reading The Alpaca (Intermediate Perl - yeah, I know, do I *really*
think I'm smart enough to be there yet?) and in Chapter 6 I find the
following example:
my @input_numbers = (1, 2, 4, 8, 16, 32, 64);
my @indices_of_odd_digit_sums = grep {
my $number = $input_numbers[$_];
my $sum;
$sum += $_ for split //, $number;
$sum % 2;
} 0..$#input_numbers;
What has happened to $_ ? In line three I understand that it is one of
the indices of @input_numbers, but in line 5 it has become each of the
digits of @input_numbers. I'm just getting used to map and grep, and
throwing that in there has been a bit confusing. I can see what has
happened, but am having difficulty reconciling that with use of $_ in
grep and map blocks. Is the first $_ clobbered, or is it still there
after the 5th line? ... hmmm, yes it is... not it isn't it's been
clobbered, it's... no, I was right the first time! (it's been a long
week). So, is line 5 a naked block with it's own $_? How do I identify,
in other grep/map blocks, which $_ is $_?!
Justin.
--
Justin C, by the sea.
------------------------------
Date: Fri, 29 Oct 2010 14:00:41 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: grep example's use of $_ confusing me.
Message-Id: <slrnicm6pj.8rg.tadmc@tadbox.sbcglobal.net>
Justin C <justin.1010@purestblue.com> wrote:
> I'm reading The Alpaca (Intermediate Perl - yeah, I know, do I *really*
> think I'm smart enough to be there yet?) and in Chapter 6 I find the
> following example:
>
> my @input_numbers = (1, 2, 4, 8, 16, 32, 64);
> my @indices_of_odd_digit_sums = grep {
> my $number = $input_numbers[$_];
> my $sum;
> $sum += $_ for split //, $number;
> $sum % 2;
> } 0..$#input_numbers;
>
> What has happened to $_ ?
Sometimes it contains list elements for grep() and sometimes
it contains list elements for for() (and then restores the grep
value, after the for is finished).
> In line three I understand that it is one of
> the indices of @input_numbers, but in line 5 it has become each of the
> digits of @input_numbers.
Because that is what foreach does.
The grep value is saved in some secret place, foreach uses its own
value for a while, and then restores the grep value when it is done.
> I'm just getting used to map and grep, and
> throwing that in there has been a bit confusing. I can see what has
> happened, but am having difficulty reconciling that with use of $_ in
> grep and map blocks. Is the first $_ clobbered, or is it still there
> after the 5th line? ... hmmm, yes it is... not it isn't it's been
> clobbered, it's... no, I was right the first time! (it's been a long
> week). So, is line 5 a naked block with it's own $_?
No, it is a foreach loop with it's own $_.
From the "Foreach Loops" section of
perldoc perlsyn
the variable is implicitly local to the loop and regains its
former value upon exiting the loop.
...
The C<foreach> keyword is actually a synonym for the C<for> keyword
...
If VAR is omitted, C<$_> is set to each value.
so,
$sum += $_ for split //, $number;
is the same as
$sum += $_ foreach split //, $number;
which is the same as
foreach (split //, $number) {
$sum += $_;
}
which is the same as
foreach $_ (split //, $number) {
$sum += $_;
}
> How do I identify,
> in other grep/map blocks, which $_ is $_?!
(you mean for grep/map/foreach, since they all can use $_)
You must keep track of their nesting.
If this was to be production code (rather than learning code), then
I would replace the for with a foreach that uses its own loop
control variable:
foreach my $digit (split //, $number) {
$sum += $digit;
}
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Fri, 29 Oct 2010 12:06:17 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: grep example's use of $_ confusing me.
Message-Id: <291020101206174739%jimsgibson@gmail.com>
In article <u6erp7-t74.ln1@purestblue.com>, Justin C
<justin.1010@purestblue.com> wrote:
> I'm reading The Alpaca (Intermediate Perl - yeah, I know, do I *really*
> think I'm smart enough to be there yet?) and in Chapter 6 I find the
> following example:
>
> my @input_numbers = (1, 2, 4, 8, 16, 32, 64);
> my @indices_of_odd_digit_sums = grep {
> my $number = $input_numbers[$_];
> my $sum;
> $sum += $_ for split //, $number;
> $sum % 2;
> } 0..$#input_numbers;
>
> What has happened to $_ ? In line three I understand that it is one of
> the indices of @input_numbers, but in line 5 it has become each of the
> digits of @input_numbers.
The 'for' in line 5 is a statement modifier. Search for 'Statement
Modifiers' in perldoc perlsyn. Line 5 is equivalent to:
foreach ( split //, $number ) {
$sum += $_;
}
Further on in 'perldoc perlsyn' we read that:
"Foreach Loops
The "foreach" loop iterates over a normal list value and sets the
variable VAR to be each element of the list in turn. If the variable
is preceded with the keyword "my", then it is lexically scoped, and is
therefore visible only within the loop. Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop. If the variable was previously declared with "my", it uses
that variable instead of the global one, but it's still localized to
the loop. This implicit localisation occurs only in a "foreach" loop."
Thus, $_ is localized in line 5 and regains its previous value at line
6.
Note also that the original value of $_ at entering the grep block is
only used once in the first line of the block, so it doesn't really
matter whether $_ is clobbered by the for modifier. Programming that
does not depend upon little-known behavior is usually a "good thing".
> I'm just getting used to map and grep, and
> throwing that in there has been a bit confusing. I can see what has
> happened, but am having difficulty reconciling that with use of $_ in
> grep and map blocks. Is the first $_ clobbered, or is it still there
> after the 5th line? ... hmmm, yes it is... not it isn't it's been
> clobbered, it's... no, I was right the first time! (it's been a long
> week). So, is line 5 a naked block with it's own $_? How do I identify,
> in other grep/map blocks, which $_ is $_?!
If you want to know what value is in $_ at any point in your program,
run it with a debugger or add some print statements:
#!/usr/bin/perl
use strict;
use warnings;
my @input_numbers = (1, 2, 4, 8, 16, 32, 64);
my @indices_of_odd_digit_sums = grep {
print "1: \$_=$_\n";
my $number = $input_numbers[$_];
my $sum;
$sum += $_ for split //, $number;
print "2: \$_=$_\n";
$sum % 2;
} 0..$#input_numbers;
print "Indices of odd digit sums: ", join(", ",
@indices_of_odd_digit_sums),
"\n";
__OUTPUT__
1: $_=0
2: $_=0
1: $_=1
2: $_=1
1: $_=2
2: $_=2
1: $_=3
2: $_=3
1: $_=4
2: $_=4
1: $_=5
2: $_=5
1: $_=6
2: $_=6
Indices of odd digit sums: 0, 4, 5
--
Jim Gibson
------------------------------
Date: Fri, 29 Oct 2010 22:51:04 +0000 (UTC)
From: Ilya Zakharevich <nospam-abuse@ilyaz.org>
Subject: Re: Outer scope of a sub inside a sub
Message-Id: <slrnicmjuo.ek2.nospam-abuse@powdermilk.math.berkeley.edu>
On 2010-10-29, Xho Jingleheimerschmidt <xhoster@gmail.com> wrote:
> Ilya Zakharevich wrote:
>> On 2010-10-28, Xho Jingleheimerschmidt <xhoster@gmail.com> wrote:
>>
>>> At compile time, this glombs onto the same memory location as was used
>>> for @elems when the outer scope was compiled. It is not subsequently
>>> recompiled, and so does not re-glomb onto the new addresses when new
>>> addresses come into existence.
>>
>> There is no such thing as "recompilation" (the whole point of closures
>> is that they are cheap; what you think of is IMO a
>> poor-man-implementation of closures via eval "sub {...}").
>
> Sure there is such a thing as recompilation. Just put it in a string
> eval. It isn't recompiled in *this* context, of course, which is what I
> said.
Mea culpa, I quoted a wrong paragraph. I should have stated it "like
this":
>> sub run_test_2 {
>> my @elems = ();
>>
>> my $filter = sub {
>> printf "%d\n", refaddr \@elems;
>> push @elems, $_[0];
>> };
>
> This is recompiled for each invocation of run_test_2, and so re-glombs
> onto the underlying address of the structure behind @elems which is in
> use each time it recompiled. (I don't think it is recompiles in full,
> they use shortcuts that allows it to re-glomb, with full
> recompilation. But it behaves as if it were recompiled)
There is no such thing as "recompilation" (the whole point of closures
is that they are cheap; what you think of is IMO a
poor-man-implementation of closures via eval "sub {...}").
Sorry,
Ilya
------------------------------
Date: Sat, 30 Oct 2010 12:15:50 +0200
From: "Larry" <dontmewithme@got.it>
Subject: perl parse (reg exp)
Message-Id: <4ccbf057$0$27971$4fafbaef@reader5.news.tin.it>
Hi,
I'm using this chunk of code to extract some content from a piece of
html.
($parse) = ($html =~ /<tbody>(.*?)<\/tbody>/sg);
so that I can grab everything between <tbody> and </tbody>
Now, I have a some <tr> tags I'd like to parse as follows:
<tr class="odd"></tr>
<tr class="even"></tr>
Yet, I want to skip the attribute. I am a newbie with reg exp and I am stuck
at this:
(@parse) = ($parse =~ /<tr (\W+)>(.*?)<\/tr>/sg);
but it's not working..how should I go about?
thanks
------------------------------
Date: Sat, 30 Oct 2010 03:41:11 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: perl parse (reg exp)
Message-Id: <i7tnc6trdqleiihl97j3q6k86jdv3riij7@4ax.com>
"Larry" <dontmewithme@got.it> wrote:
> I'm using this chunk of code to extract some content from a piece of
>html.
[...]
>but it's not working..how should I go about?
To parse HTML you should use an HTML parser. Unless you are writing such
a beast and you know what you are doing because you have experience in
e.g. writing compilers it is in general A Very Bad Idea to try parsing
HTLM using ad-hoc REs.
For further information see the FAQ:
- "How do I match XML, HTML, or other nasty, ugly things with a regex?"
- "How do I remove HTML from a string?"
or the many, many previous discussions about this perpetual topic.
jue
------------------------------
Date: Sat, 30 Oct 2010 10:07:43 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: perl parse (reg exp)
Message-Id: <slrnicodh1.aee.tadmc@tadbox.sbcglobal.net>
Larry <dontmewithme@got.it> wrote:
> I have a some <tr> tags I'd like to parse
Let me repeat:
You should use a module that understands HTML for processing HTML data.
> I am a newbie with reg exp
That is irrelevant, since you should not be using regular
expressions for this task.
Let me repeat:
Friends don't let friends parse HTML with regexes!
> how should I go about?
By using a module that understands HTML for processing HTML data.
The HTML::TableExtract module that I gave you example code for last
December should do the trick.
Use the HTML::TableExtract module.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sat, 30 Oct 2010 16:02:47 +0200
From: "Larry" <dontmewithme@got.it>
Subject: Sort and Print Multidimensional Hash
Message-Id: <4ccc2588$0$26708$4fafbaef@reader1.news.tin.it>
Hi!,
I have a multi dimensional hash made up like this below:
my $global = {};
$global->{"KEY1"}->{"ID"} = k;
$global->{"KEY1"}->{"VALUE1"} = "...";
$global->{"KEY1"}->{"VALUE2"} = "...";
$global->{"KEY2"}->{"ID"} = k;
$global->{"KEY2"}->{"VALUE1"} = "...";
$global->{"KEY2"}->{"VALUE2"} = "...";
Now I'd like to sort it by ID (0..1) then print it, is there an easy way to
do that?
thanks
------------------------------
Date: Sat, 30 Oct 2010 07:46:25 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Sort and Print Multidimensional Hash
Message-Id: <veboc6de63rrudqme5f2t2i16re6ni21v6@4ax.com>
"Larry" <dontmewithme@got.it> wrote:
>Hi!,
>
>I have a multi dimensional hash made up like this below:
>
>my $global = {};
>$global->{"KEY1"}->{"ID"} = k;
>$global->{"KEY1"}->{"VALUE1"} = "...";
>$global->{"KEY1"}->{"VALUE2"} = "...";
>
>$global->{"KEY2"}->{"ID"} = k;
>$global->{"KEY2"}->{"VALUE1"} = "...";
>$global->{"KEY2"}->{"VALUE2"} = "...";
>
>Now I'd like to sort it by ID (0..1) then print it, is there an easy way to
>do that?
No, the way you stated your question it is impossible because hashes do
not have an order and therefore cannot be sorted.
However, if you rephrase your question in a more meaningful way like
e.g. "how can I sort the keys of %global such that they are sorted by
the value of ID", then the challenge becomes trivial to solve:
Just sort() the keys() of %global, using {$global->$_->'ID'} as the
compare function for sort().
And then loop through the the sorted keys and print the associated hash
elements in that order.
jue
------------------------------
Date: Sat, 30 Oct 2010 08:13:54 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Sort and Print Multidimensional Hash
Message-Id: <5cdoc6196ukg6on0jfejtuqvvthsotv3l5@4ax.com>
Jürgen Exner <jurgenex@hotmail.com> wrote:
>"Larry" <dontmewithme@got.it> wrote:
>>Hi!,
>>
>>I have a multi dimensional hash made up like this below:
>>
>>my $global = {};
>>$global->{"KEY1"}->{"ID"} = k;
>>$global->{"KEY1"}->{"VALUE1"} = "...";
>>$global->{"KEY1"}->{"VALUE2"} = "...";
>>
>>$global->{"KEY2"}->{"ID"} = k;
>>$global->{"KEY2"}->{"VALUE1"} = "...";
>>$global->{"KEY2"}->{"VALUE2"} = "...";
>>
>>Now I'd like to sort it by ID (0..1) then print it, is there an easy way to
>>do that?
>
>No, the way you stated your question it is impossible because hashes do
>not have an order and therefore cannot be sorted.
>
>However, if you rephrase your question in a more meaningful way like
>e.g. "how can I sort the keys of %global such that they are sorted by
>the value of ID", then the challenge becomes trivial to solve:
>Just sort() the keys() of %global, using {$global->$_->'ID'} as the
>compare function for sort().
Actually, this is very poorly worded, let me rephrase:
... comparing $global->$a->'ID' and $global->$b->'ID' in the compare
function for sort().
jue
------------------------------
Date: Sat, 30 Oct 2010 10:23:24 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Sort and Print Multidimensional Hash
Message-Id: <slrnicoeef.aee.tadmc@tadbox.sbcglobal.net>
Larry <dontmewithme@got.it> wrote:
> Hi!,
>
> I have a multi dimensional hash made up like this below:
>
> my $global = {};
> $global->{"KEY1"}->{"ID"} = k;
> $global->{"KEY1"}->{"VALUE1"} = "...";
> $global->{"KEY1"}->{"VALUE2"} = "...";
>
> $global->{"KEY2"}->{"ID"} = k;
> $global->{"KEY2"}->{"VALUE1"} = "...";
> $global->{"KEY2"}->{"VALUE2"} = "...";
>
> Now I'd like to sort it by ID
That won't be very interesting, because your data has the same
value for all of its IDs.
That is, it is _already_ sorted.
If you mean that your data has different values for ID, then you
should post data that has different values for ID.
Have you seen the posting Guidelines that are posted here frequently?
> (0..1)
I have no idea what you meant by that part.
What did you mean by that part?
> then print it,
Print it how?
Print the KEYs?
print "$_\n"
for sort {$global->{$a}->{ID} <=> $global->{$b}->{ID}} keys %$global;
Print the IDs?
print "$global->{$_}{ID}\n"
for sort {$global->{$a}->{ID} <=> $global->{$b}->{ID}} keys %$global;
Print the whole subhashes?
use Data::Dumper;
print Dumper $global->{$_}
for sort {$global->{$a}->{ID} <=> $global->{$b}->{ID}} keys %$global;
> is there an easy way to
> do that?
So if there is only a hard way, then you don't want an answer?
I would think you should be interested in whether there is a
way to do that.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sat, 30 Oct 2010 10:31:13 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Sort and Print Multidimensional Hash
Message-Id: <slrnicoet3.agf.tadmc@tadbox.sbcglobal.net>
Jürgen Exner <jurgenex@hotmail.com> wrote:
> Jürgen Exner <jurgenex@hotmail.com> wrote:
>>However, if you rephrase your question in a more meaningful way like
>>e.g. "how can I sort the keys of %global such that they are sorted by
>>the value of ID", then the challenge becomes trivial to solve:
>>Just sort() the keys() of %global, using {$global->$_->'ID'} as the
>>compare function for sort().
>
> Actually, this is very poorly worded,
and it is missing a bunch of curly braces too...
> let me rephrase:
> ... comparing $global->$a->'ID' and $global->$b->'ID' in the compare
> function for sort().
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 3192
***************************************