Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > length of an array in a struct in an array of structs in a struct in an array of structs

Reply
Thread Tools

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

 
 
Tuan Bui
Guest
Posts: n/a
 
      07-28-2005
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

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      07-28-2005
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*

 
Reply With Quote
 
 
 
 
Billy N. Patton
Guest
Posts: n/a
 
      07-28-2005
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
>

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-28-2005
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

 
Reply With Quote
 
Tuan Bui
Guest
Posts: n/a
 
      07-28-2005


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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-28-2005
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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      07-28-2005
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

 
Reply With Quote
 
Tuan Bui
Guest
Posts: n/a
 
      07-28-2005


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

 
Reply With Quote
 
Tuan Bui
Guest
Posts: n/a
 
      07-28-2005


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

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      07-28-2005
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static (non-zero) initialization of an array of structs (array of length #define LEN) anon.asdf@gmail.com C Programming 23 08-28-2007 11:54 PM
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments