monkeyking monkeyking - 3 months ago 7
Perl Question

How to print variables in Perl

I'm extremely new in Perl.

I have some code that looks like

my ($ids,$nIds);
while (<myFile>){
chomp;
$ids.= $_." ";
$nIds++;
}


This should concatenate every line in my
myFile
, and
nIds
should be my number of lines.
How do i print out my
$ids
and
$nIds
.

I tried simply
print $ids
, but Perl complains.

my ($ids,$nIds)


Is a list right? with 2 elements?

Answer
print "Number of lines: $nids\n";
print "Content: $ids\n";

How did Perl complain? print $ids should work, though you probably want a newline at the end, either explicitly with print as above or implicitly by using say or -l/$\.

If you want to interpolate a variable in a string and have something immediately after it that would looks like part of the variable but isn't, enclose the variable name in {}:

print "foo${ids}bar";