[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: interpreting ARGV[]
From: |
Davide Brini |
Subject: |
Re: interpreting ARGV[] |
Date: |
Tue, 29 Oct 2024 22:42:15 +0100 |
On Wed, 30 Oct 2024 07:19:20 +1000, Miriam English <mim@miriam-english.org>
wrote:
> Hi folks,
>
> I'm reading some colors into an awk program on the commandline using
> ARGV[]. I also have colors defined by their names in a library file
> function that consists of just a long list of color definitions
> reformatted from the one at /usr/share/X11/rgb.txt and written like
> this:
>
> red="255 0 0"
> white="255 255 255"
> black="0 0 0"
> ...
>
> I want the user to be able to input number values, like "255 0 0" or
> color names like "red". The problem is, when I get the name "red"
> through ARGV[] I can't find a way to reinterpret it as the number value.
> It seems like there should be an obvious and simple way to do that, but
> I can't see it. Can anybody enlighten me?
I'm not sure I understand correctly, but if you load the colors beforehand
into an associative array (eg in the BEGIN block), then you can check
whether the color exists:
if (ARGV[1] in colors) { do something }
or get its definition:
def = colors[ARGV[1]]
If, on the other hand, you already have n actual variables (n being the
number of colors), then you might access the variable indirectly
using the special SYMTAB array (a gawk extension), for example:
if (ARGV[1] in SYMTAB) { do something }
def = SYMTAB[ARGV[1]]
--
D.