Link to home
Start Free TrialLog in
Avatar of David Aldridge
David AldridgeFlag for United States of America

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
Avatar of David Aldridge
David Aldridge
Flag of United States of America image

ASKER

The array is @groups.  I'm not sure why this isn't working.

my $count = 1;
foreach  my $group ( @groups ) {
        print( "$count\) $group\n");
        $count++;
}
Avatar of wilcoxon
To print out a numbered list, this will work:
foreach my $i (0..@array-1) {
    printf "%d) %s\n", $i+1, $array[$i];
}

Open in new window


Alternately, this would work as well:
my $i = 1;
foreach my $str (@array) {
    printf "%d) %s\n", $i, $str;
    $i++;
}

Open in new window


in place of printf, you can also use print like this:
print $i+1, ") ", $array[$i], "\n";

Open in new window

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)?
I get the same result with your first code as I do with mine.

1) 0

It's got me stumped.
Are you sure that @groups contains what you think it does?  Try this just before the loop:
use Data::Dumper;
print Data::Dumper::Dump(\@groups), "\n";

Open in new window


What does that show?
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=organizationalunit\" \"objectclass=organizationalunit\" | grep -i people | grep dn: | cut -d= -f2 | grep -v people | cut -d, -f1"' );
You're right.  The array isn't getting loaded.  It was empty and I don't know why.  That command works.
ASKER CERTIFIED SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me give that a shot.
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
Splitting is fine with me.  However, the points distribution looks like all the points went to Dave.
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.
Okay, I'll see what I can do about it.  Sorry about that.
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