Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > what's wrong with <DATA>

Reply
Thread Tools

what's wrong with <DATA>

 
 
cyl
Guest
Posts: n/a
 
      04-03-2006
my script is as below

$i=1;
while(<DATA>){
$x=`dir $_`;
print $i++.". $_";
}
__DATA__
123.txt
456.txt
789.txt

and the output is
1. 123.txt
2.
3. 456.txt
4.
5. 789.txt

Can anyone shed a light on why the extra blank appeared? Thanks.

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      04-03-2006
cyl wrote:
> my script is as below
>
> $i=1;
> while(<DATA>){
> $x=`dir $_`;
> print $i++.". $_";
> }
> __DATA__
> 123.txt
> 456.txt
> 789.txt
>
> and the output is
> 1. 123.txt
> 2.
> 3. 456.txt
> 4.
> 5. 789.txt
>
> Can anyone shed a light on why the extra blank appeared? Thanks.


Doesn't do it here. Maybe you
--
Josef Mo"llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      04-03-2006
cyl wrote:
> my script is as below
>
> $i=1;
> while(<DATA>){
> $x=`dir $_`;
> print $i++.". $_";
> }
> __DATA__
> 123.txt
> 456.txt
> 789.txt
>
> and the output is
> 1. 123.txt
> 2.
> 3. 456.txt
> 4.
> 5. 789.txt
>
> Can anyone shed a light on why the extra blank appeared? Thanks.


Can't reproduce.
Maybe you have CR+LF in your file?


--
Josef Mo"llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-03-2006
cyl <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> my script is as below
>
> $i=1;
> while(<DATA>){
> $x=`dir $_`;
> print $i++.". $_";
> }
> __DATA__
> 123.txt
> 456.txt
> 789.txt


You're running without "strict". Switch it on and declare your variables.
What is the variable $x for? You're not using it.

> and the output is
> 1. 123.txt
> 2.
> 3. 456.txt
> 4.
> 5. 789.txt
>
> Can anyone shed a light on why the extra blank appeared? Thanks.


I don't see that output, it prints three lines as expected. What kind
of line feeds does your source file have and what are the settings of
$/ and $\?

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      04-03-2006
cyl <(E-Mail Removed)> wrote:
> my script is as below


> $i=1;
> while(<DATA>){
> $x=`dir $_`;
> print $i++.". $_";
> }
> __DATA__
> 123.txt
> 456.txt
> 789.txt


> and the output is
> 1. 123.txt
> 2.
> 3. 456.txt
> 4.
> 5. 789.txt


> Can anyone shed a light on why the extra blank appeared? Thanks.


It doesn't on my system. Maybe you have some non-printing characters
embedded after the 'txt' portion of each datum.

Axel
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      04-03-2006
cyl wrote:
> my script is as below
>
> $i=1;
> while(<DATA>){
> $x=`dir $_`;
> print $i++.". $_";
> }
> __DATA__
> 123.txt
> 456.txt
> 789.txt
>
> and the output is
> 1. 123.txt
> 2.
> 3. 456.txt
> 4.
> 5. 789.txt
>
> Can anyone shed a light on why the extra blank appeared? Thanks.


I have no answers for you, but I will state that unlike the rest of the
responders, I did duplicate your issue. Under Windows 2000
Professional and ActiveState's Perl 5.8.4, the following code (modified
from the above to agree with the Posting Guidelines),

#!/usr/bin/perl
use strict;
use warnings;

my $i = 1;
while (<DATA>) {
chomp;
print "$i: '$_'\n";
$i++;
my $x = qx"dir $_";
}

__DATA__
123.txt
456.txt
789.txt

