Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: c-like struct?

by xiaoyafeng (Chaplain)
on Jun 24, 2011 at 13:19 UTC ( #911248=note: print w/ replies, xml ) Need Help??


in reply to Re: c-like struct?
in thread c-like struct?

I have many pieces of sorted data to use like below:

['US', 23, 'Andy', 'adress....' ] ['CA', 34, 'White', '........' ] ..... .....
I'd like to crate a structure like c struct to be self description of every element meaning:
struct test{ char* state; int age; char* name; ... ... };
In this case, I think a hash is not appropriate because
  • hash is unsorted, but the order of elements of data is fixed
  • I will never pick some elements in a struct to use. Every time, I manipulate a whole struct as a piece of data, copy, delete, print etc.
  • Now, my way is to add a comment to show sequence of elements by the end of statement:
    my @struct = () #(state, age, name, ....)
    But I'd like to know if there is a better way to reach this. Thanks




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction


Comment on Re^2: c-like struct?
Select or Download Code
Re^3: c-like struct?
by MidLifeXis (Prior) on Jun 24, 2011 at 13:34 UTC

    Why would the "perl way" of using an AoH not work?

    my @data = ( { state => 'US', age => 22, name => 'Andy', ... }, { state => 'CA', age => 34, name => 'White, ... }, );

    Is there some action that you are having problems with, or is the representation just not C-like?

    --MidLifeXis

      the most difference is hash is not sorted, array is.




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        What is the use case where having the data sorted is important? If you are serializing your data, you have a serialization function (could even be pack). How it is stored in memory seems to me to be a detail that is a "don't care" in most circumstances, and is possibly a premature optimization.

        --MidLifeXis

Re^3: c-like struct?
by Anonymous Monk on Jun 24, 2011 at 13:39 UTC
    You're asking for objects
    { sub SomeUser::new { shift; return bless [ @_ ], 'SomeUser' } sub SomeUser::State { $_[0]->[0] } sub SomeUser::Age { $_[0]->[1] } sub SomeUser::Name { $_[0]->[2] } sub SomeUser::Address { $_[0]->[3] } my $foo = bless ['US', 23, 'Andy', 'adress....' ], 'SomeUser'; print "Hello ", $foo->Name, "\n"; $foo = SomeUser->new('US', 23, 'Andy', 'adress....' ); print "Hello ", $foo->[2], "\n"; } __END__ Hello Andy Hello Andy
    chromatics free book Modern Perl discusses both this, and the modern way (Any::Moose)
Re^3: c-like struct?
by BrowserUk (Pope) on Jun 24, 2011 at 13:56 UTC
    to be self description ... the order of elements of data is fixed

    If your main reason for considering using a hash rather than an array, is for the purpose of naming the elements, then how about naming the indices of the array elements:

    use enum qw[ STATE NAME AGE ADDRESS ]; my @arrrayOfRecs = ( ['US', 23, 'Andy', 'adress....' ], ['CA', 34, 'White', '........' ], );

    But I'm confused as to why you are concerned with naming the elements, if your program truly

    will never pick some elements in a struct to use. Every time, I manipulate a whole struct as a piece of data, copy, delete, print etc.
    ?

    If these are opaque units of data at this level of the code, why name the internal parts at all?

    hash is unsorted, but the order of elements of data is fixed
    ?

    Equally, if you want them named, why on earth are you concerned with the details of the internal storage "ordering"? When you eventually take these elements out of the hash -- as you must do at some point to write them to a file or display them on a screen; otherwise what purpose do they serve -- then so long as you use the names (hash keys) in the right order, then you will get the elements back in that order. (Or any other order you choose.)

    Basically, your questions make no sense. You seem to be worrying about irrelevant internal details and making a mountain of it.

    I think you need to step back and clarify what these "structs" really are and how they are used. Ie. Describe:

    • how they are populated: where do you get the data they contain from?
    • what you do with them in the program. Do you sort them? By fields? Do you select subsets of them for particular purposes?
    • what do you do with them at the end of the program? Write them to a file? To the Screen? Discard them completely?

    Without you telling us more about where these "structs" come from and how they are manipulated, all we can do is try to answer the literal interpretation of your questions, and at the moment, they are not making a much sense.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://911248]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-08-01 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls