Solved

printing

Posted on 2009-05-04
6
178 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now