Link to home
Start Free TrialLog in
Avatar of cucugirl
cucugirl

asked on

printing

I have an array that looks like this:

laptop
computer
books
window

and I have a subroutine that will print this in 2 columns but I'd like to put the array element next to each element so that i can print something like this:

1. laptop                        3. computer
2. books                        4. window

Thanks in advance for the help!

sub print{
      my @values = @_;
         s/^\s*(.*?)\s*$/$1/s for (@values);
      print $values[$_],("\t\t\t","\n")[$_ % 2] for 0..$#values;
   }

Open in new window

Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image


my @array = ('laptop', 'computer', 'books', 'window');
 
printit(@array);
 
sub printit{
      my @values = @_;
      s/^\s*(.*?)\s*$/$1/s for (@values);
      print $_+1 . ". ", $values[$_],("\t\t\t","\n")[$_ % 2] for 0..$#values;
   }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

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
Avatar of ozo
laptop
computer
books
window

and I have a subroutine that will print this in 2 columns but I'd like to put the array element next to each element so that i can print something like this:

1. laptop                        3. computer
2. books                        4. window

Did you really intend to put a 3. next to the 2nd array element, and a 2. next to the 3rd array element,
instead of vice versa as both solutions are doing?
Avatar of cucugirl
cucugirl

ASKER

oops my bad.. sorry it should be backwards, thanks for pointing that out ozo
@cucugirl: Just for the future, it might be appropriate to split points when multiple experts provide solutions and/or point out problems, as in this case. Adam's was obviously the solution, but ozo also pointed out a problem that you acknowledged. Its a minor thing, but I notice on all of your questions you never split points, or not that I have seen, even when multiple solutions are provided, and over time, experts may notice this and some may be less motivated to donate their time in helping you if you will always award 100% of the points to a single solution.

I'm not speaking for ozo, but more for your sake, as it is good etiquette and listed in the EE Help guide on how to award points.
mrjoltcola: ooh ok good call, I didn't know I could split points like that.. good to know, will do it next time ;) thanks!