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:
how to get each element of array to print out

 



regex2012
User

Feb 6, 2017, 8:57 AM

Post #1 of 6 (1783 views)
how to get each element of array to print out Can't Post

I have an array "arr1" that comes from a do statement and when it prints out it prints the following:
Inventory1x
Inventory1x
Inventory1x
Inventory2x
Inventory2x
Inventory2x
Inventory2x
Inventory3x
Inventory3x

I then have this statement to stop the duplicates from printing:



Code
my %seen; 
my @lines;
for (@arr1) {
unless ($seen{$_}++) {
push @lines, $_;
print "THESE ARE THE @lines";
}
}


This works, but only prints one line "Inventory3x." How do I get it to print Inventory1x and Inventory2x one time as well?


(This post was edited by regex2012 on Feb 6, 2017, 8:57 AM)


Laurent_R
Veteran / Moderator

Feb 6, 2017, 2:01 PM

Post #2 of 6 (1770 views)
Re: [regex2012] how to get each element of array to print out [In reply to] Can't Post

The logic is wrong. You don't print the @lines array in the right place, you need to to it after you've finished populating it. You would probably have noticed if you had taken the pain of correctly indenting your code.

Try this:


Code
perl -e ' my @arr1 = qw / 
Inventory1x
Inventory1x
Inventory1x
Inventory2x
Inventory2x
Inventory2x
Inventory2x
Inventory3x
Inventory3x /;
my %seen;
my @lines;
for (@arr1) {
push @lines, $_ unless $seen{$_};
$seen{$_} = 1;
}
print "THESE ARE THE LINES: @lines"; '



BillKSmith
Veteran

Feb 6, 2017, 2:31 PM

Post #3 of 6 (1770 views)
Re: [regex2012] how to get each element of array to print out [In reply to] Can't Post

You have been posting long enough in this forum to know that you should post a complete program that demonstrates your problem. I am unable to duplicate your problem using the scraps you have posted.


Code
use strict; 
use warnings;
my @arr1 = qw(
Inventory1x Inventory1x Inventory1x
Inventory2x Inventory2x Inventory2x Inventory2x
Inventory3x Inventory3x
);
my %seen;
my @lines;
for (@arr1) {
unless ( $seen{$_}++ ) {
push @lines, $_;
}
}
print "THESE ARE THE @lines\n";



OUTPUT:

THESE ARE THE Inventory1x Inventory2x Inventory3x


Of course, this is exactly what grep is intended for.



Code
use strict; 
use warnings;
my @arr1 = qw(
Inventory1x Inventory1x Inventory1x
Inventory2x Inventory2x Inventory2x Inventory2x
Inventory3x Inventory3x
);
my %seen;
my @lines = grep {!$seen{$_}++} @arr1 ;
print "THESE ARE THE @lines\n";

Good Luck,
Bill


Laurent_R
Veteran / Moderator

Feb 7, 2017, 6:28 AM

Post #4 of 6 (1762 views)
Re: [BillKSmith] how to get each element of array to print out [In reply to] Can't Post


In Reply To
I am unable to duplicate your problem using the scraps you have posted.

Yes, Bill, because your program is correct, while the OP program is wrong: it is trying to print the @lines array within the unless block instead of doing it after the end of the for loop.

To Regex2012: you should really indent your code correctly and consistently, that would help you understand the structure of your own programs.


BillKSmith
Veteran

Feb 7, 2017, 2:18 PM

Post #5 of 6 (1748 views)
Re: [Laurent_R] how to get each element of array to print out [In reply to] Can't Post

Laurent,

I assumed that the OP moved the print statement as a debugging aid. When I ran the code that was posted, I got exactly what I expected. I then moved the print 'back' to its normal position to reduce the clutter. Neither version reproduces the problem that he reported.


Quote
This works, but only prints one line "Inventory3x." How do I get it to print Inventory1x and Inventory2x one time as well?


Perhaps he mean the print statement (in the right place) to do this:


Code
printf "THESE ARE THE %s\n" x @lines, @lines; 

OUTPUT:
THESE ARE THE Inventory1x
THESE ARE THE Inventory2x
THESE ARE THE Inventory3x

Good Luck,
Bill


regex2012
User

Feb 16, 2017, 8:05 AM

Post #6 of 6 (1661 views)
Re: [regex2012] how to get each element of array to print out [In reply to] Can't Post

 
Still trying to figure out what I did, but I found that the array I was trying to remove duplicates from was not the proper one (it had a similar name) and so I used the proper one and took out unnecessary fields and that just about fixed it. I am removing some unnecessary commas.
I do appreciate all the time you guys spent helping, I am always learning, will try to be more careful.

 
 


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

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