On Sun, Apr 3, 2011 at 6:32 PM, John W. Krahn <jwkr...@shaw.ca> wrote:
> shawn wilson wrote:
>>
>> this is pretty abstract from what i'm really doing, but i'll put in
>> the blurb anyway...
>>
>> i'm using CAM::PDF to parse data. this works well. however, i am
>> trying to extract information out of a table. so, i want to find
>> certain keys, then go up 3 levels and grab the position of that peace
>> of data so that i can then search for text with that same horizontal
>> or vertical position (defined in two other refs on the same level of a
>> different 'branch'). so, what i want is an array ref with the number
>> of found keys with the refs it took to get there. i've liked
>> Data::Walk but i couldn't figure out how to return the results - all i
>> could do is 'print'. and i couldn't figure out how to use
>> Data::Visitor either. so, i figured i'd try myself.
>>
>> a few things i don't understand here:
>> does the second print cut off?
>> when i pass $tree instead of \$tree, it gives me this:
>> CAM::PDF::Content=HASH(0x2b298a8) is CAM::PDF::Content LEVEL: 0 FOUND:
>> 0 CLEAR: 0
>> where CAM::PDF::Content isn't going to match anything. how do i handle
>> this?
>> obviously i'm not thinking clearly about how i need to build
>> $datdepth. and ideally, i'd like the value to be internal to the sub
>> and returned when it is finished instead of global. any help with my
>> thinking would be great.
>>
>> so, here's where i'm at:
>
> [ SNIP some code ]
>
>
>>    if( ( ref( $data ) or $data ) eq 'SCALAR' ) {
>
>>    } elsif( ( ref( $data ) or $data ) eq 'HASH' ) {
>
>>    } elsif( ( ref( $data ) or $data ) eq 'ARRAY' ) {
>
>>    } elsif( ( ref( $data ) or $data ) eq 'REF' ) {
>
>>    } elsif( ( ref( $data ) or $data ) ne ( 'SCALAR' or 'HASH' or 'ARRAY' )
>> ) {
>
>
> Those comparisons are not going to work, or at least not in the way you
> probably intended.
>
> $ perl -le' print "It did ", ( $ARGV[0] or $ARGV[1] ) eq "TEST" ? "" : "NOT
> ", "work." '   a b
> It did NOT work.
> $ perl -le' print "It did ", ( $ARGV[0] or $ARGV[1] ) eq "TEST" ? "" : "NOT
> ", "work." '   a TEST
> It did NOT work.
> $ perl -le' print "It did ", ( $ARGV[0] or $ARGV[1] ) eq "TEST" ? "" : "NOT
> ", "work." '   TEST b
> It did work.
>
>
> You have to do it like this:
>
> $ perl -le' print "It did ", $ARGV[0] eq "TEST" || $ARGV[1] eq "TEST" ? "" :
> "NOT ", "work." '   a b
> It did NOT work.
> $ perl -le' print "It did ", $ARGV[0] eq "TEST" || $ARGV[1] eq "TEST" ? "" :
> "NOT ", "work." '   a TEST
> It did work.
> $ perl -le' print "It did ", $ARGV[0] eq "TEST" || $ARGV[1] eq "TEST" ? "" :
> "NOT ", "work." '   TEST b
> It did work.
>
>
> As to the last example you probably need something like this:
>
>    } elsif ( grep( $_ ne ref $data, 'SCALAR', 'HASH', 'ARRAY' ) || grep( $_
> ne $data, 'SCALAR', 'HASH', 'ARRAY' ) ) {
>

it would seem you were right. however, it's still not catching
CAM::PDF::Content=HASH(0x2f27340) as i'm still getting this:

PAGES: 32
PAGE: 8
CAM::PDF::Content=HASH(0x2690340) is CAM::PDF::Content LEVEL: 0 FOUND:
0 CLEAR: 0
CAM::PDF::Content=HASH(0x2690340) is CAM::PDF::Content
DONE

i think the problem is that i'm checking if it exactly equals 'HASH'
or whatever and it's giving me the result in an object. i don't really
want to do =~ /HASH/ because if there's a phrase in the pdf with the
word, i don't want it to match and this just doesn't seem like the
proper way to do things.

below is my function:

sub datarec {
   my( $data, $regex, $lvl, $found, $clear ) = @_;
   $lvl ||= 0;
   $found ||= 0;
   $clear ||= 0;
   return -1 unless( $data );

print "$data is ", ref( $data ), " LEVEL: $lvl FOUND: $found CLEAR: $clear\n";
   if(  ref( $data ) eq 'SCALAR' || $data eq 'SCALAR' ) {
      if( $data =~ /$regex/ ) {
         $datdepth->[ $found + 1 ] = $datdepth->[ $found ];
         $datdepth->[ $found++ ]->[ $lvl ] = $data;
         $clear = 0;
      } else {
         return $clear = 1;
      }
   } elsif( ref( $data ) eq 'HASH' || $data eq 'HASH' ) {
      foreach( values %$data ) {
         $datdepth->[ $found ]->[ $lvl ] = $data;
         $clear = datarec( $_, $regex, $lvl + 1, $found );
      }
   } elsif( ref( $data ) eq'ARRAY' || $data eq 'ARRAY' ) {
      foreach( @$data ) {
         $datdepth->[ $found ]->[ $lvl ] = $data;
         ( $found, $clear ) = datarec( $_, $regex, $lvl + 1, $found );
      }
   } elsif( ref( $data ) eq 'REF' || $data eq 'REF' ) {
      $datdepth->[ $found ]->[ $lvl ] = $data;
      ( $found, $clear ) = datarec( $_, $regex, $lvl + 1, $found );
   } elsif ( grep( $_ ne ref $data, 'SCALAR', 'HASH', 'ARRAY' ) ||
grep( $_ ne $data, 'SCALAR', 'HASH', 'ARRAY' ) ) {
      print "$data is ", ref( $data ), "\n";
   } else {
      print "SOMETHING HORRIBLE!\n";
   }

   if( $clear and $clear == 1 ) {
      foreach my $i ( $lvl .. $#{ $datdepth->[ $found ] } ) {
         undef( $datdepth->[ $found ]->[ $i ] );
      }
   }
}

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to