I was replying to jettero who said "substr doesn't actually modify the string like splice modifies arrays" and there was no mention of hash keys in his post.

I know. I just related your answer to the topic. Out of context answers are dangerous.

And actually, hash keys are strings

I never said they weren't. I said they weren't Perl strings. They're not C strings either. They're C strings with a flag byte appended.

struct hek { U32 hek_hash; /* hash of key */ I32 hek_len; /* length of hash key */ char hek_key[1]; /* variable-length hash key */ /* the hash-key is \0-terminated */ /* after the \0 there is a byte for flags, such as whether the key is UTF-8 */ }; typedef struct hek HEK;

hash keys are strings, just not modifiable strings, but there is no error if you try to modify them:

Not so. What you call a hash key is modifiable.

use warnings; use strict; my %x = qw/abcd efgh ijkl mnop/; for (%x) { substr $_, 0, 2, ""; print("$_\n"); }
kl op cd gh

That's because it's a copy of the hash key, not the hash key itself.


In reply to Re^6: Using substr on Hash Keys by ikegami
in thread Using substr on Hash Keys by pc0019

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.