![]() |
length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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* |
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 > |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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 |
Re: length of an array in a struct in an array of structs in a struct in an array of structs
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! |
| All times are GMT. The time now is 09:50 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.