[31195] in Perl-Users-Digest
Perl-Users Digest, Issue: 2440 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 25 00:09:42 2009
Date: Sun, 24 May 2009 21:09:07 -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 Sun, 24 May 2009 Volume: 11 Number: 2440
Today's topics:
Re: &sub qw(1 2); QoS@invalid.net
Arrays and Hashes <someone@somewhere.nb.ca>
Re: Arrays and Hashes <ben@morrow.me.uk>
Re: Arrays and Hashes <someone@somewhere.nb.ca>
Re: Arrays and Hashes <uri@StemSystems.com>
Re: comma operator <tadmc@seesig.invalid>
Re: comma operator <frank@example.invalid>
Re: comma operator <kst-u@mib.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 25 May 2009 03:03:39 GMT
From: QoS@invalid.net
Subject: Re: &sub qw(1 2);
Message-Id: <fUnSl.512$9L2.344@nwrddc02.gnilink.net>
Tad J McClellan <tadmc@seesig.invalid> wrote in message-id: <slrnh1imck.ro6.tadmc@tadmc30.sbcglobal.net>
>
> QoS@invalid.net <QoS@invalid.net> wrote:
>
>
> > How come this works:
> >
> > &ezmenu qw(
> > ++ Action Lookup Cancel -- Exit
> > ++ Edit Cut Copy Paste
> > ++ Logfile View Save -- Clear
> > ++ Options Font Color
> > ++ Help Help About
> > );
> >
> > But this produces syntax error:
> >
> > ezmenu qw(
> > ++ Action Lookup Cancel -- Exit
> > ++ Edit Cut Copy Paste
> > ++ Logfile View Save -- Clear
> > ++ Options Font Color
> > ++ Help Help About
> > );
> >
> > Is this a feature of using & vs invoking a sub without & ?
>
>
> Kinda sorta.
>
> If you want to omit the ampersand and omit parenthesis, then you must
> pay attention to this from perlsub:
>
> NAME LIST; # Parentheses optional if predeclared/imported.
>
> You have omitted the parenthesis but you have NOT predeclared/imported.
>
> Either pre_define_ ezmenu() before you call it:
>
> sub ezmenu {
> # do stuff
> }
> ...
> ezmenu qw( ... );
>
> or, pre_declare_ ezmenu() before you call it:
>
> sub ezmenu;
> ...
> ezmenu qw( ... );
> ...
> sub ezmenu {
> # do stuff
> }
>
> or, don't omit the parenthesis:
>
> ezmenu (qw( ... ));
>
> but in that case, I'd chose some qw delimiter other than parens:
>
> ezmenu (qw/ ... /);
>
>
So there is a way to do this without an ampersand; that is great, thank you.
------------------------------
Date: Sun, 24 May 2009 19:54:49 -0300
From: "Guy" <someone@somewhere.nb.ca>
Subject: Arrays and Hashes
Message-Id: <4a19d029$0$23766$9a566e8b@news.aliant.net>
Is this correct? The type of parenthesis etc?
Guy
@x; Entire Array
%x; Entire Hash
$x[0]; One item of Array
%x{key}; One item of Hash
$r=\@x; Referencing Array variable
$r=\%x; Referencing Hash variable
@$r; De-referecing entire array
De-referecing entire hash ???
$$r[0]; Referencing one array item
$$r{key}; Referencing one hash item
(v1,v2); Anonymous Array (depending on context)
(k1,v1,k2,v2); Anonymous Hash (depending on context)
[v1,v2]; Reference to Anonymous Array
{k1,v1,k2,v2}; Reference to Anonymous Hash
$a=(1,2,3)[0]; Slice of a list
@x=(1,2,3)[0,1]; Slices of a list
($a,$b)=(1,2,3)[0,1]; Slices of a list
@y=@x[0,1]; Slices of array
@x=@y{k1,k2}; Slices of hash (use @ not % because partial hash)
@x=@y{@keys}; Slices of hash (use @ not % because partial hash)
@x{@keys}=@values; Assigning to specific or all keys of hash
$y[0]=\@x; Creating Array of Array (2D Array)
$y[0][0]; Accessing item
$y[0]=\%x; Creating Array of Hash
$y[0]{key}; Accessing item
$y{key}=%x; Creating Hash of Hash
$y{key}{key2}; Accessing item
------------------------------
Date: Mon, 25 May 2009 00:24:20 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Arrays and Hashes
Message-Id: <4i1pe6-qt6.ln1@osiris.mauzo.dyndns.org>
Quoth "Guy" <someone@somewhere.nb.ca>:
> Is this correct? The type of parenthesis etc?
> Guy
>
> @x; Entire Array
> %x; Entire Hash
>
> $x[0]; One item of Array
> %x{key}; One item of Hash
$x{key}
Perl is more consistent than you seem to think. An initial '$' always
indicates a single value, an initial '@' always indicates a list.
> $r=\@x; Referencing Array variable
> $r=\%x; Referencing Hash variable
One would normally say 'Taking a reference to an array'.
> @$r; De-referecing entire array
> De-referecing entire hash ???
%$r
> $$r[0]; Referencing one array item
> $$r{key}; Referencing one hash item
Yes. You simply replace the 'x' part of '$x{key}' with a hashref. If the
hashref is 'too complicated', the normally-optional braces around the
name become required, giving '${$r}{key}'. See perlreftut.
It's usually clearer to use the arrow notation $r->{key}. Again, see
perlreftut.
> (v1,v2); Anonymous Array (depending on context)
> (k1,v1,k2,v2); Anonymous Hash (depending on context)
No. These are both simply lists. Assigning a list to a hash or array
variable is one way of setting its values.
> [v1,v2]; Reference to Anonymous Array
> {k1,v1,k2,v2}; Reference to Anonymous Hash
Yes. Note that what is inside the [] or {} can be any list; so something
like
[ function_returning_list() ]
works perfectly well.
> $a=(1,2,3)[0]; Slice of a list
> @x=(1,2,3)[0,1]; Slices of a list
> ($a,$b)=(1,2,3)[0,1]; Slices of a list
> @y=@x[0,1]; Slices of array
Yes.
> @x=@y{k1,k2}; Slices of hash (use @ not % because partial hash)
> @x=@y{@keys}; Slices of hash (use @ not % because partial hash)
Yes, except '(use @ not $ because the result is a list not a scalar)'.
> @x{@keys}=@values; Assigning to specific or all keys of hash
>
> $y[0]=\@x; Creating Array of Array (2D Array)
Note that here $y[0] will contain a ref to @x, so modifying (say)
$y[0][0] will modify $x[0]. You can copy the array with
$y[0] = [ @x ];
following the rule 'anything that returns a list can go inside the []'.
> $y[0][0]; Accessing item
>
> $y[0]=\%x; Creating Array of Hash
> $y[0]{key}; Accessing item
>
> $y{key}=%x; Creating Hash of Hash
$y{key} = \%x;
> $y{key}{key2}; Accessing item
Which parts of perlreftut were unclear to you?
Ben
------------------------------
Date: Sun, 24 May 2009 21:04:28 -0300
From: "Guy" <someone@somewhere.nb.ca>
Subject: Re: Arrays and Hashes
Message-Id: <4a19e07b$0$23768$9a566e8b@news.aliant.net>
"Ben Morrow" <ben@morrow.me.uk> a écrit dans le message de news:
4i1pe6-qt6.ln1@osiris.mauzo.dyndns.org...
>
> Quoth "Guy" <someone@somewhere.nb.ca>:
>> Is this correct? The type of parenthesis etc?
>> Guy
>>
>> @x; Entire Array
>> %x; Entire Hash
>>
>> $x[0]; One item of Array
>> %x{key}; One item of Hash
>
> $x{key}
>
> Perl is more consistent than you seem to think. An initial '$' always
> indicates a single value, an initial '@' always indicates a list.
>
>> $r=\@x; Referencing Array variable
>> $r=\%x; Referencing Hash variable
>
> One would normally say 'Taking a reference to an array'.
>
>> @$r; De-referecing entire array
>> De-referecing entire hash ???
>
> %$r
>
>> $$r[0]; Referencing one array item
>> $$r{key}; Referencing one hash item
>
> Yes. You simply replace the 'x' part of '$x{key}' with a hashref. If the
> hashref is 'too complicated', the normally-optional braces around the
> name become required, giving '${$r}{key}'. See perlreftut.
>
> It's usually clearer to use the arrow notation $r->{key}. Again, see
> perlreftut.
>
>> (v1,v2); Anonymous Array (depending on context)
>> (k1,v1,k2,v2); Anonymous Hash (depending on context)
>
> No. These are both simply lists. Assigning a list to a hash or array
> variable is one way of setting its values.
>
>> [v1,v2]; Reference to Anonymous Array
>> {k1,v1,k2,v2}; Reference to Anonymous Hash
>
> Yes. Note that what is inside the [] or {} can be any list; so something
> like
>
> [ function_returning_list() ]
>
> works perfectly well.
>
>> $a=(1,2,3)[0]; Slice of a list
>> @x=(1,2,3)[0,1]; Slices of a list
>> ($a,$b)=(1,2,3)[0,1]; Slices of a list
>> @y=@x[0,1]; Slices of array
>
> Yes.
>
>> @x=@y{k1,k2}; Slices of hash (use @ not % because partial hash)
>> @x=@y{@keys}; Slices of hash (use @ not % because partial hash)
>
> Yes, except '(use @ not $ because the result is a list not a scalar)'.
>
>> @x{@keys}=@values; Assigning to specific or all keys of hash
>>
>> $y[0]=\@x; Creating Array of Array (2D Array)
>
> Note that here $y[0] will contain a ref to @x, so modifying (say)
> $y[0][0] will modify $x[0]. You can copy the array with
>
> $y[0] = [ @x ];
>
> following the rule 'anything that returns a list can go inside the []'.
>
>> $y[0][0]; Accessing item
>>
>> $y[0]=\%x; Creating Array of Hash
>> $y[0]{key}; Accessing item
>>
>> $y{key}=%x; Creating Hash of Hash
>
> $y{key} = \%x;
>
>> $y{key}{key2}; Accessing item
>
> Which parts of perlreftut were unclear to you?
>
> Ben
Thanks for the corrections. I just want to print myself a -quick- reference
sheet that I can tape to the wall next to my computer as I try to grasp all
this stuff.
Guy
------------------------------
Date: Sun, 24 May 2009 21:41:04 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Arrays and Hashes
Message-Id: <877i060xjj.fsf@quad.sysarch.com>
>>>>> "G" == Guy <someone@somewhere.nb.ca> writes:
G> Thanks for the corrections. I just want to print myself a -quick-
G> reference sheet that I can tape to the wall next to my computer as
G> I try to grasp all this stuff. Guy
you should have to rip that down very soon hopefully. most of what you
have in there is very basic perl and easy to derive from a few
rules. dereferencing anything is just taking a reference and surrounding
it with ${} (or the correct sigil). you can drop the {} in the case
where the ref is a scalar var. that removes several lines from your
cheat sheet. another it arrays and hashes have very similar syntax and
you use [] for arrays and {} for hashes along with the @ vs %
sigils. that means once you learn how to make anon arrays, anon hashes
follow. same for slices and other things. that removes about 1/3 of the
cheat sheet. similar rules can remove most of the rest. in general it is
better to learn the rules about something rather than a short list of
simple examples. the examples don't explain why things are as they are
nor to they help with more complex examples. rules can be used in all
cases, simple or complex.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sun, 24 May 2009 15:34:13 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: comma operator
Message-Id: <slrnh1jbq5.vtp.tadmc@tadmc30.sbcglobal.net>
["Followup-To:" header set to comp.lang.perl.misc.]
Franken Sense <frank@example.invalid> wrote:
> my ($since, @arts)=time-10*60*60;
[snip other code that has nothing to do with an example of the comma operator]
> I think this is an example of the comma operator that purports to be the
> same as C's.
You think wrong then.
The 2nd paragraph of the docs for Perl's comma operator point out that
the comma above is "just the list argument separator";
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Sun, 24 May 2009 17:08:13 -0700
From: Franken Sense <frank@example.invalid>
Subject: Re: comma operator
Message-Id: <erj1xwnscae0$.7n95t39u06ck.dlg@40tude.net>
In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
> Franken Sense <frank@example.invalid> writes:
>> I wonder whether this is something that has been baffling me for as long as
>> I've been looking at source in C-like languages, as with the following:
>>
>> my ($since, @arts)=time-10*60*60;
>> for (reverse $first..$last) {
>> my %hdr=map /^(\S[^:]+):\s(.*)$/g, @{$nntp->head($_)};
>> defined(my $date=$hdr{'NNTP-Posting-Date'}) or next;
>> defined(my $time=str2time $date)
>> or warn "Couldn't parse date for article $_ ($date)\n"
>> and next;
>> last if $time < $since;
>> unshift @arts, $_;
>> }
>>
>> I think this is an example of the comma operator that purports to be the
>> same as C's. I think it puts RHS into the scalar $since and simply
>> declares @arts as an array.
>
> I see no comma operator in that Perl code. (I see things I would have
> done differently, but I won't get into that in comp.lang.c.)
Alright, well, I'm just going to give up the ghost on this one. The idiom
above was from Michele Dondi. I thought comparing these operators in two
different syntaxes would be illuminating. Now that I've seen C and some
perl with it, I can state one fact: fortran has no comma operators.
>> I'm also still scratching my head with this from n1256.pdf:
>>
>> The left operand of a comma operator is evaluated as a void expression;
>> there is a sequence point after its evaluation. Then the right operand is
>> evaluated; the result has its type and value.
>>
>> Do any of the examples posted show this as a distinction that matters?
>
> What distinction are you referring to? Distinction between what and
> what, exactly?
It's a distinction I wanted to address if I weren't behind in the count by
seven strikes. Andere mal.
Disgression, aka, bugging out, is the better part of valor.
--
Frank
Most of us here in the media are what I call infotainers...Rush Limbaugh is
what I call a disinfotainer. He entertains by spreading disinformation.
~~ Al Franken
------------------------------
Date: Sun, 24 May 2009 16:12:46 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: comma operator
Message-Id: <lnprdy14ep.fsf@nuthaus.mib.org>
Franken Sense <frank@example.invalid> writes:
> In Dread Ink, the Grave Hand of Keith Thompson Did Inscribe:
[...]
>> I see no comma operator in that Perl code. (I see things I would have
>> done differently, but I won't get into that in comp.lang.c.)
>
> Alright, well, I'm just going to give up the ghost on this one. The idiom
> above was from Michele Dondi. I thought comparing these operators in two
> different syntaxes would be illuminating.
Comparing the use of the comma operator in C and Perl might be
illuminating. You just have to compare code that actually uses it.
On the other hand, just looking at the comma operator for one language
should tell you all you need to know about the comma operator in that
language. Its use is very similar in C and in Perl. (But Perl has so
many contexts in which the comma means something else that it's
probably not as common as in C.)
[...]
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
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 2440
***************************************