Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data structures in perl.... 1

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
hi,

I am new to using data structures in perl. I want to have a data structure something like this in C:

struct tmp {
int a
int b
void *function(); sub routine that needs tobe executed
}

Can any one tell me how i can do it in perl. thanks in advance.

 
Feel free to just make a hash, but you have to understand that perl isn't very typed. Integers are stored as scalars, and a pointer to a function is like a reference to a subroutine, and all references are scalars, too.
Code:
my %hash;
$hash{a} = 12;
$hash{b} = 7632;
$hash{function} = \&some_sub;

sub some_sub
{
    #that does something
}
If you're into combinations of Perl's standard data types (hashes of arrays of arrays of hashes of arrays or something) read the perldoc entry 'perldsc' for data structure cookbook. There's probably pointers to more docs in there for references and data types, but is a good read if you're interrested.

________________________________________
Andrew - Perl Monkey
 
hi ,
( sorry for my english )
i was trying to take advantage of the initial pos for myself.
I try this code but i'm not able to execute the subroutine , what is the right syntax please ?
#!/usr/bin/perl
my %hash ;
$hash{akey} = [ 12,7632,\&sub1 ];
print "$hash{akey}[0] \n" ;
print "$hash{akey}[1] \n" ;
print "$hash{akey}[2] \n" ;

# this line say syntax error
# &$hash{akey}[2] ;

# with those lines nothing happends
\$hash{akey}[2] ;
$$hash{akey}[2] ;
sub sub1
{
print "test subroutine\n" ;
}

this code return :
12
7632
CODE(0x8057724)


I've got an another script where i made :
$cmde="toto" ;
$param="titi" ;
$execution = sub { &$cmde } ;
my $result=&$execution($param) ;
and that works ( it launche subrotine toto ) but i'm not able to clearly understand the difference beetween the two examples above.
 
sorry , it was a stupid post :
syntax error : with hash{akey}[2]() it works fine ;
 
I am trying this simple code but it doesnt print anything....can anyone tell where i am ging wrong?

#!/usr/bin/perl

my %hash ;
for($i=0; $i<3; $i++)
{
if ($i == 0)
{
$hash{$i}{'conf'} = 11;
$hash{$i}{'lead'} = 7631;
$hash{$i}{'sub'} = \&sub1;
}

if ($i == 1)
{
$hash{$i}{'conf'} = 12;
$hash{$i}{'lead'} = 7632;
$hash{$i}{'sub'} = \&sub2;
}

if ($i == 2)
{
$hash{$i}{'conf'} = 13;
$hash{$i}{'lead'} = 7633;
$hash{$i}{'sub'} = \&sub3;
}
}

hasharray(\@hash);


sub hasharray
{

$a = @_;

for($i=0; $i<3; $i++)
{
print "$a{$i}->{'conf'} \n" ;
print "$a{$i}->{'lead'} \n" ;
print "$a{$i}->{'sub'} \n" ;
}

}

sub sub1
{
print "In first test subroutine\n" ;
}

sub sub2
{
print "In second test subroutine\n" ;
}

sub sub3
{
print "In third test subroutine\n" ;
}
 
Best to get in the habit of running under the strict pragma. Add "use strict;" to the top of the script and fix the errors it generates. Once you're to that point, you'll probably see what's wrong.

________________________________________
Andrew - Perl Monkey
 
Thanks for the reply. What i am trying to do in the above code is have an array of hashes. This is the first time I am writing this. I am not getting it right. Is there somethign wrong that i am doing in the above code.

I need to have an array of length 3 and each element should contain the values of conf, lead and sub routine function. I then pass this to a subroutine where i need to access them and use it. Looks like i am getting confused in the way to pass it to the sub routine and accessing it there.
 
If you want an array, use an array, if you want a hash, use a hash. You create a hash with keys 1,2,3, then pass an array to the sub. The hash you created is different from the array you passed. @hash and %hash are completely separate. Basically, strict would tell you that you haven't declared @hash when you tried to pass it, even though %hash was declared and populated.

Create an array instead of a hash setting values like [tt]$hash[$i]{'conf'}[/tt] or pass a hash ref to the sub via [tt]\%hash[/tt].

________________________________________
Andrew - Perl Monkey
 
sorry for my simple question...i got it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top