slicing syntax question

Discussion in 'Perl Misc' started by AC, Mar 24, 2005.

  1. AC

    AC Guest

    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
    AC, Mar 24, 2005
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Mar 24, 2005
    #2
    1. Advertising

  3. 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?"
    Chris Mattern, Mar 24, 2005
    #3
  4. AC

    Steven Kuo Guest

    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
    Steven Kuo, Mar 24, 2005
    #4
  5. AC

    Guest

    "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.
    , Mar 25, 2005
    #5
  6. AC

    AC Guest

    <> wrote in message
    news:...
    > "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
    AC, Mar 25, 2005
    #6
  7. <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 25, 2005
    #7
  8. AC

    Guest

    "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
    , Mar 25, 2005
    #8
  9. AC

    Guest

    "" <> 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
    , Mar 25, 2005
    #9
  10. AC

    Guest

    On 25 Mar 2005 17:47:38 GMT, wrote:

    >"" <> 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
    , Apr 1, 2005
    #10
  11. AC

    Matija Papec Guest

    On Fri, 01 Apr 2005 02:46:01 -0800, wrote:

    > 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.
    Matija Papec, Apr 1, 2005
    #11
  12. AC

    Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > "AC" <clarke@n_o_s_p_a_m_hyperformix.com> wrote:


    [...]

    > > 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
    Anno Siegel, Apr 1, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tero Kapanen

    Slicing of an array: wrong direction

    Tero Kapanen, Nov 4, 2003, in forum: VHDL
    Replies:
    11
    Views:
    7,212
    Allan Herriman
    Nov 7, 2003
  2. Replies:
    5
    Views:
    524
  3. John Salerno

    question about slicing with a step length

    John Salerno, Mar 9, 2006, in forum: Python
    Replies:
    11
    Views:
    397
    Terry Reedy
    Mar 10, 2006
  4. Replies:
    1
    Views:
    251
    Fredrik Lundh
    Apr 6, 2006
  5. , India
    Replies:
    2
    Views:
    264
    Daniel T.
    Nov 5, 2007
Loading...

Share This Page