Solved

printing

Posted on 2009-05-04
6
212 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:cucugirl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24299236

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

0
 
LVL 39

Accepted Solution

by:
Adam314 earned 250 total points
ID: 24299406
Both your original code, and the update in post 24299236 will only work as long as all of the array values are approx the same length.  If you had:
    my @array = ('my favorite laptop', 'computer', 'books', 'window');

Here is some updated code that will handle that.  The "%-20s" needs to be the length of the longest array item.  If needed, you could have this calculated automatically.
sub printit{
	my @values = @_;
	s/^\s*(.*?)\s*$/$1/s for (@values);
	printf("%2d. %-20s %s", $_+1, $values[$_], ("", "\n")[$_ % 2]) for (0..$#values)
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24299774
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cucugirl
ID: 24304746
oops my bad.. sorry it should be backwards, thanks for pointing that out ozo
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24307827
@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.
0
 

Author Comment

by:cucugirl
ID: 24313649
mrjoltcola: ooh ok good call, I didn't know I could split points like that.. good to know, will do it next time ;) thanks!
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question