Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > To use Perl like conditional assignment in C

Reply
Thread Tools

To use Perl like conditional assignment in C

 
 
Ya Shou
Guest
Posts: n/a
 
      09-24-2011
Hello,


I want to assign an uncertain value to a variable, this variable with
values in the configuration file, reads the configuration file
through a function to return to the read configuration value, if not
configured or the value of non-compliance, will return 0 for failure,
when this, I will to give it a default value assigned.

However, I found it seem impossible to achieve this in C, but much
easier to implement in Perl, see below:

cfgval = loadConfig () || DEFAULT_VAL;

In C '||' operator does not return the value of the expression which
is true, but returns 1 or 0, so that I can not be used in a similar
way. I considered a program in C is:

cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;

And however, this approach to involve a duplication of code, it is
ugly, the other will call loadConfig function twice, inefficient and
likely to cause unnecessary accidents.

Of course, if / else statement can achieve this functionality, but
this method requires an additional variable, and not look simple. As
follows:

int loadval = loadConfig ();
if (loadval! = 0) {
cfgval = loadval;
} else {
cfgval = DEFAULT_VAL;
}

Or

int loadval;
cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;


I would like to ask you is, in this case, there is nothing more
beautiful realization?


Thanks.

 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      09-24-2011
Ya Shou wrote:
) Hello,
)
)
) I want to assign an uncertain value to a variable, this variable with
) values in the configuration file, reads the configuration file
) through a function to return to the read configuration value, if not
) configured or the value of non-compliance, will return 0 for failure,
) when this, I will to give it a default value assigned.
)
) However, I found it seem impossible to achieve this in C, but much
) easier to implement in Perl, see below:
)
) cfgval = loadConfig () || DEFAULT_VAL;
)
) In C '||' operator does not return the value of the expression which
) is true, but returns 1 or 0, so that I can not be used in a similar
) way. I considered a program in C is:
)
) cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
)
) And however, this approach to involve a duplication of code, it is
) ugly, the other will call loadConfig function twice, inefficient and
) likely to cause unnecessary accidents.
)
) Of course, if / else statement can achieve this functionality, but
) this method requires an additional variable, and not look simple. As
) follows:
)
) int loadval = loadConfig ();
) if (loadval! = 0) {
) cfgval = loadval;
) } else {
) cfgval = DEFAULT_VAL;
) }
)
) Or
)
) int loadval;
) cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
)
)
) I would like to ask you is, in this case, there is nothing more
) beautiful realization?

Why can't you assign to cfgval twice?

if (!(cfgval = loadConfig()))
cfgval = DEFAULT_VAL;

Or if you want to mimic the Perl style:

(cfgval = loadConfig()) || (cfgval = DEFAULT_VAL);

But that will be deemed very ugly by most C programmers.

IIRC, some compilers have an extension which allows this:

cfgval = loadConfig() ?: DEFAULT_VAL;

But that is not standard C, as far as I know.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      09-24-2011
On 9/24/2011 7:06 AM, Ya Shou wrote:
> Hello,
>
>
> I want to assign an uncertain value to a variable, this variable with
> values in the configuration file, reads the configuration file
> through a function to return to the read configuration value, if not
> configured or the value of non-compliance, will return 0 for failure,
> when this, I will to give it a default value assigned.
> [...]


Others have suggested ways to write this. But is it a good
idea? It leaves you with no way for the configuration file to
supply zero as an actual value, which may be satisfactory now
but could become troublesome later.

