Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    What do you mean by "it doesn't print all the the entries of the file."?

    Instead of that foreach loop, just do this:
    Code:
    print @PeerList;
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Hi,

    what is wrong in your code:

    Perl Code:
    foreach $andreas (@PeerList) 	
    { 		
       chomp $andreas; 		
       print "$andreas\n"; 		
       <>; 	# THIS LINE MUST BE REMOVED
    }


    is the "<>;" line right before the end: after having printed the first line of your array, the program waits for some data from standard input (basically waits for you to type some text finished by a new line), and will hang forever until you do so. Once you hit enter, a new line will be printed and the program will wait again until you enter something, and so on.

    But, as Fishmonger pointed out, it would be easier to print directly the array (the print command expects a list of items, so an array of lines will do very well).

    Actually, you don't even need an array in your program. You could just print the line from the file:

    Perl Code:
    $peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";          
    open(my $peer_fh, '<', $peer_add) or die "failed to open $peer_add $!";          
    while (my $line = <$peer_fh>) {
         print $line;
    }     
    close $peer_fh;


    Or, even simpler:

    Perl Code:
    $peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";          
    open(my $peer_fh, '<', $peer_add) or die "failed to open $peer_add $!";          
    print while <$peer_fh>;     
    close $peer_fh;
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    If you only want to print the contents of the file, then you don't even need the while loop.
    Code:
    print <$peer_fh>;
    If you do intend to slurp the file into an array, then I'd use the File::Slurp module.
    Code:
    use File::Slurp;
    
    my $peer_add = "C:/Users/TsentisA/Documents/Project/peer_address.txt";
    my @peers = read_file( $peer_add, chomp => 1 );
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo