Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What's wrong

Reply
Thread Tools

What's wrong

 
 
Alex
Guest
Posts: n/a
 
      10-25-2006
Here is my perl script:

my %files;
my $filepath = "test.txt";
open (IN, $filepath);
while (<IN>) {
chomp;
my $file = $_;
$files{$file} = $file;
}
close IN;

my $code = 10;
if (exists $files{$code}) {
print "find key\n";
}
exit;

here is test.txt file:
10
20
30

When I run the script, there is no print "find key". However, if I change
the scipt to:


my %files;
my $filepath = "test.txt";
open (IN, $filepath);
while (<IN>) {
chomp;
my $file = substr($_,0,2);
$files{$file} = $file;
}
close IN;

my $code = 10;
if (exists $files{$code}) {
print "find key\n";
}
exit;

It will print "find key". I could find out what's wrong. Why change

my $file = $_;
to
my $file = substr($_,0,2);

will work.

PS: in test.txt file, after type 10 followed by Enter key.

Thanks in advance!



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      10-25-2006
Alex wrote:
> Here is my perl script:
>
> my %files;
> my $filepath = "test.txt";
> open (IN, $filepath);
> while (<IN>) {
> chomp;
> my $file = $_;
> $files{$file} = $file;
> }
> close IN;
>
> my $code = 10;
> if (exists $files{$code}) {
> print "find key\n";
> }
> exit;
>
> here is test.txt file:
> 10
> 20
> 30
>
> When I run the script, there is no print "find key".


A wild guess: test.txt contains Windows newlines ( \015\012 ), while the
script is run on *nix ( \012 ).

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-25-2006
Alex <(E-Mail Removed)> wrote:

> Subject: What's wrong



There is no subject in your Subject, that's what.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      10-25-2006
Alex wrote:
> Why change
> my $file = $_;
> to
> my $file = substr($_,0,2);
> will work.


You should get perl to print out what it is actually working with.
Change
chomp;
to
print "Before: ",(unpack "H*",$_),"\n";
chomp;
print "After: ",(unpack "H*",$_),"\n";
to see if there are more than two characters on the line after chomp().

-Joe
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      10-25-2006
Joe Smith schreef:

> print "Before: ",(unpack "H*",$_),"\n";


To get the bytes separated, one can do

{ local ($\, $,) = ("\n", '.') ;
print "Before: ", unpack('(H2)*', $_) ;
}

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Have I bought wrong product? enquirer Wireless Networking 2 06-10-2005 10:59 PM
Zero Config keeps connecting to the wrong AP =?Utf-8?B?ZGdyaWZmaXRo?= Wireless Networking 2 03-04-2005 05:52 PM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
wrong connection status Peter Welk Wireless Networking 0 12-22-2004 03:26 PM
XP SP2 Wrong IP on connection D Wells Wireless Networking 3 12-09-2004 03:35 AM



Advertisments