-1

This seems really simple but neither a google search nor a stackoverflow search turn anything up. Is there a reason people don't do it this way? Sorry if I'm missing something.

I tried

$B = `wc -l $fileB`;
print "$B\n";
@B_array = split /\s+/, $B; #try to split on whitespace
foreach my $item (@B_array) {
        print "$item\n";
}

but that doesn't split the output of wc -l for some reason.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Michael Starr
  • 7
  • 1
  • 11

2 Answers2

6

Your code works on my computer, perhaps this is one of the reasons people avoid using external tools. You can do this with native Perl of course:

my $lc = 0;
open my $file, "<", "input" or die($!);
$lc++ while <$file>;
close $file;
print $lc, "\n";
perreal
  • 94,503
  • 21
  • 155
  • 181
2

This avoids the split problem, but using cat like this can be considered wasteful of resources.

$B = `cat $fileB | wc -l`;

cat filename | wc -l just produces a number of lines -- wc cannot get a file name as it is reading from a pipe into stdin.

Also @array=split ' ', $B will work for you.

jim mcnamara
  • 16,005
  • 2
  • 34
  • 51