This code outputs the scalars in the row array properly:
$line = "This is my favorite test";
@row = split(/ /, $line);
print $row[0];
print $row[1];
The same code inside a foreach loop doesn't print any scalar values:
foreach $line (@lines){
@row = split(/ /, $line);
print $row[0];
print $row[1];
}
What could cause this to happen?
I am new to Perl coming from python. I need to learn Perl for my new position.
@linesis properly initialized? (Also, maybe you're declaring variables elsewhere, but these snippets would fail to run withuse strict;)split / / ...orsplit /\s+/ ..., it's almost always better to usesplit " " ...instead. perldoc.perl.org/functions/split.htmlprintbuiltin takes a list as an argument, and slices are possible as well, so you could simplify your code by writingprint @row[0..1]orprint @row[0,1]. Doingprint ...; print ...;is ok but I wouldn't recommend it.use strict; use warnings;, which should be used in every perl script and module.