title: Need help getting output from Telnet to array in Perl
body: <p>First I want to apologize for such a silly question but I'm quite new to scripting. </p>
  
  <p><strong>What I'm attempting to do:</strong>
  At my office, I'm attempting to automate the ability to factory reset switches. Its testing equipment for developers not production equipment fyi. I'm trying to dump a list of files from the telnet prompt of the switch into an array and then filter through them to keep our images and delete the rest.</p>
  
  <p><strong>The constants and variables of the switch:</strong> the switch prompt for this section is =>, the files both do and don't have extensions, folders are outputted as foo\</p>
  
  <p><strong>My question:</strong> The sub isn't properly issuing the rm command, it works in a test (printing the value) but not in the actual deletion script.</p>
  
  <p><strong>New Update:</strong></p>
  
  <p>I know it looks like I abandoned this question, but I didn't! I've been monkeying around with this problem for the last month and I've achieved a major bit of success. I've been able to get my data into my initial array (yes I have 2, more on that in a bit). It will process the array and delete the contents except those I want to keep.</p>
  
  <pre><code>#!/usr/bin/perl
  
  $| = 1;
  
  use Net::Telnet;
  use strict;
  use CGI;
  use warnings;
  
  my $junk;
  my $telnet;
  my @array;
  
  #establish connection
  $telnet = new Net::Telnet (Timeout=&gt; 10,
                             Errmode=&gt; 'return',
                             Port   =&gt; '2037');
  
  $telnet-&gt;open('trn-13-mrv-con-07');
  $telnet-&gt;put(chr(13));
  
  #clear out buffered output for array to grab
  $junk = $telnet-&gt;cmd(string =&gt; 'ls', prompt =&gt; '/=&gt;/');
  #array grabbing
  @array = $telnet-&gt;cmd(string =&gt; 'ls', prompt =&gt; '/=&gt;/');
  
  #determining what the values are of the array
  foreach my $item (@array) {
     #removing the carriage return
     chomp ($item);
     #if it's a folder, this will shunt the to the sub
     if ($item=~ /\//) {
         subfolders ($item);
         $telnet -&gt; print("rm $item");
     }
     #checks to see if the file is the firmware for the switch
     elsif ($item =~ /btm.swi|secondary.swi/) {
     }
     #deletes the rest
     else {
      $telnet -&gt; print ("rm $item");
      $telnet-&gt;put(chr(13));
     }
  }
  
  sub subfolders {
   my @subfolder_contents;
   my $subfolder = $_[0];
   my ($file, $sub_junk);
  
   #attempting to clear out the array
   undef @subfolder_contents;
   #again grabing any cached output so array can do it's work
   $sub_junk = $telnet -&gt;cmd(string =&gt; "ls $subfolder", prompt =&gt; '/=&gt;/');
   #grabbing files from the folder
   @subfolder_contents = $telnet -&gt;cmd(string =&gt; "ls $subfolder", prompt =&gt; '/=&gt;/');
  
   #deleting all files in the subfolder
   foreach $file (@subfolder_contents){
    chomp ($file);
      $telnet-&gt; print ("rm $subfolder$file");
      $telnet-&gt;put(chr(13));
  
   }
  };
  
  exit;
  </code></pre>
  
  <p>That all works wonderfully, AND this will work for the folders as well, mostly... It works on the folders if the folder is empty or has just one item in it. When it has 2 or more items in the folder it doesn't issue the command to delete the files in that folder correctly.</p>
  
  <p>Any thoughts would be appreciated. thanks!
  Thanks!</p>