length of an array in a struct in an array of structs in a struct in an array of structs

Discussion in 'Perl Misc' started by Tuan Bui, Jul 28, 2005.

  1. Tuan  Bui

    Tuan Bui Guest

    Hello,

    So I have this complex data structure I've built for myself, and I'm
    trying to get this snippet to work:

    $k <
    scalar(@{@{@VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray}})

    VisitArray is an array of VisitStructs. Each VisitStruct contains an
    array of PSearchStructs called PSearchStructArray. Each PSearchStruct
    contains an array called ClickPathArray. I'm trying to get the length
    of ClickPathArray out. I've tried numerous variations of that line, and
    none of them work. I do know that

    scalar(@{@VisitArray[$i]->PSearchStructArray})

    will return the length of PSearchStructArray well enough though.

    I'm helplessly floundering here - hope one of you perl hackers can
    help. Anyone who does help will earn my undying gratitude :)

    Tuan
     
    Tuan Bui, Jul 28, 2005
    #1
    1. Advertising

  2. hmm, i'm unsure why you want to do everything in one line. try breaking
    it up.

    this is part of a double nested loop i'm assuming.

    for my $i (0..$#VisitArray) {
    my $PSearchArray_ref = $VisitArray[$i];
    my @PSearchArray = @{$PSearchArray_ref};
    for my $j (0..$#PSearchArray) {
    $k < scalar(@{$PSearchArray[$j]});
    }
    }

    i think that would work and is what you want--i could be wrong on both
    counts though *shrug*
     
    it_says_BALLS_on_your forehead, Jul 28, 2005
    #2
    1. Advertising

  3. Re: length of an array in a struct in an array of structs in a structin an array of structs

    Tuan Bui wrote:
    > Hello,
    >
    > So I have this complex data structure I've built for myself, and I'm
    > trying to get this snippet to work:
    >
    > $k <
    > scalar(@{@{@VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray}})
    >


    Ths should do it.
    $l=scalar(@{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray});


    > VisitArray is an array of VisitStructs. Each VisitStruct contains an
    > array of PSearchStructs called PSearchStructArray. Each PSearchStruct
    > contains an array called ClickPathArray. I'm trying to get the length
    > of ClickPathArray out. I've tried numerous variations of that line, and
    > none of them work. I do know that
    >
    > scalar(@{@VisitArray[$i]->PSearchStructArray})
    >
    > will return the length of PSearchStructArray well enough though.
    >
    > I'm helplessly floundering here - hope one of you perl hackers can
    > help. Anyone who does help will earn my undying gratitude :)
    >
    > Tuan
    >
     
    Billy N. Patton, Jul 28, 2005
    #3
  4. Tuan  Bui

    Paul Lalli Guest

    Tuan Bui wrote:
    > I do know that
    >
    > scalar(@{@VisitArray[$i]->PSearchStructArray})
    >
    > will return the length of PSearchStructArray well enough though.


    Perhaps I'm having a brain cramp here, but I can't especially see how
    that could work. The arrow notation is used for either A)
    dereferencing a hash or array reference to get at its elements or B)
    calling a method of an object or class.

    In other words, I can see that
    $VisitArray[$i]->{PSearchStructArray}
    might return the value of the hash referenced by $VisitArray[$i] at key
    'PSearchStructArray', or
    $VisitArray[$i]->PSearchStructArray()
    might call a method of the object $VisitArray[$i], which could in turn
    return a list of values.

    I cannot, however, discern any way in which
    $VisitArray[$i]->PSearchStructArray
    could return "the length of PSearchStructArray".

    Can you please enlighten me by showing the code that creates your
    classes and objects?

    (For why I replaced your '@' with '$' in each example, please see:
    perldoc -q difference
    )

    Paul Lalli
     
    Paul Lalli, Jul 28, 2005
    #4
  5. Tuan  Bui

    Tuan Bui Guest

    Paul Lalli wrote:
    > Tuan Bui wrote:
    > > I do know that
    > >
    > > scalar(@{@VisitArray[$i]->PSearchStructArray})
    > >
    > > will return the length of PSearchStructArray well enough though.

    >
    > Perhaps I'm having a brain cramp here, but I can't especially see how
    > that could work. The arrow notation is used for either A)
    > dereferencing a hash or array reference to get at its elements or B)
    > calling a method of an object or class.


    I am using Class::Struct, and indeed i am implicitly calling methods
    from it. It indeed returns the correct lengths that I want.

    >
    > In other words, I can see that
    > $VisitArray[$i]->{PSearchStructArray}
    > might return the value of the hash referenced by $VisitArray[$i] at key
    > 'PSearchStructArray', or
    > $VisitArray[$i]->PSearchStructArray()
    > might call a method of the object $VisitArray[$i], which could in turn
    > return a list of values.
    >
    > I cannot, however, discern any way in which
    > $VisitArray[$i]->PSearchStructArray
    > could return "the length of PSearchStructArray".
    >
    > Can you please enlighten me by showing the code that creates your
    > classes and objects?




    struct( VisitStruct => {
    VisitID => '$', # ID number of this visit
    PSearchStructArray => '@', # array of psearch structures
    (PSearchStruct type)

    });

    struct( PSearchStruct => {
    ClickPathArray => '@', # array of click path elements (scalar type)
    Before => EventStruct, # event that occured before psearch
    (EventStruct type)
    After => EventStruct, # event that occured after psearch
    (EventStruct type)
    ReachedItemScreen => '$', # bool that is true if user reached item
    detail screen (scalar type)
    ReachedCatalogPage => '$', # bool that is true if user reached
    catalog screen (scalar type)
    ReachedBookmarkScreen => '$', # bool that is true if user bookmarked
    item (scalar type)
    # note: in order to set this bool, we will have to go one
    beyond the
    # item detail screen when traversing events
    });

    struct( EventStruct => {
    EventCode => '$', # code of event (scalar type)
    EventDesc => '$', # short description of event (scalar type)
    });

    @VisitArray has VisitStructs pushed onto it elsewhere in the code.



    >
    > (For why I replaced your '@' with '$' in each example, please see:
    > perldoc -q difference
    > )


    Neat... thanks for pointing this out.

    >
    > Paul Lalli


    Thanks.
    To everyone else who has responded, thank you also! I am testing out
    your solutions as we type.

    Tuan
     
    Tuan Bui, Jul 28, 2005
    #5
  6. Tuan  Bui

    Paul Lalli Guest

    Tuan Bui wrote:
    > Paul Lalli wrote:
    > > Tuan Bui wrote:
    > > > I do know that
    > > >
    > > > scalar(@{@VisitArray[$i]->PSearchStructArray})
    > > >
    > > > will return the length of PSearchStructArray well enough though.

    > >
    > > Perhaps I'm having a brain cramp here, but I can't especially see how
    > > that could work. The arrow notation is used for either A)
    > > dereferencing a hash or array reference to get at its elements or B)
    > > calling a method of an object or class.

    >
    > I am using Class::Struct, and indeed i am implicitly calling methods
    > from it. It indeed returns the correct lengths that I want.


    Ahh, when you said you had structs, you actually meant structs, as in
    objects of Class::Struct. My apolgies - I kinda through that word out
    as a "doesn't mean what you think it means" when I scanned your
    original post.

    I'm reading up on Class::Struct as we speak. Much obliged.

    Paul Lalli
     
    Paul Lalli, Jul 28, 2005
    #6
  7. Tuan  Bui

    Paul Lalli Guest

    Billy N. Patton wrote:
    > Tuan Bui wrote:
    > > Hello,
    > >
    > > So I have this complex data structure I've built for myself, and I'm
    > > trying to get this snippet to work:
    > >
    > > $k <
    > > scalar(@{@{@VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray}})
    > >

    >
    > Ths should do it.
    > $l=scalar(@{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray});


    Just to be pedantic, the scalar keyword and its parentheses are no-ops
    there. The assignment to a scalar variable already imposes scalar
    context on the array.

    Assuming the above does what you want,

    $l = @{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray};

    should work just as well.

    Paul Lalli
     
    Paul Lalli, Jul 28, 2005
    #7
  8. Tuan  Bui

    Tuan Bui Guest

    Paul Lalli wrote:
    > Billy N. Patton wrote:
    > > Tuan Bui wrote:
    > > > Hello,
    > > >
    > > > So I have this complex data structure I've built for myself, and I'm
    > > > trying to get this snippet to work:
    > > >
    > > > $k <
    > > > scalar(@{@{@VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray}})
    > > >

    > >
    > > Ths should do it.
    > > $l=scalar(@{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray});

    >
    > Just to be pedantic, the scalar keyword and its parentheses are no-ops
    > there. The assignment to a scalar variable already imposes scalar
    > context on the array.


    Yeah, I'm a perl newbie and I'm using the constructs the llama tells me
    to use :)

    >
    > Assuming the above does what you want,
    >
    > $l = @{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray};
    >
    > should work just as well.


    If by "just as well" you mean "syntax error," then yes :) It's one of
    the first things I tried.

    I'm pursuing the intermediate-step path right now:

    my @PSARef = @{$VisitArray[$i]->PSearchStructArray};
    for(my $k = 0; $k < scalar(@{$PSARef[$j]->ClickPathArray}); $k++) {

    but for some reason it's returning abnormal lengths that are invariant
    with different iterations of the innermost loop.

    >
    > Paul Lalli


    Back to the grind,
    Tuan
     
    Tuan Bui, Jul 28, 2005
    #8
  9. Tuan  Bui

    Tuan Bui Guest

    Tuan Bui wrote:
    > I'm pursuing the intermediate-step path right now:
    >
    > my @PSARef = @{$VisitArray[$i]->PSearchStructArray};
    > for(my $k = 0; $k < scalar(@{$PSARef[$j]->ClickPathArray}); $k++) {
    >
    > but for some reason it's returning abnormal lengths that are invariant
    > with different iterations of the innermost loop.
    >


    Update: This approach actually works. The constant-length problem
    seems to stem from a bug elsewhere in my code.

    Thanks for all the help, peepz!

    Tuan
     
    Tuan Bui, Jul 28, 2005
    #9
  10. that's great news :).

    one suggestion:

    using C-type loops in Perl is inefficient.
    The fastest For Loop would loop by value, i.e.:
    for my $value (@array) {
    ...do something with $value...
    }

    or you could use the default variable $_:

    for (@array) {
    $_ is the element...
    }


    when you need an iterator, this is much faster than a C-style for loop:
    for my $i (0..$#array) {
    blah $array[$i];
    }


    hope this helps!
     
    it_says_BALLS_on_your forehead, Jul 28, 2005
    #10
  11. Tuan  Bui

    Paul Lalli Guest

    Tuan Bui wrote:
    > Paul Lalli wrote:
    > > Billy N. Patton wrote:
    > >
    > > > Ths should do it.
    > > >
    > > > $l=scalar(@{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray});

    > >
    > > Assuming the above does what you want,
    > >
    > > $l = @{$VisitArray[$i]->PSearchStructArray[$j]->ClickPathArray};
    > >
    > > should work just as well.

    >
    > If by "just as well" you mean "syntax error," then yes :) It's one of
    > the first things I tried.


    Wait. Do you actually mean that the first line of code above, from
    Billy Patton, does work, but that the second line of code, from me,
    does not?

    What syntax error does the second give that the first does not?

    Paul Lalli
     
    Paul Lalli, Jul 28, 2005
    #11
  12. Tuan  Bui

    Tuan Bui Guest

    > Wait. Do you actually mean that the first line of code above, from
    > Billy Patton, does work, but that the second line of code, from me,
    > does not?


    Yes, that's right.

    >
    > What syntax error does the second give that the first does not?


    syntax error at test.pl line 396, near "->PSearchStructArray["

    >
    > Paul Lalli


    Now I'm trying to puzzle my way through how Perl handles pointers to
    structs declared with "new."

    Tuan
     
    Tuan Bui, Jul 28, 2005
    #12
  13. Tuan  Bui

    Tuan Bui Guest

    Oops, sorry - the code from balls works. I never tried the code from
    Billy.
     
    Tuan Bui, Jul 28, 2005
    #13
  14. Tuan  Bui

    T Beck Guest

    it_says_BALLS_on_your forehead wrote:
    > that's great news :).
    >
    > one suggestion:
    >
    > using C-type loops in Perl is inefficient.

    -snip-

    I don't know where that got started... It could be in the camel, but I
    don't remember reading that. What's more, on my machine, C-Style is
    almost universally faster (if not the same speed) than (0..n) for
    simple loops. For instance:
    ~>time perl -e '$a=1; for ($i=0; $i<10001; $i++){$a*=100}'
    0.02u 0.01s 0:00.02 150.0%
    ~>time perl -e '$a=1; for (0..10000){$a*=100}'
    0.05u 0.03s 0:00.07 114.2%

    IMHO, the difference isn't a big deal one way or the other, so I say if
    someone feels good writing C-Style loops, then TIMTOWTDI, indeed!

    --T Beck
     
    T Beck, Jul 29, 2005
    #14
  15. last year I attended a couple of lectures by Damian Conway ( he wrote,
    among othe things, the Object Oriented Perl book, and he is a major
    contributor to the regex engine for Perl ). he handed out his new book,
    "Perl Best Practices". in it, he describes what the best "for loops"
    are. i think he's pretty familiar with how Perl works behind the
    scenes. I benchmarked all 3 methods, and got this:

    [nagano 25] ~> benchForLoop.pl
    Benchmark: running array by c iterator, array by perl iterator, array
    by value, array by value using default var, each for at least 1 CPU
    seconds...
    array by c iterator: 1 wallclock secs ( 1.09 usr + 0.00 sys = 1.09
    CPU) @ 76.15/s (n=83)
    array by perl iterator: 2 wallclock secs ( 1.05 usr + 0.00 sys =
    1.05 CPU) @ 122.86/s (n=129)
    array by value: 1 wallclock secs ( 1.04 usr + 0.00 sys = 1.04 CPU) @
    146.15/s (n=152)
    array by value using default var: 2 wallclock secs ( 1.09 usr + 0.00
    sys = 1.09 CPU) @ 146.79/s (n=160)

    ....here is the code:

    $x = 0;
    for my $index (0..3000) {
    push( @numbers, $index );
    }

    sub perlIter {
    my ($data_ref) = @_;
    my @data = @{$data_ref};

    for my $i (0..$#data) {
    $data[$i] = 2;
    }
    }

    sub cIter {
    my ($data_ref) = @_;
    my @data = @{$data_ref};

    for (my $i = 0; $i < scalar(@data); $i++) {
    $data[$i] = 2;
    }
    }

    sub byValue {
    my ($data_ref) = @_;
    my @data = @{$data_ref};

    for my $num (@data) {
    $num = 2;
    }
    }

    sub byDefaultValue {
    my ($data_ref) = @_;
    my @data = @{$data_ref};

    for (@data) {
    $_ = 2;
    }
    }

    timethese(-1, {
    'array by perl iterator' => '\&perlIter(\@numbers)',
    'array by c iterator' => '\&cIter(\@numbers)',
    'array by value' => '\&byValue(\@numbers)',
    'array by value using default var' => '\&byDefaultValue(\@numbers)',
    });
     
    it_says_BALLS_on_your forehead, Jul 29, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    651
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    565
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    651
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,423
    Keith Thompson
    Apr 10, 2006
  5. Replies:
    23
    Views:
    858
    Chris Thomasson
    Aug 29, 2007
Loading...

Share This Page