Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slice arrays in Perl

I know there are easier ways to do this, but I must demonstrate removing the first seven lines of a system call (top -bn1) and write the results to a file using array slices. I currently am having trouble with the syntax of removing the seven lines, and I'm not sure how to write it to a file.

my @top_command = `top -bn1`;

@top_command = @top_command(7..@top_command); 

print @top_command, $file_name;

1 Answers

  1. Array indexes are made with square brackets ([]), not parentheses.
  2. You can get the highest index of an array called @array with $#array.
  3. I can't even tell what you're trying to do in your third line, but you probably want to look up the splice operator.

That should be enough to complete your assignment.

like image 168
friedo Avatar answered Oct 27 '25 14:10

friedo