473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 :

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -w
  2.  
  3. open (src, 'aa') || die "cannot open this file $!";
  4. open (dest, '>bb');
  5.  
  6. while (<src>) {
  7.     next if (/^[^0-9]/);
  8.  
  9.     @tab = split(/\s+/,$_);
  10.     ($o,$s,$t,$fo) = split(/\./,$tab[0]);
  11.     $test = 128;
  12.     if ($o ne $test) {
  13.         @table = ('1.1.1.1',@tab[1..13]);
  14.     } elsif ($o eq $test) {
  15.         @table = @tab;
  16.     }
  17.     print dest "@table\n";
  18. }
  19. close(src);
  20. close(dest);
  21.  
When I ran it, I got warnings with "uninitialized 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.
Jun 22 '07 #1
4 1561
DeMan
1,806 1GB
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!
Jun 22 '07 #2
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.
Jun 22 '07 #3
KevinADC
4,059 Expert 2GB
Hi brian le,

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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. 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.
Jun 22 '07 #4
miller
1,089 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl -w
  2.  
  3. use strict;
  4. use warnings;
  5. use diagnostics;
  6.  
  7. my $src = 'aa';
  8. my $dest = 'bb';
  9.  
  10. open(SRC, $src) or die "Can't open $src: $!";
  11. open(DEST, ">$dest") or die "Can't open $dest: $!";
  12.  
  13. while (<SRC>) {
  14.     next unless /^\d/;
  15.  
  16.     my @tab = split /\s+/;
  17.  
  18.     $tab[0] = '1.1.1.1' if $tab[0] !~ /^128\./;
  19.  
  20.     print DEST "@tab\n";
  21. }
  22.  
  23. close(SRC);
  24. close(DEST);
  25.  
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
Jun 23 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris Lambacher | last post by:
Hi, I have to do some data manipulation that needs to be fast. I had a generator approach (that was faster than a list approch) which was taking approximately 5 seconds to run both encode and...
5
by: Johnny Meredith | last post by:
Is there a function in VBA to convert a string to an array of characters. I'm looking for the exact same functionality as the ToCharArray() method in C#. If this functionality does not exist, do...
7
by: Jamil Anwar Zaman | last post by:
If I dynamically allocate memory to a pointer, then is it possible to see afterwards what is the memory size the pointer is looking at. e.g int SIZE = 10; int *a = malloc(SIZE*sizeof(int));...
6
by: Neo | last post by:
Dear All, I want to know how a subroutine should return an array of values to the main program. From the main program, I call a sub-routine 'get_sql' which then fetches data from oracle db using...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
12
by: Sheldon | last post by:
Hi, I have two arrays that are of the same dimension but having 3 different values: 255, 1 or 2. I would like to set all the positions in both arrays having 255 to be equal, i.e., where one...
1
by: lukemack | last post by:
Hi, I have an array in the following format : Array ( =Array ( =Array (
8
by: Francisco | last post by:
Hello, Is there any code faster than this array position manipulation (some code omitted for brevity)?: internal struct TreeNodeTableItem { public int a; public int b; public int c; public...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
15
by: DL | last post by:
say, we have the following: // declare an array myArray = ; // short hand? same as myArray = new Array ? // populate it myArray = 0; myArray = 0; myArray = 1; myArray = 0;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.