Array Manipulation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brian le
    New Member
    • Jun 2007
    • 1

    Array Manipulation

    Hi all,

    I am new to Perl but really want to learn to do some programming with networking stuffs. I have difficuty writing a Perl script that I wish somebody can help me with. The script I wrote basically read a data file into an array, change one variable with an "if-then" statement, put back in the array and print it out. It is as follows :

    [CODE=perl]
    #! /usr/bin/perl -w

    open (src, 'aa') || die "cannot open this file $!";
    open (dest, '>bb');

    while (<src>) {
    next if (/^[^0-9]/);

    @tab = split(/\s+/,$_);
    ($o,$s,$t,$fo) = split(/\./,$tab[0]);
    $test = 128;
    if ($o ne $test) {
    @table = ('1.1.1.1',@tab[1..13]);
    } elsif ($o eq $test) {
    @table = @tab;
    }
    print dest "@table\n";
    }
    close(src);
    close(dest);
    [/CODE]

    When I ran it, I got warnings with "uninitiali zed value in an array" and the script only print out "1.1.1.1" on a line where the condition 1 is met and ignore "@tab[1..13]" and lines with initial read in @tab where condition 2 is met. I tried many different ways but still couldn't get what I want which is the same array as I first read in but the first column of the array will change depending upon condition 1.

    I really appreciate anyone out there can help me with this little crazy trick somewhere in the script that I don't know of. Thanks again.
    Last edited by miller; Jun 22 '07, 11:17 PM. Reason: Code Tag and ReFormatting
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    Hi Brianle,

    Welcome to TSDN. I'm sure you will find a wealth of knowledge ion the various forums (available through the drop-down menus on the blue bar near the top of the screen). As this is a technical question, I will move it to the perl forum for now, and the experts there will hopefully be able to point you in the right direction.....

    Hope they can help!

    Comment

    • rellaboyina
      New Member
      • Jan 2007
      • 55

      #3
      Could you be more specific about the requirement i.e. what you want actually and post the data of the input file "aa" which you are opening in the first line of the code.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Hi brian le,

        There are two thing to know when starting to learn perl:

        Code:
        #!/usr/bin/perl
        use strict;
        use warnings;
        use diagnostics;
        OK, so it's really three. ;)

        The three pragmas above will help you learn perl faster and write better code. Another thing, use all upper case characters for filehandle names, like SRC instead of src.

        To help you further, as rellaboyina asked, post some of your data.

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          The source of your warning is this statement: "@tab[1..13]". You are assuming that the @tab array has at least 14 elements, when there is nothing enforcing or garanteeing that this is the case. However, I believe that ultimately your problem lies with this statement "next if (/^[^0-9]/);". Instead of checking for what you aren't looking for, look for what you are. This is the first step to proper use of regular expressions.

          Applying this change and the ones that Kevin suggested gives you this code.

          [CODE=perl]
          #! /usr/bin/perl -w

          use strict;
          use warnings;
          use diagnostics;

          my $src = 'aa';
          my $dest = 'bb';

          open(SRC, $src) or die "Can't open $src: $!";
          open(DEST, ">$dest") or die "Can't open $dest: $!";

          while (<SRC>) {
          next unless /^\d/;

          my @tab = split /\s+/;

          $tab[0] = '1.1.1.1' if $tab[0] !~ /^128\./;

          print DEST "@tab\n";
          }

          close(SRC);
          close(DEST);
          [/CODE]

          Note, how I removed the need for specifying the max index entirely. Now even if any particular line doesn't contain 14 elmenents, it will not give you that warning.

          - Miller

          Comment

          Working...