Combine hash declaration/assignment into single statement?

T

Tassilo v. Parseval

Also sprach Anno Siegel:
use warnings;
use strict;
use List::MoreUtils qw(zip);

my %character_value = zip('a'..'z', 1..26);

But as it turns out, there is a subtle distinction between lists and
arrays of which I was not previously aware, and List::MoreUtils::zip
needs arrays:

Then give it arrays:

my %character_value = zip @{ [ 'a' .. 'z'] }, @{ [ 1 .. 26] };

or, overriding the prototype:

my %character_value = &zip( [ 'a' .. 'z'], [ 1 .. 26]);

That's clever. But maybe there also should be a zipref() function
(similar to each_array/each_arrayref) without the prototype that expects
explicit array-references.

Tassilo
 
U

Uri Guttman

TvP> Also sprach Uri Guttman:

TvP> 'scuse me? List::MoreUtils has no dependencies whatsoever:

TvP> and I took great effort that both the XS- and the
TvP> Perl-implementation of its functions work on anything between
TvP> 5.005_04 and 5.9.4.

TvP> Check your CPAN setup. It's borked. :)

i said that was probably what happened. but that was also running the
cpan cli which i rarely do. i either download and install by hand or do
a perl -MCPAN. i couldn't imagine why moreutils would want sax!

uri
 
A

Anno Siegel

Tassilo v. Parseval said:
Also sprach Anno Siegel:
use warnings;
use strict;
use List::MoreUtils qw(zip);

my %character_value = zip('a'..'z', 1..26);

But as it turns out, there is a subtle distinction between lists and
arrays of which I was not previously aware, and List::MoreUtils::zip
needs arrays:

Then give it arrays:

my %character_value = zip @{ [ 'a' .. 'z'] }, @{ [ 1 .. 26] };

or, overriding the prototype:

my %character_value = &zip( [ 'a' .. 'z'], [ 1 .. 26]);

That's clever. But maybe there also should be a zipref() function
(similar to each_array/each_arrayref) without the prototype that expects
explicit array-references.

Maybe. In fact, in the case of List::MoreUtils I'm inclined to agree,
though it raises the question whether zip()s alias mesh() will follow
suit and have a meshref() too. Functions from List::MoreUtils are
relatively high level, and the user is supposed to call them all over
the source. I wouldn't want to force the user to use ampersand-decorated
calls routinely.

I have a similar case with a lower-level module I'm planning. I expect
the typical user to call a prototyped routine only once in a routine
of their own and then use *that*. In this case I'm going to advise
the user to use "&" if the prototype gets in the way.

I find these design decisions inordinately hard and time-consuming.

Anno
 
T

Tassilo v. Parseval

Also sprach Anno Siegel:
Tassilo v. Parseval said:
Also sprach Anno Siegel:
Then give it arrays:

my %character_value = zip @{ [ 'a' .. 'z'] }, @{ [ 1 .. 26] };

or, overriding the prototype:

my %character_value = &zip( [ 'a' .. 'z'], [ 1 .. 26]);

That's clever. But maybe there also should be a zipref() function
(similar to each_array/each_arrayref) without the prototype that expects
explicit array-references.

Maybe. In fact, in the case of List::MoreUtils I'm inclined to agree,
though it raises the question whether zip()s alias mesh() will follow
suit and have a meshref() too. Functions from List::MoreUtils are
relatively high level, and the user is supposed to call them all over
the source. I wouldn't want to force the user to use ampersand-decorated
calls routinely.

There is probably a bloat-threshold. L::MU already has quite a few
aliases and looking back, I probably should have done without them.
Along with the ref version, the same function exists virtually four
times.
I have a similar case with a lower-level module I'm planning. I expect
the typical user to call a prototyped routine only once in a routine
of their own and then use *that*. In this case I'm going to advise
the user to use "&" if the prototype gets in the way.

The ampersand has one other drawback: Someone reviewing code and seeing
the ampersands could come to the conclusion they are not necesssary as
they aren't often enough. For something that is only called once this is
not such a problem as one can easily add a comment stating that this is
a vital ampersand.
I find these design decisions inordinately hard and time-consuming.

