[31480] in Perl-Users-Digest
Perl-Users Digest, Issue: 2739 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 24 14:09:42 2009
Date: Thu, 24 Dec 2009 11:09:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 24 Dec 2009 Volume: 11 Number: 2739
Today's topics:
array reference and implicit creation <user@example.net>
Re: array reference and implicit creation (Jens Thoms Toerring)
Re: array reference and implicit creation <user@example.net>
Re: array reference and implicit creation <jurgenex@hotmail.com>
Re: array reference and implicit creation (Gary E. Ansok)
Re: array reference and implicit creation <uri@StemSystems.com>
Re: i18n - and 2-headed strings? <ams@sister.ludd.ltu.se>
Re: i18n - and 2-headed strings? <ams@sister.ludd.luth.se>
Re: Java vs. C# <john@castleamber.com>
Re: Java vs. C# <spamtrap@shermpendley.com>
Re: Java vs. C# <john@castleamber.com>
Re: Simple loop error (Seymour J.)
Re: Simple loop error (Bradley K. Sherman)
Re: Trying to split a string on numbers sln@netherlands.com
Re: Trying to split a string on numbers <tadmc@seesig.invalid>
Re: Trying to split a string on numbers <darkon.tdo@gmail.com>
Re: Trying to split a string on numbers <tadmc@seesig.invalid>
Re: Trying to split a string on numbers <sopan.shewale@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 24 Dec 2009 11:20:25 -0500
From: monkeys paw <user@example.net>
Subject: array reference and implicit creation
Message-Id: <Fo2dnQ5uib9DCa7WnZ2dnUVZ_h-dnZ2d@insightbb.com>
I ran into an elusive error and am posting what seems to
be a perl oddity. Take a hash ref:
use Data::Dumper;
my $item = { name => 'joe' }
print Dumper($item);
you get:
$VAR1 = {
'name' => 'joe'
};
Now check item for a element call bill_tags:
if ($#{$item->{bill_tags}} > -1) {
;
}
print Dumper($item);
OUTPUT IS:
$VAR1 = {
'bill_tags' => [],
'name' => 'joe'
};
# The bill_tags array ref is created just by checking to see if it
# is there. This is implicit array creation is it not?
------------------------------
Date: 24 Dec 2009 17:28:38 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: array reference and implicit creation
Message-Id: <7phmm6F840U1@mid.uni-berlin.de>
monkeys paw <user@example.net> wrote:
> I ran into an elusive error and am posting what seems to
> be a perl oddity. Take a hash ref:
> use Data::Dumper;
> my $item = { name => 'joe' }
> print Dumper($item);
> you get:
> $VAR1 = {
> 'name' => 'joe'
> };
> Now check item for a element call bill_tags:
> if ($#{$item->{bill_tags}} > -1) {
> ;
> }
> ....
> # The bill_tags array ref is created just by checking to see if it
> # is there. This is implicit array creation is it not?
You actually don't check for an hash element named 'bill_tags'
but you ask what the index of the last elements of an array,
pointed to by the hash element 'bill_tags', is. That makes a
difference because you implicitely tell the interpreter that
you want the 'bill_tags' element to exist (as a array ref),
otherwise why would you care for the index of its last element
(and how should the interpreter figure out the index for a
non-existing array)?
So if you want to avoid creating the element automatically
you will have to check first if the 'bill_tags' element
actually exists and only in that case ask for the index of
the last element, e.g. using
if (defined $item->{bill_tags} and $#{$item->{bill_tags}} > -1) {
...
The check if the 'bill_tags' hash element exists is done with
the defined() function, since using the element (e.g. by asking
for the index) forces the interpreter to automatically create it.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Thu, 24 Dec 2009 12:38:52 -0500
From: monkeys paw <user@example.net>
Subject: Re: array reference and implicit creation
Message-Id: <l8GdnfbEw8fcOq7WnZ2dnUVZ_txi4p2d@insightbb.com>
Jens Thoms Toerring wrote:
> monkeys paw <user@example.net> wrote:
>> I ran into an elusive error and am posting what seems to
>> be a perl oddity. Take a hash ref:
>
>> use Data::Dumper;
>> my $item = { name => 'joe' }
>
>> print Dumper($item);
>
>> you get:
>
>> $VAR1 = {
>> 'name' => 'joe'
>> };
>
>> Now check item for a element call bill_tags:
>
>> if ($#{$item->{bill_tags}} > -1) {
>> ;
>> }
>
>> ....
>
>> # The bill_tags array ref is created just by checking to see if it
>> # is there. This is implicit array creation is it not?
>
> You actually don't check for an hash element named 'bill_tags'
> but you ask what the index of the last elements of an array,
> pointed to by the hash element 'bill_tags', is. That makes a
> difference because you implicitely tell the interpreter that
> you want the 'bill_tags' element to exist (as a array ref),
> otherwise why would you care for the index of its last element
> (and how should the interpreter figure out the index for a
> non-existing array)?
Thanks, that makes sense. I knew from this finding that i needed
to check existence using defined. It was just interesting
that it was created in that situation. As i said, it does make
sense that asking for the properties of an array implies its
existence.
>
> So if you want to avoid creating the element automatically
> you will have to check first if the 'bill_tags' element
> actually exists and only in that case ask for the index of
> the last element, e.g. using
>
> if (defined $item->{bill_tags} and $#{$item->{bill_tags}} > -1) {
> ...
>
> The check if the 'bill_tags' hash element exists is done with
> the defined() function, since using the element (e.g. by asking
> for the index) forces the interpreter to automatically create it.
>
> Regards, Jens
------------------------------
Date: Thu, 24 Dec 2009 09:58:43 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: array reference and implicit creation
Message-Id: <hha7j55mllea3tqvpgi7uc88cboo6qee8u@4ax.com>
monkeys paw <user@example.net> wrote:
>Jens Thoms Toerring wrote:
>> monkeys paw <user@example.net> wrote:
>>> I ran into an elusive error and am posting what seems to
>>> be a perl oddity.
I'd call it a nifftly little feature
>Take a hash ref:
Don't need a hash ref, a simple hash will do just fine.
>>> use Data::Dumper;
>>> my $item = { name => 'joe' }
>>
>>> print Dumper($item);
>>
>>> you get:
>>
>>> $VAR1 = {
>>> 'name' => 'joe'
>>> };
>>
>>> Now check item for a element call bill_tags:
>>
>>> if ($#{$item->{bill_tags}} > -1) {
>>> ;
>>> }
>>
>>> ....
>>
>>> # The bill_tags array ref is created just by checking to see if it
>>> # is there. This is implicit array creation is it not?
>>
>> You actually don't check for an hash element named 'bill_tags'
>> but you ask what the index of the last elements of an array,
[...]
>Thanks, that makes sense. I knew from this finding that i needed
>to check existence using defined.
Actually exists() would probably be the better choice.
>It was just interesting
>that it was created in that situation.
It's called auto-vivication and is a very useful feature, e.g. in
while (my $elem = ....) {
$mylist{getkey($elem)} ++;
to count keys without worrying about creating the hash element first.
jue
------------------------------
Date: Thu, 24 Dec 2009 18:07:29 +0000 (UTC)
From: ansok@alumni.caltech.edu (Gary E. Ansok)
Subject: Re: array reference and implicit creation
Message-Id: <hh0al1$8rd$1@naig.caltech.edu>
In article <Fo2dnQ5uib9DCa7WnZ2dnUVZ_h-dnZ2d@insightbb.com>,
monkeys paw <user@example.net> wrote:
>I ran into an elusive error and am posting what seems to
>be a perl oddity. Take a hash ref:
>
>use Data::Dumper;
>my $item = { name => 'joe' }
>
>if ($#{$item->{bill_tags}} > -1) {
> ;
>}
>print Dumper($item);
The array reference is not auto-created if you test this way:
if (@{$item->{bill_tags}}) {
(You can add an explicit > 0 test if you like, but it seems
cleaner to me without it.)
Remember that @array in a scalar context gives the number of
entries in the array, which is often what you are really after
(rather than the index of the last entry given by $#array).
Gary Ansok
------------------------------
Date: Thu, 24 Dec 2009 13:32:18 -0500
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: array reference and implicit creation
Message-Id: <87d424xlxp.fsf@quad.sysarch.com>
>>>>> "GEA" == Gary E Ansok <ansok@alumni.caltech.edu> writes:
GEA> In article <Fo2dnQ5uib9DCa7WnZ2dnUVZ_h-dnZ2d@insightbb.com>,
GEA> monkeys paw <user@example.net> wrote:
>> I ran into an elusive error and am posting what seems to
>> be a perl oddity. Take a hash ref:
>>
>> use Data::Dumper;
>> my $item = { name => 'joe' }
>>
>> if ($#{$item->{bill_tags}} > -1) {
>> ;
>> }
>> print Dumper($item);
GEA> The array reference is not auto-created if you test this way:
GEA> if (@{$item->{bill_tags}}) {
that is wrong.
perl -le '@{$x->{bar}} > 0 ; print $x'
HASH(0x900d248)
autovivification happens whenever you have an undef value that is used
like a hard ref (and can be modified). your code assumed the ref existed
and so it then existed afterwards.
GEA> (You can add an explicit > 0 test if you like, but it seems
GEA> cleaner to me without it.)
GEA> Remember that @array in a scalar context gives the number of
GEA> entries in the array, which is often what you are really after
GEA> (rather than the index of the last entry given by $#array).
same thing in this case. the diff is 1 (or $[ which should always be
left as 1).
i will post my autoviv article again for the OP and all interested
parties. please post it whenever you see an autovivify issue as it
covers it fairly well and in depth.
http://sysarch.com/Perl/autoviv.txt
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 24 Dec 2009 10:50:15 +0000 (UTC)
From: Martin Str|mberg <ams@sister.ludd.ltu.se>
Subject: Re: i18n - and 2-headed strings?
Message-Id: <hgvh17$461$1@aioe.org>
Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth Martin Str|mberg <ams@sister.ludd.luth.se>:
>> Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>> > Do you understand that some $@ are created by Perl executable, and
>> > some by scripts? Which do you mean?
>>
>> Doesn't matter. When $@ is read while "use i18n Swedish" is in effect
>> it should be in Swedish, which for me implies some dynamic translation
> *What* should be in Swedish? Messages from the perl C code? Messages
> from modules?
Perhaps I misunderstand something? This is how it should work:
$@ = "the water";
use i18n Swedish;
print "$@\n"; # Prints "vattnet".
use i18n German;
print "$@\n"; # Prints "der Wasser". (Sorry for any genus maltreatment.)
use i18n French;
print "$@\n"; # Prints "Ãl'oeau". (Sorry for any misspelling.)
Except (by necessesity) limited to whatever strings are in perl (and
modules when completed).
--
MartinS
------------------------------
Date: Thu, 24 Dec 2009 10:53:51 +0000 (UTC)
From: Martin Str|mberg <ams@sister.ludd.luth.se>
Subject: Re: i18n - and 2-headed strings?
Message-Id: <hgvh7v$47r$4@aioe.org>
Ben Morrow <ben@morrow.me.uk> wrote:
> Quoth Martin Str|mberg <ams@sister.ludd.luth.se>:
>> Ilya Zakharevich <nospam-abuse@ilyaz.org> wrote:
>> > Do you understand that some $@ are created by Perl executable, and
>> > some by scripts? Which do you mean?
>>
>> Doesn't matter. When $@ is read while "use i18n Swedish" is in effect
>> it should be in Swedish, which for me implies some dynamic translation
> *What* should be in Swedish? Messages from the perl C code? Messages
> from modules?
Perhaps I misunderstand something? This is how it should work:
$@ = "the water";
use i18n Swedish;
print "$@\n"; # Prints "vattnet".
use i18n German;
print "$@\n"; # Prints "der Wasser". (Sorry for any genus maltreatment.)
use i18n French;
print "$@\n"; # Prints "Ãl'oeau". (Sorry for any misspelling.)
Except (by necessesity) limited to whatever strings are in perl (and
modules when completed).
--
MartinS
------------------------------
Date: Wed, 23 Dec 2009 15:36:47 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: Java vs. C#
Message-Id: <87skb174pc.fsf@castleamber.com>
Sherm Pendley <spamtrap@shermpendley.com> writes:
> ccc31807 <cartercc@gmail.com> writes:
>
>> I interview for jobs occasionally, and it's typical to be told, "If
>> you work here, you will use X." Would I not be a 'real programmer' if
>> I accepted a position under these conditions and wrote only in X?
>
> Like I said - real programmers will learn and use whatever is required.
> Sometimes those requirements are driven by technical constraints; more
> often, it's the result of a boss saying "use X."
>
> The wannabe programmer would refuse that job on the basis that he's a
> "Y programmer" and won't use anything else. Or, given the current state
> of the economy, he'd take the job and then whine and moan endlessly
> about how much X sucks.
I don't understand how someone who wants to program Y and detests X so
much shows up on a job interview for X.
Anyway, I wouldn't take a job that requires me to program in PHP
(anymore). I do detest that language, and no, I am not a wannabe
programmer and no I don't have a problem with learning new languages.
Programming must be fun.
--
John Bokma
Read my blog: http://johnbokma.com/
Hire me (Perl/Python): http://castleamber.com/
------------------------------
Date: Wed, 23 Dec 2009 18:07:51 -0500
From: Sherm Pendley <spamtrap@shermpendley.com>
Subject: Re: Java vs. C#
Message-Id: <m21viljnlk.fsf@shermpendley.com>
John Bokma <john@castleamber.com> writes:
> I don't understand how someone who wants to program Y and detests X so
> much shows up on a job interview for X.
I've seen a candidate show up for an interview where the ad clearly
stated we needed someone to help maintain some existing Perl code. He
dodged all of the questions about Perl, and basically tried to convince
us to scrap it all and rewrite it in Java.
No, I'm not joking. Needless to say, the interview didn't last long...
> Anyway, I wouldn't take a job that requires me to program in PHP
> (anymore).
<shrug> Your loss. I'll take that money if you don't want it. ;-)
sherm--
------------------------------
Date: Wed, 23 Dec 2009 18:25:23 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: Java vs. C#
Message-Id: <87oclp6wwc.fsf@castleamber.com>
Sherm Pendley <spamtrap@shermpendley.com> writes:
> John Bokma <john@castleamber.com> writes:
>
>> I don't understand how someone who wants to program Y and detests X so
>> much shows up on a job interview for X.
>
> I've seen a candidate show up for an interview where the ad clearly
> stated we needed someone to help maintain some existing Perl code. He
> dodged all of the questions about Perl, and basically tried to convince
> us to scrap it all and rewrite it in Java.
>
> No, I'm not joking. Needless to say, the interview didn't last long...
:-D I am sure you're not joking. But still I don't understand what
people are thinking when they do so.
>> Anyway, I wouldn't take a job that requires me to program in PHP
>> (anymore).
>
> <shrug> Your loss. I'll take that money if you don't want it. ;-)
If you really want it, email me. I do get now and then asked if I can do
PHP coding and I really don't want to do it anymore. I know my
limitations, and just do not want to learn (more) PHP [1] if I can avoid
it. It's the same reason why I didn't like German as a language much,
too similar to Dutch (my 1st language). While I can switch between
programming languages, it eats up time, and in my experience more if the
languages are somewhat similar.
I want to get better at Perl and get much better at Python. And then
there is Haskell, LUA, Ruby, and Perl 6 (somewhere in the future). So
many languages, so little time.
Hope this all doesn't make me a programming n00b :D. (j/k)
[1] despite having recently bought two good books on PHP.
--
John Bokma
Read my blog: http://johnbokma.com/
Hire me (Perl/Python): http://castleamber.com/
------------------------------
Date: Wed, 23 Dec 2009 16:56:06 -0500
From: Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid>
Subject: Re: Simple loop error
Message-Id: <4b3291f6$2$fuzhry+tra$mr2ice@news.patriot.net>
In <hgo4cd$p07$1@reader1.panix.com>, on 12/21/2009
at 03:31 PM, bks@panix.com (Bradley K. Sherman) said:
>So, in short, on your planet, in Perl, '<=' does not test
>for equality, while here on Earth, it does.
No. On Earth it test for less-than-or-equal, a single Boolean operator. On
*your* planet it performs two separate tests.
--
Shmuel (Seymour J.) Metz, SysProg and JOAT <http://patriot.net/~shmuel>
Unsolicited bulk E-mail subject to legal action. I reserve the
right to publicly post or ridicule any abusive E-mail. Reply to
domain Patriot dot net user shmuel+news to contact me. Do not
reply to spamtrap@library.lspace.org
------------------------------
Date: Thu, 24 Dec 2009 14:21:57 +0000 (UTC)
From: bks@panix.com (Bradley K. Sherman)
Subject: Re: Simple loop error
Message-Id: <hgvte5$ql0$1@reader1.panix.com>
In article <4b3291f6$2$fuzhry+tra$mr2ice@news.patriot.net>,
Shmuel (Seymour J.) Metz <spamtrap@library.lspace.org.invalid> wrote:
>In <hgo4cd$p07$1@reader1.panix.com>, on 12/21/2009
> at 03:31 PM, bks@panix.com (Bradley K. Sherman) said:
>
>>So, in short, on your planet, in Perl, '<=' does not test
>>for equality, while here on Earth, it does.
>
>No. On Earth it test for less-than-or-equal, a single Boolean operator. On
>*your* planet it performs two separate tests.
>
Respond to what I wrote, not what you think I wrote.
See also
<http://mathworld.wolfram.com/StrictInequality.html>
--bks
------------------------------
Date: Wed, 23 Dec 2009 13:28:48 -0800
From: sln@netherlands.com
Subject: Re: Trying to split a string on numbers
Message-Id: <bp25j5pkmdrstd6hbtbtk12d1h9ch2vumh@4ax.com>
On Wed, 23 Dec 2009 14:54:17 -0500, "darkon" <darkon.tdo@gmail.com> wrote:
>A general hint that I learned from someone in this group*: use split when
>you know what you want to throw away; capturing regular expressions when you
>know what you want to keep.
No basis whatsoever.
-sln
------------------------------
Date: Wed, 23 Dec 2009 15:51:13 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Trying to split a string on numbers
Message-Id: <slrnhj53vv.h49.tadmc@tadbox.sbcglobal.net>
darkon <darkon.tdo@gmail.com> wrote:
> A general hint that I learned from someone in this group*: use split when
> you know what you want to throw away; capturing regular expressions when you
> know what you want to keep. Looks like you want a capturing regular
> expression.
>
>
> * I don't remember who it was. Probably Tad or Randal, I guess.
I'm sure it was Randal.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Wed, 23 Dec 2009 17:20:30 -0500
From: "darkon" <darkon.tdo@gmail.com>
Subject: Re: Trying to split a string on numbers
Message-Id: <xaOdnUtAwvUzCq_WnZ2dnUVZ_vidnZ2d@supernews.com>
<sln@netherlands.com> wrote in message
news:bp25j5pkmdrstd6hbtbtk12d1h9ch2vumh@4ax.com...
> On Wed, 23 Dec 2009 14:54:17 -0500, "darkon" <darkon.tdo@gmail.com> wrote:
>>A general hint that I learned from someone in this group*: use split when
>>you know what you want to throw away; capturing regular expressions when
>>you
>>know what you want to keep.
>
> No basis whatsoever.
Whatever *that* means....
------------------------------
Date: Wed, 23 Dec 2009 19:38:23 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Trying to split a string on numbers
Message-Id: <slrnhj5h9s.i1p.tadmc@tadbox.sbcglobal.net>
darkon <darkon.tdo@gmail.com> wrote:
><sln@netherlands.com> wrote in message
> news:bp25j5pkmdrstd6hbtbtk12d1h9ch2vumh@4ax.com...
>> On Wed, 23 Dec 2009 14:54:17 -0500, "darkon" <darkon.tdo@gmail.com> wrote:
>>>A general hint that I learned from someone in this group*: use split when
>>>you know what you want to throw away; capturing regular expressions when
>>>you
>>>know what you want to keep.
>>
>> No basis whatsoever.
>
> Whatever *that* means....
Paying attention to sln is a poor use of your time.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
------------------------------
Date: Thu, 24 Dec 2009 03:53:55 -0800 (PST)
From: "sopan.shewale@gmail.com" <sopan.shewale@gmail.com>
Subject: Re: Trying to split a string on numbers
Message-Id: <a5585111-65bd-456d-9a60-91de0a3d1972@r24g2000prf.googlegroups.com>
On Dec 23, 7:45=A0pm, Marc Girod <marc.gi...@gmail.com> wrote:
> On Dec 23, 1:25=A0pm, "sopan.shew...@gmail.com"
>
> <sopan.shew...@gmail.com> wrote:
> > try something like this:
>
> Like this, yes, but not exactly.
> OK for the first 2, but what about the 2 next ones?
>
> for (<DATA>) {
> =A0 print;
> =A0 s/(^.*?)(\d+)/$2/g;
> =A0 print;
>
> }
>
> __DATA__
> JAMES SMITH CA 4995 NW 72ND AVE STE 301
> JANE JONES FL 12 MAIN ST
> BILL CLINTON 2nd MA 1 SIDE ST
> JAMES BOND007 (SECRET AGENT) UK 1001 NIGHTS BLD
>
> Is this better?
>
> =A0 s/^(^.*?\s+)(\d+)\b/$2/;
>
> Marc
Marc - you are right, s/^(^.*?\s+)(\d+)\b/$2/;
is better than what i wrote.
------------------------------
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 2739
***************************************