help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: interpreting ARGV[]


From: Miriam English
Subject: Re: interpreting ARGV[]
Date: Wed, 30 Oct 2024 07:57:57 +1000

Wow! You were quick Davide! Thanks for your reply.

Your SYMTAB[ARGV[1]] solution is perfect!
I would never have thought of it. Thank you.

Cheers,

 - Miriam


On Tue, 29 Oct 2024 22:42:15 +0100
Davide Brini via Help-gawk <help-gawk@gnu.org> wrote:

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



-- 
There are two wolves and they're always fighting.
One is darkness and despair. The other is light and hope.
Which wolf wins?
Whichever one you feed.
  -- Casey in Brad Bird's movie "Tomorrowland"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]