开发者

Simple multi-dimensional array with loop in perl

I'm trying to use an array and a loop to print out the following (basically for each letter of the alphabet, print each letter of the alphabet after it and then move on to the next letter). I'm new to perl, anyone have any quick words of :

aa
ab
ac
ad
...

ba
bb
bc
bd
...

ca
cb
...

Currently I have this, but it only prints a single character alphabet...

@arr = ("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"开发者_如何学运维);
$i = @arr;

while ($i)
{
 print $arr[$i];
 $i--;
}


Using the range operator and the ranges you want to target:

use strict;
use warnings;

my @elements = ("aa" .. "zz");
for my $combo (@elements)
{
    print "$combo\n";
}

You can utilize the initial 2 letters till the ending 2 letters you want as ending and the for will take care of everything.


This really isn't multi-dimensional array work, if it were you'd be working with stuff like:

my @foo = (
    [1,2,3],
    [4,7,8,1,2,3],
    [2,3],
);

This is really a very basic how do I make a nested loop that iterates over the same array. I'll bet this is homework.

So, I'll let you figure out the nesting bits, but give some help with Perl's loop operators.

!! for/foreach

for (the each is optional) is the real heavy hitter for looping in perl. Use it like so:

for my $var ( @array ) {  
    #do stuff with $var
}

Each element in @array will be aliased to the $var variable, and the block of code will be executed. The fact that we are aliasing, rather than copying means that if alter the value of $var, @array will be changed as well. The stuff between the parenthesis may be any expression. The expression will be evaluated in list context. So if you put a file handle in the parens, the entire file will be read into memory and processed.

You can also leave off naming the loop variable, and $_ will be used instead. In general, DO NOT DO THIS.

!! C-Style for

Every once in a while you need to keep track of indexes as you loop over an array. This is when a C style for loop comes in handy.

for( my $i=0; $i<@array; $i++ ) {
   # do stuff with $array[$i]
}

!! While/Until

While and until operate with boolean loop conditions. That means that the loop will repeat as long as the appropriate boolean value if found for the condition ( TRUE for while, and FALSE for until). In addition to the obvious cases where you are looking for a particular condition, while is great for processing a file one line at a time.

while ( my $line = <$fh> ) {
     # Do stuff with $line.
}

!! map

map is an amazingly useful bit of functional programming kung-fu. It is used to turn one list into another. You pass an anonymous code reference that is used to enact the transformation.

# Multiply all elements of @old by two and store them in @new.
my @new = map { $_ * 2 } @old;

So how do you solve your particular problem? There are many ways. Which is best depends on how you want to use the results. If you want to create a new array of the letter pairs, use map. If you are interested primarily in a side effect (say printing a variable) use for. If you need to work with really big lists that come from sort of interator (like lines from a filehandle) use while.

Here's a solution. I wouldn't turn it in to your professor until you understand how it works.

print map { my $letter=$_; map "$letter$_\n", "a".."z" } "a".."z";

Look at perldoc articles, perlsyn for info on the looping constructs, perlfunc for info on map and look at perlop for info on the range operator (..).

Good luck.


Use the range operator (..) for your initialization. The range operator basically grabs a range of values such as numbers or characters.

Then use a nested loop to go through the array one time per character for a total of 26^2 iterations.

Rather than a while loop I've used a foreach loop to go through each item in the array. You could also put 'a' .. 'z' instead of declared @arr as the argument to the foreach loop. The foreach loops below set $char or $char2 to each value in @arr in turn.

my @arr = ('a' .. 'z');
for my $char (@arr) {
    for my $char2 (@arr) {
        print "$char$char2\n";
    }
}


If all you really want to do is print the 676 strings you describe, then:

#!/usr/bin/perl
use warnings;
use strict;

my $str = 'aa';

while (length $str < 3) {
    print $str++, "\n";
}

But I smell an "XY problem"...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