In my experience one needs to see how an interface behaves in the wild
before making a judgement. Over the course of the last months where I
was fixing and modifying Event::Lib with a company where it is used,
tweaks to the interface turned out to be a good idea. Sometimes it's
something as trivial as renaming a method as it happened to me when I
had to change del() into remove(). del() looked fine to me most of the
time, but in certain code-contexts it was indeed misleading.

So a programmer can really only work by best effort. Once a piece of
software is out, it may take another year or even two to smooth out some
of the rough edges it will undoubtedly have.

Tassilo
 
D

Dr.Ruud

Anno Siegel schreef:
I expect
the typical user to call a prototyped routine only once in a routine
of their own and then use *that*. In this case I'm going to advise
the user to use "&" if the prototype gets in the way.

Can't you create a wrapper-sub, named something like othername_init(),
to do that?
 
R

Randal L. Schwartz

usenet> It is often possible to declare and assign a variable in a single
usenet> statement, such as:
usenet> my $foo = 'bar';
usenet> or
usenet> my @foo = qw{bar baz};

usenet> Is it possible to do this in a single statement (under strict):

usenet> my %character_value;
usenet> @character_value{'a'..'z'} = (1..26);

@$_{'a'..'z'} = (1..26) for \my %character_value;

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
 
A

Anno Siegel

Dr.Ruud said:
Anno Siegel schreef:


Can't you create a wrapper-sub, named something like othername_init(),
to do that?

Yes, I can. The hard part is to decide if I should.

Anno
 
J

Josh Smith

Randal said:
print "Just another Perl hacker,"; # the original

NO, you asshole. Larry Wall is the original Perl hacker. He invented
the fucking language!

You, on the other hand, are just another convicted felon, convicted of
knowingly accessing and using a computer and computer network for the
purpose of committing theft.
 
A

A. Sinan Unur

Josh Smith said:
Randal said:
print "Just another Perl hacker,"; # the original

NO, you asshole. Larry Wall is the original Perl hacker. He invented
the fucking language!

You, on the other hand, are just another ... [drivel]

You, on the other hand, are being crushed under the unbearable
insignificance of your existence, and the fact that you have
never ever had the self-confidence to make a stand on anything.

Pathetic, and worthy of our sympathy, really.

Sinan

PS: You are still using North Carolina Research and Education
Network's resources to access the internet, and are still bound
by their terms of use:

http://www.ncren.net/index.cfm?fuseaction=page&filename=ncren_acceptable_use.html
 
M

Matt Garrish

A. Sinan Unur said:
Randal said:
print "Just another Perl hacker,"; # the original

NO, you asshole. Larry Wall is the original Perl hacker. He invented
the fucking language!

You, on the other hand, are just another ... [drivel]

You, on the other hand, are being crushed under the unbearable
insignificance of your existence, and the fact that you have
never ever had the self-confidence to make a stand on anything.

Pathetic, and worthy of our sympathy, really.

Not to mention completely wrong. Larry Wall did not originate the JAPH. He
probably forgot it's medication first then usenet...

Matt
 
M

Matt Garrish

Josh Smith said:
NO, you asshole. Larry Wall is the original Perl hacker. He invented
the fucking language!

No, the language of love existed long before Larry...

Matt
 
J

John Bokma

Sinan & Matt (maybe others, I skip this group more then I read it):

Kind request (I do my best to follow my own request as well)

ignore posts from Josh, Roger & co.

Thanks,
 
M

Matt Garrish

John Bokma said:
Sinan & Matt (maybe others, I skip this group more then I read it):

Kind request (I do my best to follow my own request as well)

ignore posts from Josh, Roger & co.

But where would be the fun in that? : P

Don't worry, I've had my fill of these vacuous trolls (I was home sick today
and needed something to do!), so I'll drop the noise level...

Matt
 
J

John Bokma

Matt Garrish said:
But where would be the fun in that? : P

No idea. I am now and then tempted as well, but if I add up all those
attempts at kicking at people who just are here (or there) because they
like the attention, it's a lot. It worries me. I probably should write
something nice and put on my site :)
Don't worry, I've had my fill of these vacuous trolls (I was home sick
today and needed something to do!), so I'll drop the noise level...

Thanks :-D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top