Where to hitch the <<EOF to plaster input file?

D

Dan Jacobson

Where do I hitch the <<EOF so I can plaster my input file right into
this source file? Yes, saw man perlop. Yes, I am prepared to give up
-anl if that's the price to pay.

#!/usr/bin/perl -anlw
#Input: each line consists of seconds per rotation of electric meter disk followed by
#a note of what appliances were on at the time.
#Output: total watts, appliance list. The Kh factor is written on the meter.
#Reference: http://www.earth.uni.edu/EECP/elem/mod2_math.html
BEGIN { $Kh = 7.2 };
unless (/^[^#]/) { next }
$p = 3600 * $Kh / $F[0];
shift @F;
print int($p), "\t@F";
 
E

Eric Amick

Where do I hitch the <<EOF so I can plaster my input file right into
this source file? Yes, saw man perlop. Yes, I am prepared to give up
-anl if that's the price to pay.

You'll have to give up the -anl; <<EOF can't put data into a filehandle.
Despite its resemblance to shell here-documents, it doesn't work the
same.
#!/usr/bin/perl -anlw
#Input: each line consists of seconds per rotation of electric meter disk followed by
#a note of what appliances were on at the time.
#Output: total watts, appliance list. The Kh factor is written on the meter.
#Reference: http://www.earth.uni.edu/EECP/elem/mod2_math.html
BEGIN { $Kh = 7.2 };
unless (/^[^#]/) { next }
$p = 3600 * $Kh / $F[0];
shift @F;
print int($p), "\t@F";

#!/usr/bin/perl -w
$Kh = 7.2;
while (<DATA>)
{
next if /^#/;
($p, @F) = split(' ', chomp);
$p = 3600 * $Kh / $p;
print int($p), "\t@F";
}
__DATA__
# Your data here...

See man perldata and search for __DATA__.
 
T

Tad McClellan

Eric Amick said:
($p, @F) = split(' ', chomp);


split() will always return a 1-element list, regardless of what is in $_.

@F will always get the empty list, regardless of what is in $_.

$p will get either a 1 or a 0 ( if you haven't messed with $/ ).

Man oh man, that bug is going to present itself strangely.


The return value from chomp() is not very useful.

Seeing someone use it, therefore, is a red flag to me...
 
E

Eric Amick

split() will always return a 1-element list, regardless of what is in $_.

@F will always get the empty list, regardless of what is in $_.

$p will get either a 1 or a 0 ( if you haven't messed with $/ ).

Man oh man, that bug is going to present itself strangely.

I was trying to be too clever. I wasn't thinking.

Make that

chomp;
($p, @F) = split;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top