Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > slicing syntax question

Reply
Thread Tools

slicing syntax question

 
 
AC
Guest
Posts: n/a
 
      03-24-2005
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



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      03-24-2005
AC wrote:
> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Chris Mattern
Guest
Posts: n/a
 
      03-24-2005
AC wrote:

> 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?"
 
Reply With Quote
 
Steven Kuo
Guest
Posts: n/a
 
      03-24-2005
On Thu, 24 Mar 2005, AC wrote:

> 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.

--
Hope this helps,
Steven


 
Reply With Quote
 
robic0@yahoo.com
Guest
Posts: n/a
 
      03-25-2005
"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.

 
Reply With Quote
 
AC
Guest
Posts: n/a
 
      03-25-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> "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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      03-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:

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



Do you think that questions end with a question mark?


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-25-2005
"AC" <clarke@n_o_s_p_a_m_hyperformix.com> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-25-2005
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
> "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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
robic0@yahoo.com
Guest
Posts: n/a
 
      04-01-2005
On 25 Mar 2005 17:47:38 GMT, (E-Mail Removed) wrote:

>"(E-Mail Removed)" <(E-Mail Removed)> wrote:
>> "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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner's question on slicing subramanian100in@yahoo.com, India C++ 2 11-05-2007 01:33 PM
slicing question: rawdata[j:j+1] == 'xx' #will this ever be true ?? richard.hsu@gmail.com Python 1 04-06-2006 05:27 AM
question about slicing with a step length John Salerno Python 11 03-10-2006 01:42 AM
Vector Slicing in assigments nrivera.eng@gmail.com VHDL 5 08-10-2005 08:39 AM
Slicing of an array: wrong direction Tero Kapanen VHDL 11 11-07-2003 02:03 AM



Advertisments