CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Reverse array from a file

 



esavi1
New User

Sep 21, 2016, 11:48 AM

Post #1 of 7 (3245 views)
Reverse array from a file Can't Post

Hi

I have this problem reversing the lines of my array.
The data on my file is in this format
Original Reversed
1 4
2 3
3 2
4 1

This is my code atm.

$dnaseq = <STDIN>;
chomp $dnaseq;
unless(open(FILEHANDLE, $dnaseq)) {
print "cant open file";
exit;
}

@dna_array = <FILEHANDLE>;
close FILEHANDLE;

#THIS IS THE PART THAT GOES WRONG
print @dna_array, "\n\n";
@rev_array = reverse @dna_array;
print @rev_array

The output I get from this is:
Original Reversed
1 43
2 2
3 1
4
It puts the first two elements together for some reason. I could use pop with a loop etc, but my book wants me to use reverse for this. I've tried a lot of stuff but can't get it to work...

Thanks


FishMonger
Veteran / Moderator

Sep 21, 2016, 12:24 PM

Post #2 of 7 (3243 views)
Re: [esavi1] Reverse array from a file [In reply to] Can't Post

Does your file have 2 numbers per row as your example shows? Or is that first number in each row just representing the line number?


FishMonger
Veteran / Moderator

Sep 21, 2016, 12:33 PM

Post #3 of 7 (3238 views)
Re: [esavi1] Reverse array from a file [In reply to] Can't Post

I'm going to guess that the first number is representing the line number.

Each row has a \n line terminator with the possible exception of the last row. You need to remove that line terminator from each line and add it back in when you print the array. The chomp function is used to to do that removal.


Code
#!/usr/bin/perl 

use strict;
use warnings;

chomp(my @rows = <DATA>);
print $_, $/ for reverse @rows;


__DATA__
1
2
3
4



esavi1
New User

Sep 22, 2016, 12:45 AM

Post #4 of 7 (3227 views)
Re: [FishMonger] Reverse array from a file [In reply to] Can't Post


In Reply To
Does your file have 2 numbers per row as your example shows? Or is that first number in each row just representing the line number?


Hi. No, it's one element per row. I was trying to show you the order in the original file and how I want it in the reversed one but the forum put them together so it was hard to understand perhaps.

I attached a file to show how I mean.
Attachments: My problem..jpg (47.8 KB)


Laurent_R
Veteran / Moderator

Sep 22, 2016, 1:37 AM

Post #5 of 7 (3222 views)
Re: [esavi1] Reverse array from a file [In reply to] Can't Post

It is just because the last line ("GH" in your attached example) does not have a trailing new line. So the new line ("EF") gets printed on the same line as "GH". The best is to remove new lines when you read the data and to add them back when you print them out:


Code
use strict; 
use warnings;

my @input;
while (<DATA>) {
chomp;
push @input, $_;
}

my @output = reverse @input;
print "$_\n" for @output;

__DATA__
AB
CD
EF
GH


Output:

Code
$ perl gurus_esavi1.pl 
GH
EF
CD
AB


BTW, use "code" tags for your examples, they will be displayed correctly.


esavi1
New User

Sep 22, 2016, 2:42 AM

Post #6 of 7 (3219 views)
Re: [Laurent_R] Reverse array from a file [In reply to] Can't Post

Yeah, that fixed it.

Although I haven't learned loops yet and the "$_\n expression.

I think it's weird behavior to use put them on the same line because of that reason.

Thanks anyways!


Laurent_R
Veteran / Moderator

Sep 22, 2016, 7:47 AM

Post #7 of 7 (3205 views)
Re: [esavi1] Reverse array from a file [In reply to] Can't Post

Hi,

a more basic way to do the same loop, if you prefer:


Code
# ... 
my @output = reverse @input;
for my $item (@output) {
print $item, "\n";
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives