Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

unGodly problems with pop()

by Anonymous Monk
on Jul 13, 2001 at 05:50 UTC ( [id://96261]=perlquestion: print w/replies, xml ) Need Help??

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

Ok, here's a really weird one that I can't figure out for the life of me. I've got a scaler called $rest. The last element(word) in $rest is supposed to be a password, whether $rest is 1 element or 432678 elements.

After reading up, I found the sexy pop function.. but since it only works on array's I did a little diddling with $rest

(@rest = split(/ /, $rest);
so that it would be in array form to use pop. well, straightforward,
$apass = pop @rest;
doesn't work, I
print "ATTEMPT PASS: $apass\n";
and get nothing at all. I thought perhaps maybe I was just a really bad coder and $rest or @rest was empty, but I can
print "\$REST: $rest\n";
and
foreach $i @rest { print "\@REST: $i\n";
and I get data everytime! (the correct data!)

So I did more looking and saw that pop @rest; should return $_, so I

print "\$_: $_\n";
and get NO data other than my "$_" literal...

I'm SO stumped on this, it isnt even funny. Thanks for the help! Terron the lamer

Edit: chipmunk 2001-07-12
Edit: neophyte 2001-07-13 for better readability

Replies are listed 'Best First'.
Re: unGodly problems with pop()
by bikeNomad (Priest) on Jul 13, 2001 at 05:57 UTC
    Sounds like you're on the right track. You are using strict, aren't you? Perhaps you have a typo. This program:
    use strict; my $rest = 'a b c d password'; my @rest = split(/ /, $rest); print "\@rest=@rest\n"; my $apass = pop @rest; print "PASS: $apass\n"; print "\@rest=@rest\n";
    produces:
    @rest=a b c d password PASS: password @rest=a b c d

    update: And maybe you have a space at the end of your scalar, and so the last element is empty. You may want to print out the scalar first with something around it like print "($rest)\n" so you can see it.

Re (tilly) 1: unGodly problems with pop()
by tilly (Archbishop) on Jul 13, 2001 at 05:59 UTC
    I have 2 guesses.

    The first is that you have a typo in a variable name and so don't have data where you think you should. Been there, done that, strict.pm is your friend for catching that "D'oh!" that happens to all of us.

    The second (and more likely) issue is that your split on every character is not what you wanted. Your last character is some kind of white-space, and so what happens is that you do have something there, but you don't see it in your debugging statement. The first answer to that is to put |'s in your statement so that white-space is easier to see. The second is to split on something other than the space between characters...

Re: unGodly problems with pop()
by synapse0 (Pilgrim) on Jul 13, 2001 at 06:22 UTC
    the others that have posted have given good info on some troubleshooting tips. Another (as was suggested, there may be whitespace at the end that's fouling you up) is to try:
    # get rid of trailing whitespace chomp $rest; # split on consecutive whitespace # (includes tabs and mult spaces) @rest_ary = split(/\s+/, $rest); # grab last item $pass = $rest_ary[$#rest_ary]; print "pass = $pass\n"; # just to double check - for ($x = 0; $x <= $#rest; $x++) { print "array item $x = $rest[$x]\n"; }
    That may shed some light...
    -Syn0
(Fixed) Re: unGodly problems with pop()
by Anonymous Monk on Jul 13, 2001 at 06:57 UTC
    goddamn you guys are too useful! There was a \r (return) and \n (newline) at the end of $rest.. imagine that! I chop'ed it and did a /\r/ thingide.. and whop-de-doo, it worked! thanks(100000000000000000);
      This is a bit of a quibble, but don't use chop to get rid of newlines like \r and \n. Use chomp instead. It'll only get rid of newlines at the end, not the last character. So, you can safely chomp anything without verifying that it indeed does have newlines at the end.
      also, realize your script will break on passwords with spaces... so be careful!

      ~Particle

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://96261]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found