gave the same output as above. (Except that mine also printed three
"file not founds" because I didn't have those three files).

Very interesting to note is that after I duplicated your problem and
couldn't explain why, I rebooted my computer for an unrelated reason.
I just went back to the same program, without modifying anything, and
now the code works as I expect it to. I get three lines of output,
plus the File Not Founds.

I have no idea what was the original cause of the problem, but the fact
that a reboot solved it leads me to blame Windows in general. I'm
sorry I can't offer more assistance than that.

Paul Lalli

 
Reply With Quote
 
cyl
Guest
Posts: n/a
 
      04-04-2006
My script is written in Windows so it has CRLF in the end of a line. I
tested it again in both Windows and Linux and only got this problem in
Windows. If I convert the CRLF to LF, the problem disappears. Another
interesting thing to me is that if I comment the system call, the
problem won't happen either even with CRLF in the end. Like this

$i=1;
while(<DATA>){
#$x=`dir $_`;
print $i++.". $_";
}
__DATA__
123.txt
456.txt
789.txt

output is
1. 123.txt
2. 456.txt
3. 789.txt

so there might be something during the system call that affect the file
handle DATA?

 
Reply With Quote
 
Brad Murray
Guest
Posts: n/a
 
      04-04-2006
cyl <(E-Mail Removed)> wrote:
c> My script is written in Windows so it has CRLF in the end of a line. I
c> tested it again in both Windows and Linux and only got this problem in
c> Windows. If I convert the CRLF to LF, the problem disappears. Another
c> interesting thing to me is that if I comment the system call, the
c> problem won't happen either even with CRLF in the end. Like this
c>
c> $i=1;
c> while(<DATA>){

chomp;

c> #$x=`dir $_`;
c> print $i++.". $_";
c> }
c> __DATA__
c> 123.txt
c> 456.txt
c> 789.txt

Why not chomp your data if you don't want the newlines? No matter how
you slice it, the newlines are probably not intended to be part of
your filenames, so why try to make things work with them intact?

--
Brad Murray * "This won't help relations. Marines taking over a Baghdad
VSCA Founder * hotel. [yelling]" -- CNN closed captioning error
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      04-04-2006
cyl wrote:
> My script is written in Windows so it has CRLF in the end of a line. I
> tested it again in both Windows and Linux and only got this problem in
> Windows. If I convert the CRLF to LF, the problem disappears. Another
> interesting thing to me is that if I comment the system call, the
> problem won't happen either even with CRLF in the end. Like this
>
> $i=1;
> while(<DATA>){
> #$x=`dir $_`;
> print $i++.". $_";
> }
> __DATA__
> 123.txt
> 456.txt
> 789.txt
>
> output is
> 1. 123.txt
> 2. 456.txt
> 3. 789.txt
>
> so there might be something during the system call that affect the file
> handle DATA?


Not being a conoisseur of the software that comes from the northwest of
the US, I venture to put it the other way round: Maybe the DATA
containing CRLFs affects the working of the backticks operation?

Like: `dir 123.txt<CR><LF>` causes two operations to happen:
- execute "dir 123.txt"
- echo CRLF

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Ch Lamprecht
Guest
Posts: n/a
 
      04-04-2006
Josef Moellers wrote:
> cyl wrote:


>> $i=1;
>> while(<DATA>){
>> #$x=`dir $_`;
>> print $i++.". $_";
>> }
>> __DATA__
>> 123.txt
>> 456.txt
>> 789.txt
>>


> Not being a conoisseur of the software that comes from the northwest of
> the US, I venture to put it the other way round: Maybe the DATA
> containing CRLFs affects the working of the backticks operation?
>
> Like: `dir 123.txt<CR><LF>` causes two operations to happen:
> - execute "dir 123.txt"
> - echo CRLF
>

The backticks work as expected. The question is: why do the backticks
change the behaviour of <DATA>...

use warnings;
use strict;
while(<DATA>){
` `;
print "$. $_";
}
__DATA__
123.txt
456.txt
789.txt

On WinXP, -in case the file has CRLF EOLs- this prints:

1 123.txt
2
3 456.txt
4
5 789.txt

In case the file has LF EOLs or with backticks commented out it prints:
1 123.txt
2 456.txt
3 789.txt

Christoph
--

perl -e "print scalar reverse q/(E-Mail Removed)/"
 
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