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

Perl Monks: Thanks You are the best. Obviously this is my intial forey into this. i had in my head what i wanted to do, but don't know how to accomplish.

This is working now on the data i want to manipulate. Thanks for putting me the right track.

Original Post

does this code look right. it exectutes but does not produce desired results. it changes all lines, although it is being told to look for object-group at $array[0] and the then make change if condition is met..Thanks

open (FILE, $ARGV[0]) or die("Error:'\n"); open(OUTPUT,">output.txt"); my $line; while ($line = <FILE>) { chomp($line); @array = split(" ", $line); for($array[0]=object-group) { print OUTPUT "set shared address-group $array[2]\n"; } } close (FILE); exit;

Replies are listed 'Best First'.
Re: sanity check
by Corion (Patriarch) on Sep 20, 2011 at 20:21 UTC

    This is not sane. If you were using strict and warnings, Perl would tell you about some of the things that are wrong with your code. Other parts of your code look like you just made up something and expect Perl to make sense of it. That's not how programming works.

Re: sanity check
by ikegami (Patriarch) on Sep 20, 2011 at 20:20 UTC

    does this code look right

    No. In fact, I can't even tell what you're trying to do.

    Start by adding use strict; use warnings;

Re: sanity check
by cdarke (Prior) on Sep 21, 2011 at 05:56 UTC
    Ther are all kinds of issues with your code, and others have pointed some of them out, I'll try to explain.

    A major problem you have is with the for statement, which you seem to be using instead of if. In Perl, for is a type of loop for walking through items in a list.
    Secondly you are using a single = sign. In many languages, including Perl, a single = is an assignment. In Perl, to do a numeric comparison you use ==, and to do a textual comparison use eq. If we assume that object-group is supposed to be text, then that should be enclosed with quotes. So we are left with:
    if ($array[0] eq 'object-group') { print OUTPUT "set shared address-group $array[2]\n"; }
    As for the rest, people used to code Perl in that way back in the 20th century, but things have moved on since then. Try running your code through Perl::Critic or http://perlcritic.com/.
Re: sanity check
by Jim (Curate) on Sep 20, 2011 at 21:09 UTC

    That's insane! ;-)

    Compare your script with this refactored version.

    #!perl use strict; use warnings; use autodie qw( open close ); open my $input_fh, '<:encoding(UTF-8)', shift @ARGV; open my $output_fh, '>:encoding(UTF-8)', 'Output.txt'; while (my $line = <$input_fh>) { chomp $line; my @tokens = split ' ', $line; if ($tokens[0] eq 'object-group') { print {$output_fh} "set shared address-group $tokens[2]\n"; } } close $input_fh; close $output_fh; exit 0;

    UPDATE: Changed « my @tokens = split m/ /, $line; » to « my @tokens = split ' ', $line; ».

      split m/ /, $line doesn't even nearly do the same thing as split(" ", $line).

      That would be better as:

      while ( <$input_fh> ) { my @tokens = split; ...
        C:\>perl -e "use 5.012; my $foo='abc def';my @foo=split(/ /,$foo); for + (@foo) {say $_;} abc def C:\>perl -e "use 5.012; my $foo='abc def';my @foo=split m/ /,$foo; for + (@foo) {say $_;} abc def

        What did I miss?