What's wrong

Discussion in 'Perl Misc' started by Alex, Oct 25, 2006.

  1. Alex

    Alex Guest

    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!
     
    Alex, Oct 25, 2006
    #1
    1. Advertising

  2. 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
     
    Gunnar Hjalmarsson, Oct 25, 2006
    #2
    1. Advertising

  3. Alex <> wrote:

    > Subject: What's wrong



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 25, 2006
    #3
  4. Alex

    Joe Smith Guest

    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
     
    Joe Smith, Oct 25, 2006
    #4
  5. Alex

    Dr.Ruud Guest

    hexdump (was: Re: What's wrong)

    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."
     
    Dr.Ruud, Oct 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salisha Khan
    Replies:
    1
    Views:
    11,705
    JohnFol
    Aug 1, 2003
  2. Replies:
    2
    Views:
    403
  3. Pial
    Replies:
    4
    Views:
    57,075
    Phatman
    Aug 27, 2010
  4. Paul Leventis

    Re: Quartus bug or wrong VHDL?

    Paul Leventis, Jun 24, 2003, in forum: VHDL
    Replies:
    0
    Views:
    2,515
    Paul Leventis
    Jun 24, 2003
  5. Matthew
    Replies:
    7
    Views:
    878
    Priscilla Walmsley
    Jan 7, 2005
Loading...

Share This Page