in reply to Re: Using exists to check element in array
in thread Using exists to check element in array

Thanks @Rolf. I did change to using a hash, which works fine, it is just slightly messier when it comes to string concatenation because of the double quotes I find I need to put around the keys.

Thanks for link, I had a read.
  • Comment on Re^2: Using exists to check element in array

Replies are listed 'Best First'.
Re^3: Using exists to check element in array (Unquoted Hash Keys)
by LanX (Saint) on Jan 28, 2024 at 10:18 UTC
    > messier when it comes to string concatenation because of the double quotes I find I need to put around the keys.

    In Perl keys are auto-quoted!

    I.e. this "txt $h{key} txt" works fine.

    You are also free to use hash-slices for multiple values, like "txt $h{@keys} txt"

    update

    From Learning Perl

      Unquoted Hash Keys

      Perl offers many shortcuts that can help the programmer, such as omitting the quote marks on some hash keys.

      You can’t omit the quote marks on every key since a hash key may be any arbitrary string. But keys are often simple. If the hash key only consists of letters, digits, and underscores without starting with a digit, you may be able to omit the quote marks. This kind of simple string without quote marks is called a bareword since it stands alone without quotes.

      One place you are permitted to use this shortcut is the most common place a hash key appears, which is in the curly braces of a hash element reference. For example, instead of $score{"fred"}, you could write $score{fred}. Since many hash keys are like this, not using quotes is a convenience. But beware: if there’s anything inside the curly braces besides a bareword, Perl will interpret it as an expression.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Thanks, I re-checked and the double quotes were an error!!! Glad you inspired me go back and check and find this. Doh! Thanks. Much easier to read now too!