Professional Web Applications Themes

iterate over the fields of a struct? - PERL Beginners

is there a way to iterate over the fields of a Class::Struct (in the order they were declared)? something like: foreach my $i ({my_struct->fields()}) { print "$i = my_struct->value($i)\n"; } yeah i know its a long shot, but perl sometimes does things for me that i never would have believed...much less knew to expect...;) also, i know you can do this with hashes (although in "random" order, unless you sort the keys), but hashes are too error prone for my liking. i.e. $my_hash{TYPO} = $blah; does not result in an error...=( thanks....

  1. #1

    Default iterate over the fields of a struct?

    is there a way to iterate over the fields of a Class::Struct (in the order
    they were declared)?

    something like:

    foreach my $i ({my_struct->fields()}) {
    print "$i = my_struct->value($i)\n";
    }

    yeah i know its a long shot, but perl sometimes does things for me that i
    never would have believed...much less knew to expect...;)

    also, i know you can do this with hashes (although in "random" order, unless
    you sort the keys), but hashes are too error prone for my liking. i.e.
    $my_hash{TYPO} = $blah; does not result in an error...=(

    thanks.

    Christopher Guest

  2. #2

    Default Re: iterate over the fields of a struct?

    Christopher J. Bottaro wrote: 

    No. You could store the data used to generate the struct, and then use
    it later.

    my struct_data = [
    key1 => 'type',
    key2 => 'type',
    ];

    struct Name => \struct_data;

    then iterate of struct_data...
     

    If you're using version 5.8 or later you can use restricted hashes. See
    `perldoc Hash::Util`

    Randy.
    Randy Guest

  3. #3

    Default RE : iterate over the fields of a struct?


     
    > order 
    >
    > No. You could store the data used to generate the struct, and then use
    > it later.
    >
    > my struct_data = [
    > key1 => 'type',
    > key2 => 'type',
    > ];
    >
    > struct Name => \struct_data;
    >
    > then iterate of struct_data...
    > [/ref]

    did you meant:

    my $struct_data = [ #array_ref
    key1 => 'type',
    key2 => 'type',
    ];

    struct Name => $struct_data;
    then iterate of $struct_data ...

    ?

    array = array_ref is confusing to me ...
    could you explain please ?
     [/ref]
    that [/ref]
    order, [/ref]
    i.e. 
    >
    > If you're using version 5.8 or later you can use restricted hashes.[/ref]
    See 

    helpfull module !
    Hash vivification is a nightmare, really ...

    José.

    Jose Guest

Similar Threads

  1. Outputing form fields sent on action page gives me'struct empty'
    By Hydrowizard in forum Coldfusion - Getting Started
    Replies: 2
    Last Post: February 4th, 05:59 AM
  2. Creating database fields from Struct(FORM)
    By Niles Runeberg in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: April 11th, 01:32 PM
  3. Issues with declaring struct arrays inside of a struct
    By Richard Mathis in forum ASP.NET Building Controls
    Replies: 0
    Last Post: October 10th, 03:14 PM
  4. Calling fun taking struct and not pointer to struct?
    By Nathaniel Talbott in forum Ruby
    Replies: 14
    Last Post: September 11th, 03:26 AM
  5. Replies: 0
    Last Post: September 10th, 11:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139