"In-band" signalling has a long history, and you'll find it
used a lot in the C library and in C programs. The malloc()
function returns NULL to indicate a special case, and this works
because NULL is easily distinguishable from any valid pointer.
strchr(), getenv(), and many others use NULL in a similar way.
But with functions returning other types it's not always so easy
to choose an "impossible" value. time() and several other time-
related functions return -1 when something's wrong, but on most
systems the value -1 could easily be understood as a perfectly
valid time, often 1969-12-31 23:59:59. fgetc() returns the value
EOF, which is supposed to be different from all actual character
values -- but this makes trouble on systems with wide characters
and narrow integers, or on ones' complement or signed magnitude
systems where `char' is signed. strtol() uses three different
"impossible" values, *all* of them perfectly legitimate!

In short, using a special value to mean "absent" or "invalid"
may seem convenient, but can be less convenient than it appears
at first. Instead of

cfgval = loadConfig();
if (cfgval == SPECIAL_VALUE)
cfgval = DEFAULT_VALUE;

consider something more like

if (!loadConfig(&cfgval))
cfgval = DEFAULT_VALUE;

where loadConfig() tries to load the value and store it at the
indicated place, and returns 1 or 0 to indicate success or failure.
Or, if you prefer, use

cfgval = loadConfig(DEFAULT_VALUE);

where loadConfig() loads and returns the value from the configuration
file, or returns its own argument if unable to load anything. You
could even put DEFAULT_VALUE into loadConfig() itself, producing

cfgval = loadConfig();

where loadConfig() loads and returns the file's value if it can, or
returns DEFAULT_VALUE if unable.

But think long and hard and critically before using SPECIAL_VALUE.
It can become a snare and a delusion.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      09-24-2011
Eric Sosman wrote:
) Or, if you prefer, use
)
) cfgval = loadConfig(DEFAULT_VALUE);
)
) where loadConfig() loads and returns the value from the configuration
) file, or returns its own argument if unable to load anything. You
) could even put DEFAULT_VALUE into loadConfig() itself, producing
)
) cfgval = loadConfig();
)
) where loadConfig() loads and returns the file's value if it can, or
) returns DEFAULT_VALUE if unable.

I prefer the first, because of the more real-world generic version:
cfgval = loadConfig('foo_num', FOO_DEFAULT_VALUE);

Which I've used plenty of times.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      09-25-2011
On 9/24/2011 4:48 AM, pete wrote:
> Ya Shou wrote:
>>
>> Hello,
>>
>> I want to assign an uncertain value to a variable, this variable with
>> values in the configuration file, reads the configuration file
>> through a function to return to the read configuration value, if not
>> configured or the value of non-compliance, will return 0 for failure,
>> when this, I will to give it a default value assigned.
>>
>> However, I found it seem impossible to achieve this in C, but much
>> easier to implement in Perl, see below:
>>
>> cfgval = loadConfig () || DEFAULT_VAL;
>>
>> In C '||' operator does not return the value of the expression which
>> is true, but returns 1 or 0, so that I can not be used in a similar
>> way. I considered a program in C is:
>>
>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>>
>> And however, this approach to involve a duplication of code, it is
>> ugly, the other will call loadConfig function twice, inefficient and
>> likely to cause unnecessary accidents.
>>
>> Of course, if / else statement can achieve this functionality, but
>> this method requires an additional variable, and not look simple. As
>> follows:
>>
>> int loadval = loadConfig ();
>> if (loadval! = 0) {
>> cfgval = loadval;
>> } else {
>> cfgval = DEFAULT_VAL;
>> }
>>
>> Or
>>
>> int loadval;
>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>>
>> I would like to ask you is, in this case, there is nothing more
>> beautiful realization?

>
> cfgval = loadConfig ();
> if (cfgval == 0) {
> cfgval = DEFAULT_VAL;
> }
>


pete,

you just posted another syntax other than c tonight in clc; watch it,
there, revolutionary.
--
Uno
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      09-25-2011
On Sep 25, 5:58*am, Uno <(E-Mail Removed)> wrote:
> On 9/24/2011 4:48 AM, pete wrote:
>
>
>
>
>
> > Ya Shou wrote:

>
> >> Hello,

>
> >> I want to assign an uncertain value to a variable, this variable with
> >> values in the configuration file, reads the configuration file
> >> through a function to return to the read configuration value, if not
> >> configured or the value of non-compliance, will return 0 for failure,
> >> when this, I will to give it a default value assigned.

>
> >> However, I found it seem impossible to achieve this in C, but much
> >> easier to implement in Perl, see below:

>
> >> cfgval = loadConfig () || DEFAULT_VAL;

>
> >> In C '||' operator does not return the value of the expression which
> >> is true, but returns 1 or 0, so that I can not be used in a similar
> >> way. I considered a program in C is:

>
> >> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;

>
> >> And however, this approach to involve a duplication of code, it is
> >> ugly, the other will call loadConfig function twice, inefficient and
> >> likely to cause unnecessary accidents.

>
> >> Of course, if / else statement can achieve this functionality, but
> >> this method requires an additional variable, and not look simple. As
> >> follows:

>
> >> int loadval = loadConfig ();
> >> if (loadval! = 0) {
> >> * * cfgval = loadval;
> >> } else {
> >> * * cfgval = DEFAULT_VAL;
> >> }

>
> >> Or

>
> >> int loadval;
> >> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;

>
> >> I would like to ask you is, in this case, there is nothing more
> >> beautiful realization?

>
> > * * *cfgval = loadConfig ();
> > * * *if (cfgval == 0) {
> > * * * * *cfgval = DEFAULT_VAL;
> > * * *}

>
> pete,
>
> you just posted another syntax other than c tonight in clc; watch it,
> there, revolutionary.


in what way is that not C syntax?
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      09-26-2011
"Ya Shou" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> However, I found it seem impossible to achieve this in C, but much
> easier to implement in Perl, see below:
>
> cfgval = loadConfig () || DEFAULT_VAL;


> I would like to ask you is, in this case, there is nothing more
> beautiful realization?


Can you change loadConfig()? If so, just do:

cfgval = loadConfig(DEFAULT_VAL);

If you can't change it, use a wrapper:

cfgval = your_loadConfig(DEFAULT_VAL);

or just:

cfgval = your_loadConfig();

--
Bartc

 
Reply With Quote
 
J. J. Farrell
Guest
Posts: n/a
 
      09-26-2011
Uno wrote:
> On 9/24/2011 4:48 AM, pete wrote:
>> Ya Shou wrote:
>>>
>>> Hello,
>>>
>>> I want to assign an uncertain value to a variable, this variable with
>>> values in the configuration file, reads the configuration file
>>> through a function to return to the read configuration value, if not
>>> configured or the value of non-compliance, will return 0 for failure,
>>> when this, I will to give it a default value assigned.
>>>
>>> However, I found it seem impossible to achieve this in C, but much
>>> easier to implement in Perl, see below:
>>>
>>> cfgval = loadConfig () || DEFAULT_VAL;
>>>
>>> In C '||' operator does not return the value of the expression which
>>> is true, but returns 1 or 0, so that I can not be used in a similar
>>> way. I considered a program in C is:
>>>
>>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>>>
>>> And however, this approach to involve a duplication of code, it is
>>> ugly, the other will call loadConfig function twice, inefficient and
>>> likely to cause unnecessary accidents.
>>>
>>> Of course, if / else statement can achieve this functionality, but
>>> this method requires an additional variable, and not look simple. As
>>> follows:
>>>
>>> int loadval = loadConfig ();
>>> if (loadval! = 0) {
>>> cfgval = loadval;
>>> } else {
>>> cfgval = DEFAULT_VAL;
>>> }
>>>
>>> Or
>>>
>>> int loadval;
>>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>>>
>>> I would like to ask you is, in this case, there is nothing more
>>> beautiful realization?

>>
>> cfgval = loadConfig ();
>> if (cfgval == 0) {
>> cfgval = DEFAULT_VAL;
>> }
>>

>
> pete,
>
> you just posted another syntax other than c tonight in clc; watch it,
> there, revolutionary.


Where? When?
 
Reply With Quote
 
Ya Shou
Guest
Posts: n/a
 
      09-27-2011
On Sep 24, 9:04*pm, Eric Sosman <(E-Mail Removed)> wrote:
> On 9/24/2011 7:06 AM, Ya Shou wrote:
>
> > Hello,

>
> > I want to assign an uncertain value to a variable, this variable with
> > values in the configuration file, reads the configuration file
> > through a function to return to the read configuration value, if not
> > configured or the value of non-compliance, will return 0 for failure,
> > when this, I will to give it a default value assigned.
> > [...]

>
> * * *Others have suggested ways to write this. *But is it a good
> idea? *It leaves you with no way for the configuration file to
> supply zero as an actual value, which may be satisfactory now
> but could become troublesome later.
>
> * * *"In-band" signalling has a long history, and you'll find it
> used a lot in the C library and in C programs. *The malloc()
> function returns NULL to indicate a special case, and this works
> because NULL is easily distinguishable from any valid pointer.
> strchr(), getenv(), and many others use NULL in a similar way.
> But with functions returning other types it's not always so easy
> to choose an "impossible" value. *time() and several other time-
> related functions return -1 when something's wrong, but on most
> systems the value -1 could easily be understood as a perfectly
> valid time, often 1969-12-31 23:59:59. *fgetc() returns the value
> EOF, which is supposed to be different from all actual character
> values -- but this makes trouble on systems with wide characters
> and narrow integers, or on ones' complement or signed magnitude
> systems where `char' is signed. *strtol() uses three different
> "impossible" values, *all* of them perfectly legitimate!
>
> * * *In short, using a special value to mean "absent" or "invalid"
> may seem convenient, but can be less convenient than it appears
> at first. *Instead of
>
> * * * * cfgval = loadConfig();
> * * * * if (cfgval == SPECIAL_VALUE)
> * * * * * * cfgval = DEFAULT_VALUE;


