awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have a text script where I have to set the same values for different "nodes". The nodes are 1,2,3,4 and 5. The nodes can change everytime I run the script so I input them manually everytime I run it. How do I get it to cycle through the different nodes? I know this is simple but it's been a while since I use Perl.
#!/usr/bin/perl $node_number = [1,2,3,4,5]; while ($node_number) { print <<EOF; node = $node_number ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 EOF $node_number++ }
The output should print:
node = 1 ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 node = 2 ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 node = 3 ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 node = 4 ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 node = 5 ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8

Replies are listed 'Best First'.
Re: Reprinting Text With Changing Fields
by moot (Chaplain) on Apr 19, 2005 at 16:42 UTC
    for my $node_number (@$nodes) { # inner processing here } # or just for (1..5) { # inner processing here using $_ as $node_number }
Re: Reprinting Text With Changing Fields
by gellyfish (Monsignor) on Apr 19, 2005 at 16:42 UTC

    I think you probably mean something like:

    my @nodenumbers = (1,2,3,4,5); foreach my $node_number (@nodenumbers) { # etc }

    /J\

Re: Reprinting Text With Changing Fields
by dmorelli (Scribe) on Apr 19, 2005 at 16:50 UTC
    Or, if you really, really need to use the array ref...

    #! /usr/bin/perl -w # Start using this, and warnings (above -w) :) use strict; my $node_number = [1,2,3,4,5]; # Your variable above is a reference to an array # Instead of while, use an iterator like foreach # Note: must dererence with @$ # Each time through the loop the current value is in $_ foreach (@$node_number) { print <<EOF; node = $_ ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 EOF }
Re: Reprinting Text With Changing Fields
by tlm (Prior) on Apr 19, 2005 at 17:01 UTC

    As others already pointed out, your original code was not handling an array ref properly (and didn't need it in the first place). But if your question is how to give different node sets to the loop, you could try:

    my @node_number = @ARGV; for my $nn ( @node_number ) { print <<EOF; node = $nn ccu = 5 ccu2 = 9 tacaf = y enable = y sync = 8 EOF }
    Then you can invoke your program with:
    % perl my_program.pl 4 13 1 22 14

    the lowliest monk

      Okay I got it to work with the @ARGS, so it works with:

      %perl my_program.pl 4 13 1 22 14

      How can I also get it to work with a text file that has the arguments listed in it?

      I'd like to get it to also work with:

      %perl my_program.pl < textfile.txt
        my @node_number = do { local $/; split ' ', <> }; # etc.

        the lowliest monk