in reply to Remove field inside [ ] brackets

I'cant find an error - the following works fine for me:
#!/usr/bin/perl -w use strict; #$slotnumber=0; my $dpdrivloc = "Trinity"; open( DPSLOTFILE, "< c:\\perlen\\perlmonks\\test.txt") or die "Can't open file: $!"; while ( <DPSLOTFILE> ) { if ( /$dpdrivloc:\s*(\d+)/ ) { print "Slot for $dpdrivloc: $1\n"; } } #system("c:\\ipbat.bat"); close DPSLOTFILE;
Don't forget to use strict; and -w.

Replies are listed 'Best First'.
Re: Re: Remove field inside [ ] brackets
by Anonymous Monk on Mar 22, 2004 at 17:32 UTC
    Okay, I figured it out...  The file is output from 
    another program, I created a batch file:
    
    
    rem Convert to text format type test.txt > testnew.txt
    From there, I made a call to that batch file:
    system("c:\convert.bat");
    I then ran the code against testnew.txt and it worked!!! Wahoo!!!!! THANK YOU SO MUCH!!!!!
      The file is output from another program...

      Based on your evidence, I would guess that the other program runs on some other (non-Microsoft) OS, or else has some other reason for producing text with just "LF" instead of "CRLF" line termination. If you get to understand the nature of your input data better, you can get Perl to deal with it directly -- set the "$/" (input record separator) variable to match whatever that other program is using at the end of each line.

      Perl is actually a useful tool for this sort of closer inspection of input data. Consider:

      #!/usr/bin/perl use strict; $/ = undef; # go into "slurp" mode $_ = <>; # read whole file at once # (this assumes the file name is given in @ARGV) my @chars = split //; # each array element holds on character my $nlf = grep /\x0a/, @chars; my $ncr = grep /\x0d/, @chars; printf( "Input contained %d characters, %d line-feeds, %d carriage-ret +urns\n", scalar @chars, $nlf, $ncr );