slicing syntax question

A

AC

Why does this fail with a syntax error

my @items = qw(a b c d e);
my @hash{@items} = 0..$#items;

but this does not?

my @items = qw(a b c d e);
@hash{@items} = 0..$#items;

I really want to use fewer lines in my code and I'd hate to use one line to
declare the hash and one line to initialize it.

Many Thanks,

Allan
 
G

Gunnar Hjalmarsson

AC said:
Why does this fail with a syntax error

my @items = qw(a b c d e);
my @hash{@items} = 0..$#items;

but this does not?

my @items = qw(a b c d e);
@hash{@items} = 0..$#items;

Because hashes in Perl are declared with %, not @.
I really want to use fewer lines in my code and I'd hate to use one line to
declare the hash and one line to initialize it.

my %hash = map { $items[$_], $_ } 0..$#items;
 
C

Chris Mattern

AC said:
Why does this fail with a syntax error

my @items = qw(a b c d e);
my @hash{@items} = 0..$#items;

Because my sees "@" and thinks you're declaring an array.
Then the parser runs into the syntax for a hash slice
and gives up in confusion.
but this does not?

my @items = qw(a b c d e);
@hash{@items} = 0..$#items;

Because by taking out my, you remove the syntax error.
I really want to use fewer lines in my code and I'd hate to use one line
to declare the hash and one line to initialize it.

'Fraid you need to.
Many Thanks,

Allan

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
S

Steven Kuo

Why does this fail with a syntax error

my @items = qw(a b c d e);
my @hash{@items} = 0..$#items;

but this does not?

my @items = qw(a b c d e);
@hash{@items} = 0..$#items;

I really want to use fewer lines in my code and I'd hate to use one line to
declare the hash and one line to initialize it.



You can combine the declaration of the array and hash on one line:

my (@items, %hash) = qw(a b c d e);
@hash{@items} = 0..$#items;

Looks ugly to me, however.
 
R

robic0

"I really want to use fewer lines in my code ......"

Why is that... do you think fewer lines makes your code run more
effeciently.
You may think its terrible to make a mistake in front of syntax gods,
but I
assure you that content, clarity and logic ... in any language, is much
more important.
 
A

AC

"I really want to use fewer lines in my code ......"

Why is that... do you think fewer lines makes your code run more
effeciently.
You may think its terrible to make a mistake in front of syntax gods,
but I
assure you that content, clarity and logic ... in any language, is much
more important.

Understood. I was actually thinking that there would be an idiom so that
clarity would be assumed.

Allan
 
X

xhoster

AC said:
Why does this fail with a syntax error

my @items = qw(a b c d e);
my @hash{@items} = 0..$#items;

but this does not?

my @items = qw(a b c d e);
@hash{@items} = 0..$#items;

Because one has a syntax error and the other does not. It would be nice
if perl allowed you to declare a hash while using it in a slice,
but sadly it does not.
I really want to use fewer lines in my code and I'd hate to use one line
to declare the hash and one line to initialize it.

newlines are optional almost everywhere in Perl.

my %hash; @hash{@items}= 0..$#items;

Cheers,

Xho
 
X

xhoster

"I really want to use fewer lines in my code ......"

Why is that... do you think fewer lines makes your code run more
effeciently.

No, but it can make maintaining the code more efficient. It I can see an
entire logical unit of the program on the screen without have to keep
scolling up and down, that makes things a lot easier. Wise use of white
space, and wise non-use of white-space, both contribute to good coding
style.

Xho
 
R

robic0

No, but it can make maintaining the code more efficient. It I can see an
entire logical unit of the program on the screen without have to keep
scolling up and down, that makes things a lot easier. Wise use of white
space, and wise non-use of white-space, both contribute to good coding
style.

Xho

No, actually thats not true at all !! If you want to limit the
programs you write to less than a screen, you might find
that your boss will get upset after 6 months of trying to
write a single screen of code when there should be
10,000 lines by then ....
-rfc
 
M

Matija Papec

No, actually thats not true at all !! If you want to limit the
programs you write to less than a screen, you might find
that your boss will get upset after 6 months of trying to
write a single screen of code when there should be
10,000 lines by then ....

Such boss shouldn't last long, at least not in company which actually want
to produce something.
 
A

Anno Siegel

[...]
my @items = qw(a b c d e);
@hash{@items} = 0..$#items;
I really want to use fewer lines in my code and I'd hate to use one line
to declare the hash and one line to initialize it.

newlines are optional almost everywhere in Perl.

my %hash; @hash{@items}= 0..$#items;

Ah, but that's only cosmetics :)

my %hash = map { $items[ $_] => $_ } 0 .. $#items;

Anno
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top