Openning a File

Discussion in 'Perl Misc' started by MattM, Feb 21, 2008.

  1. MattM

    MattM Guest

    This should be fairly straightforward, but I'm having trouble openning
    a file. I'm modifying a current perl script that seemed to run fine.
    I open a file for input, parse it, make some changes to it, create
    another file, and write the changes to the new file. I wanted to open
    up another file for input, but when I do, it seems to be adding spaces
    between each character in the line as its read in. On of the lines of
    the file looks something like:

    ZZZ223 FP2115

    the columns are seperated by a tab. What I get when I run the simple
    file openning code is:

    Z Z Z 2 2 3 F P 2 1 1 5

    I'm trying to put these into a hash table for quick access later in
    the program. The code I'm using is pretty simple. It looks like:

    open MYFILE,"myfile.txt" or die "Can't open trigger file: $!\n";

    while (<MYFILE>) {

    chomp;

    print STDOUT $_ . "\n";

    $lineIn = $_;

    ($parm,$trigger) = split /\s+/, $lineIn;


    $triggers{$parm} .= $trigger;

    }

    close MYFILE;


    But when I run this I get the strange spacing problem. Any Ideas?
    Thanks...
     
    MattM, Feb 21, 2008
    #1
    1. Advertising

  2. MattM

    david Guest

    On Feb 21, 4:46 pm, MattM <> wrote:
    > This should be fairly straightforward, but I'm having trouble openning
    > a file. I'm modifying a current perl script that seemed to run fine.
    > I open a file for input, parse it, make some changes to it, create
    > another file, and write the changes to the new file. I wanted to open
    > up another file for input, but when I do, it seems to be adding spaces
    > between each character in the line as its read in. On of the lines of
    > the file looks something like:
    >
    > ZZZ223 FP2115
    >
    > the columns are seperated by a tab. What I get when I run the simple
    > file openning code is:
    >
    > Z Z Z 2 2 3 F P 2 1 1 5
    >
    > I'm trying to put these into a hash table for quick access later in
    > the program. The code I'm using is pretty simple. It looks like:
    >
    > open MYFILE,"myfile.txt" or die "Can't open trigger file: $!\n";
    >
    > while (<MYFILE>) {
    >
    > chomp;
    >
    > print STDOUT $_ . "\n";
    >
    > $lineIn = $_;
    >
    > ($parm,$trigger) = split /\s+/, $lineIn;
    >
    > $triggers{$parm} .= $trigger;
    >
    > }
    >
    > close MYFILE;
    >
    > But when I run this I get the strange spacing problem. Any Ideas?
    > Thanks...


    Hi,
    I ran your program on the sample line but did not observe the problem.
    Best regards,
    Michael
     
    david, Feb 21, 2008
    #2
    1. Advertising

  3. MattM

    J. Gleixner Guest

    MattM wrote:
    > This should be fairly straightforward, but I'm having trouble openning
    > a file. I'm modifying a current perl script that seemed to run fine.
    > I open a file for input, parse it, make some changes to it, create
    > another file, and write the changes to the new file. I wanted to open
    > up another file for input, but when I do, it seems to be adding spaces
    > between each character in the line as its read in. On of the lines of
    > the file looks something like:


    Instead of 'something like', provide the actual lines.

    >
    > ZZZ223 FP2115
    >
    > the columns are seperated by a tab. What I get when I run the simple
    > file openning code is:
    >
    > Z Z Z 2 2 3 F P 2 1 1 5

    [...]

    Post an actual example that displays the issue. Since we don't have
    access to your 'myfile.txt' and you didn't show the code that's
    doing something with your hash, there's no way anyone can reproduce
    your issue.

    e.g.

    my $lineIn = 'ZZZ223 FP2115';
    my ($parm,$trigger) = split /\s+/, $lineIn;
    print "parm=$parm trigger=$trigger\n";

    parm=ZZZ223 trigger=FP2115
     
    J. Gleixner, Feb 21, 2008
    #3
  4. MattM

    MattM Guest

    When I tried to create a smaller subset of "myfile.txt" that I copied
    from the larger file, the code worked ok. Just for the heck of it, I
    opened up the larger file in Hex. My text editor indicated that there
    were null characters in between the valid alphanumeric values. When
    the editor is allowed to display that larger file, it must
    automatically remove the nulls, because it appeared as subsequent
    valid alphanumeric values. When I copied some of the values to the
    smaller file, it seems to have removed the nulls as well. When I
    brought up the smaller file in Hex, it didn't have the nulls between
    the alphanumeric values. The process of copying the file removed the
    nulls. Not sure why. But anyway, thanks for the input. It made me
    dig a little deeper....

    J. Gleixner wrote:
    > MattM wrote:
    > > This should be fairly straightforward, but I'm having trouble openning
    > > a file. I'm modifying a current perl script that seemed to run fine.
    > > I open a file for input, parse it, make some changes to it, create
    > > another file, and write the changes to the new file. I wanted to open
    > > up another file for input, but when I do, it seems to be adding spaces
    > > between each character in the line as its read in. On of the lines of
    > > the file looks something like:

    >
    > Instead of 'something like', provide the actual lines.
    >
    > >
    > > ZZZ223 FP2115
    > >
    > > the columns are seperated by a tab. What I get when I run the simple
    > > file openning code is:
    > >
    > > Z Z Z 2 2 3 F P 2 1 1 5

    > [...]
    >
    > Post an actual example that displays the issue. Since we don't have
    > access to your 'myfile.txt' and you didn't show the code that's
    > doing something with your hash, there's no way anyone can reproduce
    > your issue.
    >
    > e.g.
    >
    > my $lineIn = 'ZZZ223 FP2115';
    > my ($parm,$trigger) = split /\s+/, $lineIn;
    > print "parm=$parm trigger=$trigger\n";
    >
    > parm=ZZZ223 trigger=FP2115
     
    MattM, Feb 21, 2008
    #4
    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. Mel
    Replies:
    5
    Views:
    511
    Jan Harders
    Jul 18, 2004
  2. DJ
    Replies:
    6
    Views:
    347
    John Harrison
    Oct 27, 2004
  3. Replies:
    1
    Views:
    296
    Alf P. Steinbach
    Apr 17, 2006
  4. Replies:
    2
    Views:
    283
    Alf P. Steinbach
    Apr 17, 2006
  5. ER
    Replies:
    2
    Views:
    513
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 12, 2007
Loading...

Share This Page