David Aldridge
asked on
Printing a numberd list from an array
What's the best way to print a numbered list from an array? Let's say I have this in an array:
DBA
HEWITT
INETSVC
I want to print it like this with an option of the operator choosing a number to make a selection:
1) DBA
2) HEWITT
3) INETSVC
Thanks!
David
DBA
HEWITT
INETSVC
I want to print it like this with an option of the operator choosing a number to make a selection:
1) DBA
2) HEWITT
3) INETSVC
Thanks!
David
To print out a numbered list, this will work:
Alternately, this would work as well:
in place of printf, you can also use print like this:
foreach my $i (0..@array-1) {
printf "%d) %s\n", $i+1, $array[$i];
}
Alternately, this would work as well:
my $i = 1;
foreach my $str (@array) {
printf "%d) %s\n", $i, $str;
$i++;
}
in place of printf, you can also use print like this:
print $i+1, ") ", $array[$i], "\n";
I don't see any issues with your code - it pretty much matches my second option. Does the first option work for you (just replace array with groups in the two places)?
ASKER
I get the same result with your first code as I do with mine.
1) 0
It's got me stumped.
1) 0
It's got me stumped.
Are you sure that @groups contains what you think it does? Try this just before the loop:
What does that show?
use Data::Dumper;
print Data::Dumper::Dump(\@groups), "\n";
What does that show?
ASKER
This is how I'm (supposedly) loading the array. I know the command is working because I see the output come to the screen. I can't figure out why it's not filling the array.
my @groups = system( 'ssh ldapserver "su - oudadm | ldapsearch -h localhost -p 1389 -D \"cn=Directory Manager\" -j /home/ouadm/pwd-file -b \"ou=people,dc=example,dc= com\" \"objectclass=organization alunit\" \"objectclass=organization alunit\" | grep -i people | grep dn: | cut -d= -f2 | grep -v people | cut -d, -f1"' );
my @groups = system( 'ssh ldapserver "su - oudadm | ldapsearch -h localhost -p 1389 -D \"cn=Directory Manager\" -j /home/ouadm/pwd-file -b \"ou=people,dc=example,dc=
ASKER
You're right. The array isn't getting loaded. It was empty and I don't know why. That command works.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Let me give that a shot.
ASKER
I got it. It was the way I was loading the array. You guys were both extremely helpful! I'm going to split it down the middle if it's okay with you.
Thanks again!
David
Thanks again!
David
Splitting is fine with me. However, the points distribution looks like all the points went to Dave.
ASKER
How do I change that? I said split it. I have problems with the new format.
Not sure. I rarely ask questions on here - usually just answer. You'll likely need to contact support if you don't see anything obvious.
ASKER
Okay, I'll see what I can do about it. Sorry about that.
ASKER
Okay, I sent this to the moderator:
I meant to split the points between the two contributors on this question. I thought I had. This new format confuses me sometimes. Could you please split these points evenly between the two?
Thank you,
David
I meant to split the points between the two contributors on this question. I thought I had. This new format confuses me sometimes. Could you please split these points evenly between the two?
Thank you,
David
ASKER
my $count = 1;
foreach my $group ( @groups ) {
print( "$count\) $group\n");
$count++;
}