HamNRye has asked for the wisdom of the Perl Monks concerning the following question:

OK, I know that this is probably an error on my part, but....

I am working on getting PDF's of our newspaper online. There will be a JPG thumbnail, etc... When trying to get the sections for the day, Push is not doing what I expect.

I am expecting that the "tmp" array will have 1 element per page when this is done. (e.g. GGGHHHHHHJJJ...) Then I can send this out and have the duplicates removed, and I have a listing of my sections.

The problem is that the sections are not getting pushed into the array. When the print statement is uncommented, I see all of the sections printed out, but I can never get them to push onto the stack.

I have tried several different ways of writing this, even putting the substr command into the push. push @tmp, substr($fn, 5, 1); I have also tried making another sub and sending the variables, but the values come over improperly. This makes me pretty sure it's my code, but I really cannot tell where.

Any help would be appreciated.
~Hammy

Here is my input:

R3012G006.PDF R3012G007.PDF R3012G008.PDF R3012H001.PDF R3012H002.PDF R3012H003.PDF R3012H004.PDF R3012H005.PDF R3012H006.PDF R3012J001.PDF R3012J002.PDF R3012J003.PDF R3012J004.PDF R3012J005.PDF R3012J006.PDF R3012J007.PDF R3012J008.PDF R3012J009.PDF R3012J010.PDF R3012J011.PDF R3012J012.PDF R3012J013.PDF R3012J014.PDF R3012J015.PDF R3012J016.PDF R3012J017.PDF R3012J018.PDF R3012J019.PDF R3012J020.PDF R3012J021.PDF R3012J022.PDF R3012J023.PDF R3012J024.PDF R3012J025.PDF R3012J026.PDF R3012J027.PDF R3012J028.PDF R3012J029.PDF R3012J030.PDF R3012J031.PDF R3012J032.PDF

Here is the code.

sub make_sections { my @tmp; for (@_) { my $fn = $_; print "$fn\n"; my $pnum = substr($fn, 6, 3); $pnum =~ s/0//; my $section = substr($fn, 5, 1); #@tmp = add2arr(@tmp, $section); push @tmp, $section; $MAIN::PageInfo{$section}{$pnum} = $_; s/$MAIN::fileExt/$MAIN::imgExt/; $MAIN::PageInfo{$section}{$fn} = $_; #print "Name: $PageInfo{$section}{$pnum} Section: $section P +Num: $pnum Thumb: $PageInfo{$section}{$fn} \n"; } print "Exiting Make Sections.\n"; return @tmp; } sub add2arr { my (@array, $value) = shift; #print "Section: $value"; push @array, $value; return @array; }

Replies are listed 'Best First'.
Re: Push can shove it.
by chromatic (Archbishop) on Feb 23, 2002 at 00:16 UTC
    I can't see any obvious problems except for this:
    my (@array, $value) = shift;
    You'll end up with a one-element array and nothing in $value. Try:
    my (@array, $value) = @_;
    Of course, that makes @array slurp up all of the arguments, leaving nothing for $value. Either pass a reference to an array or put the scalars first.

    If you assign the results of make_sections() similarly, there's your error.

    For debugging purposes, print the contents of @tmp before returning.

Re: Push can shove it.
by dmmiller2k (Chaplain) on Feb 23, 2002 at 03:19 UTC

    One thing that jumped out at me, not that it answers your question, is the regex substitution:

    $pnum =~ s/0//;

    which, admittedly will work as long as $pnum continues to always have a leading 0. What about a 100 page document? $pnum of 100 will become 10. Instead, you probably want:

    $pnum =~ s/^0//;

    or, even better:

    $pnum =~ s/^0//;

    which may actually be marginally faster (for what its worth), but more importantly won't break on page numbers 100+.

    We now return you to your regularly scheduled discussion... :)

Re: Push can shove it.
by maverick (Curate) on Feb 23, 2002 at 00:24 UTC
    the shift in add2arr only shifts off the FIRST element of the params, in this case the first element of @tmp. If you rewrote add2arr like
    sub add2arr { my ($value,@tmp) = @_; push(@tmp,$value); return @tmp; } # and call it like @tmp = add2arr($value,@tmp)
    it should work. BUT you shouldn't do this at all. You should do
    push(@tmp,$value)
    directly inside make_sections.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

      Thanks guys, I found the problem.

      It was variable going out of scope that was not detected by use strict or -w.

      BTW, if push(@tmp,$value); had worked initially, I wouldn't have needed to post. You may notice that the sub call is commented out, and there is a lovely (non-working) push statement directly below it.

      The basic debug was just placing everything in its proper scope manually. (You see all of those MAIN:: vars...) ~Hammy

        I call.
        my @args = ( 1 .. 3 ); push @args, 'foo'; print "@args\n";
        If this does not work on your machine, your version of Perl is horribly broken.

        I seriously doubt that to be the case.

Re: Push can shove it.
by trs80 (Priest) on Feb 23, 2002 at 01:12 UTC
    It might be a scoping issue, you didn't supply enough code for how you are feeding the list to the make_sections. Here is some code that seems to produce what you wanted if I understand correctly.
    use Data::Dumper; my @tmp; @data = <DATA>; make_sections(@data); print Dumper(\@tmp); sub make_sections { my $tmp; for (@_) { my $fn = $_; print "$fn\n"; my $pnum = substr($fn, 6, 3); $pnum =~ s/0//; my $section = substr($fn, 5, 1); push @tmp, $section; #$MAIN::PageInfo{$section}{$pnum} = $_; #s/$MAIN::fileExt/$MAIN::imgExt/; #$MAIN::PageInfo{$section}{$fn} = $_; #print "Name: $PageInfo{$section}{$pnum} Section: $se +ction PNum: $pnum Thumb: $PageInfo{$section}{$fn} \n"; } print "Exiting Make Sections.\n"; return @tmp; } __DATA__ R3012G006.PDF R3012G007.PDF R3012G008.PDF R3012H001.PDF R3012H002.PDF R3012H003.PDF R3012H004.PDF R3012H005.PDF R3012H006.PDF R3012J001.PDF R3012J002.PDF R3012J003.PDF R3012J004.PDF R3012J005.PDF R3012J006.PDF R3012J007.PDF R3012J008.PDF R3012J009.PDF R3012J010.PDF R3012J011.PDF R3012J012.PDF R3012J013.PDF R3012J014.PDF R3012J015.PDF R3012J016.PDF R3012J017.PDF R3012J018.PDF R3012J019.PDF R3012J020.PDF R3012J021.PDF R3012J022.PDF R3012J023.PDF R3012J024.PDF R3012J025.PDF R3012J026.PDF R3012J027.PDF R3012J028.PDF R3012J029.PDF R3012J030.PDF R3012J031.PDF R3012J032.PDF
    Some modifications may be needed to meet your goal, but I think it provides the results you are loooking for.