views:

227

answers:

3

I tried to placed ${date[0]} in my directory which is equivalent to 01252010 but @hits not printed. How can I managed to open the directory to get the desired output? Thanks.

ERROR: Unsuccessful open on filename containing newline at ./total.pl line 11, line 1.

#!/opt/perl/bin/perl -w

use strict;

open(FH,"/home/daily/scripts/sms_hourly_stats/date.txt");
my @date = <FH>;
print $date[0];

my $path = "/home/daily/output/sms_hourly_stats/${date[0]}/TOTAL.txt";
open(FILE,"$path") or die "Unable to open $path: $!";
my @hits = <FILE>;
print @hits;

close FH;
close FILE;
+5  A: 

You need to remove line ending symbol. Use chomp:

chomp(my @date = <FH>);
Ivan Nevostruev
alternatively you could also write chomp(@date = <FH>)
bertolami
Still Unable to open /home/sudo724/reports/daily/output/sms_hourly_stats/1/TOTAL.txtThe value of ${date[0]} is 1?Thanks.
Shiel
I think I got it already using chomp(my @date=<FH>). Thank you very much!:-)
Shiel
@Shiel: Can you show content of `date.txt` file?
Ivan Nevostruev
I don't think you want the return value of chomp. `map {chomp; $_}`
Anonymous
@Anonymous: Thanks! I've fixed it
Ivan Nevostruev
A: 
  1. Use chomp on got values, as Ivan suggested
  2. Where did you find syntax ${date[0]} ? use $date[0].
depesz
As I encountered on many scripts,there's a difference between ${date[0]} and $date[0] especially in dealing or concatenating numbers? like $num[0] = 3home/dir/2009${num[0]}0 means 30home/dir/2009$num[0]0 which is error?
Shiel
${date[0]} is "safer" in an interpolated context, but not really necessary here. It's more useful if you have a complicated expression to expand in the interpolated string.
Adam Bellaire
A: 
#!/opt/perl/bin/perl -w

use strict;

open(FH,"/home/daily/scripts/sms_hourly_stats/date.txt");
my @date = <FH>;
my $dir;
print ${date[0]};
chomp($dir = ${date[0]});


my $path = "/home/daily/output/sms_hourly_stats/$dir/TOTAL.txt";
open(FILE,"$path") or die "Unable to open $path: $!";
my @hits = <FILE>;
print @hits;

close FH;
close FILE;
Vijay Sarathi