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. Advertisements

  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. Advertisements

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

     
    Billy N. Patton, Jul 28, 2005
    #3
  4. Tuan  Bui

    Paul Lalli Guest

    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

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


    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.


    Neat... thanks for pointing this out.
    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

    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

    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

    Yeah, I'm a perl newbie and I'm using the constructs the llama tells me
    to use :)
    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.
    Back to the grind,
    Tuan
     
    Tuan Bui, Jul 28, 2005
    #8
  9. Tuan  Bui

    Tuan Bui Guest

    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

    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
    Yes, that's right.
    syntax error at test.pl line 396, near "->PSearchStructArray["
    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

    -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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.