Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

chop is chopping off the last letter

by vtprogrammer (Friar)
on Jul 05, 2000 at 23:44 UTC ( [id://21203]=perlquestion: print w/replies, xml ) Need Help??

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

I'm comparing passwords in a script I'm writing. I get the user ID and password like this:
<t> ($userid, $pword) = (split(/\|/, $record));
<t> chop($pword);
The problem is that chop chops off the last letter of the last users password. How do I fix this. Any help or suggestions are greatly aprreciated. Thanks.

Replies are listed 'Best First'.
RE: chop is chopping off the last letter
by vroom (His Eminence) on Jul 05, 2000 at 23:49 UTC
Re: chop is chopping off the last letter
by plaid (Chaplain) on Jul 05, 2000 at 23:51 UTC
    I don't quite understand your question. You're chopping a variable, but you don't want it to chop off the last letter? If you don't want it to chop off the last letter, then why are you chopping at all? The way to fix it would just be to not chop.

    If what you want is to get rid of any trailing newlines, but not do anything if there aren't any, take a look at chomp, which is a more intelligent version of chop.

Re: chop is chopping off the last letter
by cwest (Friar) on Jul 05, 2000 at 23:52 UTC
    Help? Suggestions?

    Don't use chop()!

    Change your code to say the following and you won't have problems any more. (not that one any how.)

    my( $userid, $pword ) = split /\|/, $record;
    Oye..
    --
    Casey
    
Re: chop is chopping off the last letter
by Anonymous Monk on Jul 12, 2000 at 16:28 UTC
    The problem is probably caused by reading from a file - you get lines (read into @records) as follows:

    user1|pass1\n
    user2|pass2\n
    user3|pass3

    note the last line doesn't have a \n at the end, hence chop chops the last char off rather than the \n

    Again, the easiest way is to just use chomp rather than chop. Otherwise you could hack it by doing

    # add /n to last record
    my $hackyvar = pop @records;
    $hackyvar .= "\n";
    push @records, $hackyvar;

    (you can probably do this in one line but I prefer readable code...)

    and then do your foreach loop after that. Hacky, but it works.

    But use chomp anyway...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-19 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found