|
Posted by @localhost on April 8, 2008, 4:32 am
Please log in for more thread options
I have an application which I wrote in C which uses a data structure,
and I should like to port it to Perl.
However, I am finding comprehendable information about data structures
under Perl difficult.
Basically, the existing C data structure is as follows:
typedef struct
{
char *s;
char *k;
char *a[10];
char *c[20];
}
MATCH;
MATCH *phrase;
With the strings being dynamically allocated or not in each record.
Then one has phrase->s, phrase->a[n] etc.
But even at its simplest, what I need is a structure which will hold
one string (s), and an array of a variable number of other strings
(c[] in the above example).
Anyone?
Matt
--
The Probert Encyclopaedia
http://www.probertencyclopaedia.com
|
|
Posted by mbstevens on April 8, 2008, 9:07 am
Please log in for more thread options
Matt Probert wrote:
> I have an application which I wrote in C which uses a data structure,
> and I should like to port it to Perl.
>
> However, I am finding comprehendable information about data structures
> under Perl difficult.
>
> Basically, the existing C data structure is as follows:
>
> typedef struct
> {
> char *s;
> char *k;
> char *a[10];
> char *c[20];
> }
> MATCH;
>
> MATCH *phrase;
>
>
> With the strings being dynamically allocated or not in each record.
>
> Then one has phrase->s, phrase->a[n] etc.
>
> But even at its simplest, what I need is a structure which will hold
> one string (s), and an array of a variable number of other strings
> (c[] in the above example).
>
> Anyone?
>
> Matt
>
>
> --
> The Probert Encyclopaedia
> http://www.probertencyclopaedia.com
You could use the Class::Struct module,which allows
creation of C-style structures.
Or use your own class,
which would allow putting methods
for handling the data inside
the class, too.
|
|
Posted by Scott Bryce on April 8, 2008, 10:41 am
Please log in for more thread options Matt Probert wrote:
> I have an application which I wrote in C which uses a data structure,
> and I should like to port it to Perl.
I'm not sure if I am pointing you in the right direction, but since Perl
does not have C style structures, these types of data structures are
usually represented as hashes.
http://tinyurl.com/4caubt
> However, I am finding comprehendable information about data
> structures under Perl difficult.
Because Perl does not have C style structures.
> Basically, the existing C data structure is as follows:
>
> typedef struct { char *s; char *k; char *a[10]; char *c[20]; } MATCH;
>
>
> MATCH *phrase;
>
>
> With the strings being dynamically allocated or not in each record.
>
> Then one has phrase->s, phrase->a[n] etc.
$phrase-> is simple. $phrase->->[n] may not be since Perl does not
store strings as arrays of characters. You would either have to use
substr or split to find the nth character in $phrase->;
Alternatively, you could split the string before you store it, then
store a reference to the resulting array.
> But even at its simplest, what I need is a structure which will hold
> one string (s), and an array of a variable number of other strings
> (c[] in the above example).
use strict;
use warnings;
my $phrase = {s => 'Mary had a little lamb',
a => ['Its fleece was white as snow',
'Everywhere that Mary went',
'The lamb was sure to go']
};
print "$phrase-> \n";
print "$phrase->[0] \n";
print "$phrase->[1] \n";
print "$phrase->[2] \n";
$phrase-> = [split //, 'Followed her to school one day.'];
print "$phrase->->[5] \n";
print @}[9 .. 11], "\n";
Is that what you are looking for?
|
|
Posted by @localhost on April 8, 2008, 11:46 am
Please log in for more thread options
>use strict;
>use warnings;
>
>my $phrase = {s => 'Mary had a little lamb',
> a => ['Its fleece was white as snow',
> 'Everywhere that Mary went',
> 'The lamb was sure to go']
> };
>
>print "$phrase-> \n";
>print "$phrase->[0] \n";
>print "$phrase->[1] \n";
>print "$phrase->[2] \n";
>
>$phrase-> = [split //, 'Followed her to school one day.'];
>
>print "$phrase->->[5] \n";
>print @}[9 .. 11], "\n";
>
>
>Is that what you are looking for?
Close enough - which is because I didn't communicate all my thoughts,
not through any fault of yours.
Thanks.
Matt
--
The Probert Encyclopaedia
http://www.probertencyclopaedia.com
|
| Similar Threads | Posted | | Terminology questions (DNS, A-records, MX records, Static IP, etc) | October 28, 2005, 9:40 pm |
| Perl question - libwww-perl | June 16, 2006, 9:21 am |
| How can I cut data out of HTML table, into msExcel and just take the data & columns? (but NOT the formatting & URLs!) | April 21, 2006, 5:11 am |
| how to handle editing existing data while adding new data | January 18, 2005, 6:43 pm |
| On SPF Records | January 27, 2006, 12:05 pm |
| Help with Perl problem..please | November 15, 2005, 12:20 pm |
| Perl Question | March 3, 2006, 1:27 pm |
| Fishing for JSP, PHP, Perl (which do you use?) | April 3, 2006, 6:33 pm |
| Perl help for a Rookie | June 22, 2007, 6:49 am |
| Neat Perl trick | January 27, 2005, 12:03 am |
|