This is a better way to do that, but twice assignment maybe
superfluous.

>
> consider something more like
>
> * * * * if (!loadConfig(&cfgval))
> * * * * * * cfgval = DEFAULT_VALUE;
>


I think that is a perfect solution. beautiful and so efficient.

> where loadConfig() tries to load the value and store it at the
> indicated place, and returns 1 or 0 to indicate success or failure.
> Or, if you prefer, use
>
> * * * * cfgval = loadConfig(DEFAULT_VALUE);
>
> where loadConfig() loads and returns the value from the configuration
> file, or returns its own argument if unable to load anything. *You
> could even put DEFAULT_VALUE into loadConfig() itself, producing
>
> * * * * cfgval = loadConfig();
>
> where loadConfig() loads and returns the file's value if it can, or
> returns DEFAULT_VALUE if unable.
>
> * * *But think long and hard and critically before using SPECIAL_VALUE.
> It can become a snare and a delusion.
>
> --
> Eric Sosman
> (E-Mail Removed)

 
Reply With Quote
 
Uno
Guest
Posts: n/a
 
      09-27-2011
On 9/25/2011 5:44 AM, pete wrote:
> Uno wrote:
>>
>> On 9/24/2011 4:48 AM, pete wrote:
>>> Ya Shou wrote:
>>>>
>>>> Hello,
>>>>
>>>> I want to assign an uncertain value to a variable, this variable with
>>>> values in the configuration file, reads the configuration file
>>>> through a function to return to the read configuration value, if not
>>>> configured or the value of non-compliance, will return 0 for failure,
>>>> when this, I will to give it a default value assigned.
>>>>
>>>> However, I found it seem impossible to achieve this in C, but much
>>>> easier to implement in Perl, see below:
>>>>
>>>> cfgval = loadConfig () || DEFAULT_VAL;
>>>>
>>>> In C '||' operator does not return the value of the expression which
>>>> is true, but returns 1 or 0, so that I can not be used in a similar
>>>> way. I considered a program in C is:
>>>>
>>>> cfgval = loadConfig ()? loadConfig (): DEFAULT_VAL;
>>>>
>>>> And however, this approach to involve a duplication of code, it is
>>>> ugly, the other will call loadConfig function twice, inefficient and
>>>> likely to cause unnecessary accidents.
>>>>
>>>> Of course, if / else statement can achieve this functionality, but
>>>> this method requires an additional variable, and not look simple. As
>>>> follows:
>>>>
>>>> int loadval = loadConfig ();
>>>> if (loadval! = 0) {
>>>> cfgval = loadval;
>>>> } else {
>>>> cfgval = DEFAULT_VAL;
>>>> }
>>>>
>>>> Or
>>>>
>>>> int loadval;
>>>> cfgval = (loadval! = 0)? loadval: DEFAULT_VAL;
>>>>
>>>> I would like to ask you is, in this case, there is nothing more
>>>> beautiful realization?
>>>
>>> cfgval = loadConfig ();
>>> if (cfgval == 0) {
>>> cfgval = DEFAULT_VAL;
>>> }
>>>

>>
>> pete,
>>
>> you just posted another syntax other than c tonight in clc; watch it,
>> there, revolutionary.

>
> I don't think that I did that.
>


Because perl is a child of c, or that if you thought what you posted was
C, it wouldn't be syntactical?
--
Uno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write a conditional assignment over generic or constant? sm.soimu@gmail.com VHDL 2 04-25-2006 10:00 AM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM
Conditional assignment to signals Johnsy Joseph VHDL 8 09-24-2004 11:02 AM
Comparison of Bit Vectors in a Conditional Signal Assignment Statement Anand P Paralkar VHDL 2 08-04-2003 08:40 PM
Help: conditional attribute assignment itsme VHDL 1 07-23-2003 03:26 PM



Advertisments