Solved

Reading from a text file: problems in removing the terminating character

Posted on 2003-11-12
7
269 Views
Last Modified: 2013-12-25
Hi Experts,

I am having some problem in removing the newline character from the string that is read from a text file.  I have the source code below.  The problem is if I have the username password pair as myname:password it says that the password has 9 characters.  The chomp seems to not remove the newline character.  Please provide me with some advice as to solve the problem.  This problem preventing me from going further with my assignment.

Thanks a lot,

-Thanesh
#!/usr/bin/perl --

use CGI qw( :standard );  

$dtd =

$theUsername = param( "username" );

$thePassword = param( "password" );

open(INFILE, "<password.txt") or die("The database could not be opened.");
$count = 0;
$userVerified = 0;
$passwordVerified = 0;

chomp $thePassword;
while (( $line =<INFILE> ) && (!$userVerified )) {
   
    $count = $count + 1;
    chomp( $line );
    ( $username, $password ) = split( /:/, $line );


   chomp($password);
   chomp($username);

    print( header() );
    print("<br>");
     if ( $theUsername eq $username ) {
           print("User name matches");
             print("<br>");
         $userVerified=1;  
             $len = (length($password));
             print("Length of password is: $len");
              print("<br>");

             $theLen = (length($thePassword));
             print("Lenth of input password is: $theLen");
              print("<br>");

         if ( $thePassword eq $password ) {
                print("Password matches");
                  print("<br>");
                $passwordVerified = 1;
            last;
         }
             print("The password don't match");
     }
}
close(INFILE);


print( header() );

#print( start_html( { dtd => $dtd,title => "First Perl Script" } ) );
print("thePassword: $thePassword");
print("The user name is: $theUsername\n\n");
print("<br>");
print("The user name is verified: $userVerified\n");
print("<br>");
print("The password is verified: $passwordVerified\n");

print("<br>");

print("The password is: $password");
print("<br>");
print("The user name processed: $username");
print("<br>");
print("The count is: $count");
#print( end_html() );




      
0
Comment
Question by:thanesh
7 Comments
 
LVL 1

Author Comment

by:thanesh
ID: 9736839
Hi,
As I investigate it further I found that the chomp does take out the newline character, but there seems to be TWO newline characters.  So, the problem is how to get rid of the second one?
0
 
LVL 1

Author Comment

by:thanesh
ID: 9736881
May be the second one is not a newline character but some other trailing character( non-visible)???
0
 
LVL 2

Accepted Solution

by:
rootkiddy earned 25 total points
ID: 9742239
Brief description of chomp: chomp basically removes what is in $/.  if $/ is empty then it removes all newlines from the string.

Since you are reading the file one line at a time then there shouldn't be a problem with mutliple newlines.  Your last post is probably accurate.  It may be something like a space, tab, or any other variable.  If this is a problem for you then you may consider doing something like

$password =~ s/^([\d\w]+).*/$1/;

Basically take what are valid characters and remember then and ignore everything else.  Note that if you have control over what goes into your file then you may want to make sure information is put in correctly.  Let me know if this helps or you have problems with this.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 9757277
\d is included in \w
If  password.txt was created under DOS it may use \r\n to end a line
0
 
LVL 1

Expert Comment

by:robysath
ID: 9761602
I agree with ozo, dos/windows puts a CR/LF (Carriage Return + Line Feed) as a new line.

Unix-like systems look only for LF and I think MACs look for the CR.

That's also why text files will have a larger size on a windows machine, yet smaller on a unix-like system if you transfer them to and fro in ascii mode.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sums of coloumns in html/java 15 85
Recursively Delete Files 5 103
Identify two keyboard codes and how to enter them 9 69
Exchange Server 2013 Message Tracking log Powershell ? 11 85
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question