Solved

printing

Posted on 2009-05-04
6
195 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl strange behaviour 5 72
batch script for automated email 12 111
Perl script to find new files and compress those new files 5 94
Perl script to delete older files 6 86
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

770 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