Solved

printing

Posted on 2009-05-04
6
202 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending email via Perl on Windows 3 182
add a syntax to a csv file 8 99
Regular Expression for URL 10 100
perl getopt long help 34 70
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

791 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