LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2002, 08:42 AM   #1
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
finding the length of an array in perl


i'm currently going totally nuts about this stupid datatype i'm trying to use. i'm so so so sure that the data is being put into the nested arrays properly, as i can instantly recall it on assignment, and pull certain values out using hardcoded array references, BUT the array's length is always being reported back as 1, using "$#array" + 1 "or scalar @array". i'm at a complete loss right now...
Code:
 for my $this_vts (1..$dvd->{vts}[0])
  {
    $dvd->{vts}[$this_vts]=[];
    open (IFO, "ifo_dump $DVD_DEVICE $this_vts 2> /dev/null |") or die "error running ifo_dump! maybe it's not on your path?\n";
    IFO: while (<IFO>)
    {
      if ($_=~/Program \(PGC\): *(\d*)/) {$this_ttn=$1; $dvd->{vts}[$this_vts][$this_ttn]=[]}
      elsif ($_=~/Playback time: (\d\d:\d\d:\d\d)/) {$dvd->{vts}[$this_vts][$this_ttn][0]=$1}
      elsif ($_=~/Cell:\s+(\d+)\s+(\d\d:\d\d:\d\d)/) {$dvd->{vts}[$this_vts][$this_ttn][$1]=$2; print $1, $2, "\n";}
      elsif ($_=~/Menu PGCI Unit table/) {last IFO;}
    }
  }
right, that is where the values become assigned, inparticular the highlighted line, which also includes print lines, which DO print the data i expect.

but it's when i come to interogate the data later with:

Code:
sub show_dvd ($)
{
  my $dvd = shift;
  for my $this_track (1..$dvd->{track}[0])
  {
    my $this_ttn = $dvd->{track}[$this_track]{ttn};
    my $this_vts = $dvd->{track}[$this_track]{vts};
    print scalar $dvd->{vts}[$this_vts][$this_ttn];
    print "NO. $this_track, VTS $this_vts, TTN $this_ttn, LEN $dvd->{vts}[$this_vts][$this_ttn][0], $dvd->{vts}[$this_vts][$this_ttn][1]\n";
  }
}
now, the second print statement DOES print the data in those given positions IF it exists, if not, it will complain about an unitialiased value, which is fair enough. But the first statement, or any permutation i can think of it (e.g. assign the scalar to a $ and then print it later) will always return 1.... always. nryagg!
 
Old 09-25-2002, 10:09 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
So, $dvd->{vts}[$this_vts][$this_ttn] is an array reference, correct?

If so, you'll need to put @{} around it when using scalar to determine it's length:

Code:
sub show_dvd ($)
{
  my $dvd = shift;
  for my $this_track (1..$dvd->{track}[0])
  {
    my $this_ttn = $dvd->{track}[$this_track]{ttn};
    my $this_vts = $dvd->{track}[$this_track]{vts};
    print scalar @{$dvd->{vts}[$this_vts][$this_ttn]};
    print "NO. $this_track, VTS $this_vts, TTN $this_ttn, LEN $dvd->{vts}[$this_vts][$this_ttn][0], $dvd->{vts}[$this_vts][$this_ttn][1]\n";
  }
}

That should do the trick.
 
Old 09-25-2002, 10:22 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Original Poster
Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
once again you save me!

perfect. spot on, thanks. I know all the documentation is out there, but the problems i'm falling over all seem so hard to describe, let alone search for general information... brilliant, well that saves creating even more variables... super.
 
Old 09-25-2002, 10:36 AM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
(= I'm just glad to be of help.. when I first started reading this thread, I didn't think I'd be able to help.

But, the only reason I knew this was because I have struggled with this same thing before. References can be tricky... but, you've got a really good grasp on it.. I'm impressed with how quickly you've taken to perl.
 
Old 09-25-2002, 10:39 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Original Poster
Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well i've got a degree in computer engineering, once you know one langauge you know half of all the others... you just learn what you should be able to demand and do and find out how to do it. Onl real exceptions i've found are functional languages like ML, which did make my head spin...
 
Old 09-25-2002, 10:50 AM   #6
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
ML.. hehe.. yea.. there are a few that I don't want to get near, ML is one, and lisp/scheme is another.. I'm sure there are many more, but I'll stick with perl/c/python/java thank you very much. (=
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
finding array length from a reference (in Perl) lowpro2k3 Programming 1 06-22-2005 04:49 PM
Quick perl question using length Seventh Programming 4 09-08-2004 04:29 PM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 02:50 PM
perl matching *-length strs towlie Programming 2 07-21-2004 08:13 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration