Solved

printing

Posted on 2009-05-04
6
206 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
Syslog text file into database or into .CSV 19 184
Port 80 requests 16 103
Question about @INC variable in perl 1 68
XPath Syntax help in conjunction with perl XML::LibXML 18 131
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

726 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