Click to See Complete Forum and Search --> : Quarstion Time...


PetrolMan
10-11-2000, 07:34 PM
In Perl You Can Do Neato Stuff Like:

${$foo} = 'blah';

Is that Possible in C?

-PetrolMan-

StarWeaver
10-11-2000, 08:59 PM
You mean a pointer?
like

int a; // regular int var
int * p; // pointer-to-int type var
p = &a; // p = address of a
*p = 5; // var-pointed-to-by-p = 5

?
:|
-RASX

TheLinuxDuck
10-11-2000, 10:03 PM
Originally posted by PetrolMan:
In Perl You Can Do Neato Stuff Like:
${$foo} = 'blah';
Is that Possible in C?
-PetrolMan-

There aren't any easy ways to duplicate a hash.. but there are some ways to fake it.. http://www.linuxnewbie.org/ubb/smile.gif The trouble with doing this in C is that perl handles all the memory allocation on the fly.. you don't have to worry about that, but in C you do.. so, a lot of the code to recreate a hash would be memory allocation.
Or you could preallocate, which can get nasty big if you're going to be using a lot of variables.

You can 'feign' something similar by doing something with an array, like:


#include <stdio.h>

int main(void)
{
int fred=0,barney=1;
char hash[2][50];

sprintf(hash[fred],"%s","flintstone");
sprintf(hash[barney],"%s","rubble");

printf("fred %s\nbarney %s\n",hash[fred],hash[barney]);
return 0;
}


This is a simple example. You could easily create a function to handle all the messy work of the hash... or if you wanted to go for a dynamically allocated structure or something...


------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

klamath
10-11-2000, 10:25 PM
There aren't any easy ways to duplicate a hash.. but there are some ways to fake it..

BTW, in the example he's dereferencing a scalar, not accessing a hash at all. http://www.linuxnewbie.org/ubb/biggrin.gif

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)

TheLinuxDuck
10-11-2000, 10:31 PM
Originally posted by klamath:
BTW, in the example he's dereferencing a scalar, not accessing a hash at all. http://www.linuxnewbie.org/ubb/biggrin.gif


Crappity!!!! I guess I didn't look at it very close.. I thought I saw a variable name, not a $. http://www.linuxnewbie.org/ubb/frown.gif

Btw, I've been getting into references and passing arrays and such lately.. it's pretty qool.. I was messing with this one thing today that passed a reference to an array into a function, then adjusted the first slice of the array according to info in a config file, then assigned a reference to another array to the second slice if the first slice was true.. doing stuff like:
${$array[0]}[0]="/home/groups/chicken";

It took me a little bit to get it working, but I think it's starting to sink in.. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

StarWeaver
10-12-2000, 04:21 AM
Oh hehe... it looked like he wanted .. i dunno looked like a wierd kina pointerish thing. Not that i know ANYTHING about PERL. That should be ovbious. This was PERL right? :)

-Ronin Angel StarWeaver
"I've said it before, and i'll say it again: ....... ...... ..... ah fsck!"

takshaka
10-12-2000, 05:08 PM
${$foo} = 'blah';

What this is depends on the value of $foo. If $foo is a string (thus making ${$foo} a symbolic reference) then, no, you can't do that in C. But if $foo holds a Perl hard reference, that's similar to C pointers.

PetrolMan
10-12-2000, 08:48 PM
Pointers don't quite work...

${$foo} is alot different than just have a pointer point to the value...

$foo allows you to access the variable by it's name. A pointer uses the memory address...

I can't remember what exactly I had in mind but I do know it had to do with naming c variables somewhat dynamically...

Like if the program received the input "foo" I want to be able to create a variable named foo storing a certain value (the value doesn't matter...)

I am unsure as to if or how this can be done in C...

-PetrolMan-

[This message has been edited by PetrolMan (edited 12 October 2000).]

pdc
10-13-2000, 01:56 AM
The question should be "what are you trying to do". Heck, perl is written in C. But, it is a C program reading a file and interpreting what you want done by processing the file's contents. Very powerfull, but a tad slow depending on what processing must be done in the interpretation.

Which reminds me, I had better get back to butchering perl and the Linux port of some DB/2 perl programs.

PAul

klamath
10-13-2000, 10:17 AM
Heck, perl is written in C

Actually, Objective C IIRC.



------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)

TheLinuxDuck
10-13-2000, 10:45 AM
My guess would be something like:

#include <stdio.h>

int main(void)
{
char *sptr,s[100];

sptr=s;
sprintf(sptr,"%s","monkey poo");

printf("%s\n",sptr);
return 0;
}

as compared to:

#!/usr/bin/perl -w
use strict;

my ($foo,$poo);
$foo=\$poo;
${$foo}="monkey poo";

print "${$foo}\n";


I dunno how you're using $foo, though, so this is just a thing.

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

takshaka
10-13-2000, 06:04 PM
Originally posted by PetrolMan:
${$foo} is alot different than just have a pointer point to the value...

$foo allows you to access the variable by it's name. A pointer uses the memory address...

Like I said, it depends on whether $foo contains a string or a reference. A Perl hard reference is similar a C pointer. But you are obviously thinking only of symbolic references, which can't be done in C (and usually shouldn't be used in Perl).

Like if the program received the input "foo" I want to be able to create a variable named foo storing a certain value (the value doesn't matter...)

This is a bad idea in any language, regardless of whether it is possible.

Verbed
10-13-2000, 06:38 PM
Originally posted by klamath:
Actually, Objective C IIRC.


If that were true, there wouldn't be any windows ports :P

PetrolMan
10-14-2000, 07:45 PM
Think about it this way...

$foo = 'bar';

${$foo} == $bar

I guess that is symbolic... but I find these infinitely useful... whether they are a no-no or not.

Since apparently they can't be done in C... could someone show me an alternative?

Strike
10-14-2000, 08:06 PM
Originally posted by PetrolMan:
Think about it this way...

$foo = 'bar';

${$foo} == $bar

I guess that is symbolic... but I find these infinitely useful... whether they are a no-no or not.

Since apparently they can't be done in C... could someone show me an alternative?

Um, not only are they a no-no, I find them counter-productive. Admittedly, back when I first started programming, I thought that would be a neat idea. But, since I was learning C/C++ it couldn't be done. What exactly do you need to get done that that is such a helpful way of doing things?