473,386 Members | 1,864 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,386 software developers and data experts.

Array initialization

I am trying to save the intermediate result in an array which I am accessing in every loop of a while loop.
Following is the part of the code:


my @array_mac=();
my $index1=0;
$array_mac_sz=@array_mac;
$index1=0;
#print "$array_mac_sz\n";
if ($array_mac_sz > 0){
while ($index1 < $array_mac_sz) {
if ($array_mac[$index1] eq "$hardware" ) {
$flag4=1;
}
$index1++;
}
}

if ($flag4 == 0){
$array_mac[$array_mac_sz+1]="$hardware";
} else {
$flag4=0;
}


I am getting a warning for the statement is Bold saying
"Use of uninitialized value in string eq at search_IP_05.plx line"


Is this a serious warning. What can I possibly do to get rid of it.
What I am trying to do in this code is: save the new hardware address only if it is not already present in the array. I am sure there is a better way to do the same. I was wondering if anyone could guide me on those lines.

Thanks a lot
Feb 12 '07 #1
6 9648
Found the mistake in my code and got rid of the warning. There is a new problem I am facing now:

@array_mac contains just one mac address.
When I type $array_size=$array_mac
I expected that
$array_size = 1
I am getting $array_size=2. Why is that happening?
Please help.
Feb 12 '07 #2
KevinADC
4,059 Expert 2GB
I assume you meant:

Expand|Select|Wrap|Line Numbers
  1. $array_size = @array_mac;
if the return value is 2 when you believe it should be 1 (one) print the array and see whats in it:

Expand|Select|Wrap|Line Numbers
  1. print "value='$_'\n" for @array_mac;
Feb 12 '07 #3
@Studentmadhura05

What you are trying to do is the following:
Expand|Select|Wrap|Line Numbers
  1. @list_1 = ( "a", "b", "c", "d" );
  2. @list_2 = ( "b", "hardware", "c" );
  3.  
  4. sub addToArr {
  5.   my($arrRef, $str) = @_;
  6.  
  7.   if ( ! grep(/$str/, @$arrRef) ) {
  8.     push(@$arrRef, $str);
  9.   }
  10. }
  11.  
  12. addToArr(\@list_1, "hardware");
  13. addToArr(\@list_2, "hardware");
  14.  
  15. printf("%s\n", join(", ", @list_1));
  16. printf("%s\n", join(", ", @list_2));
Enjoy!

Greetz, Doc
Feb 14 '07 #4
TonyV
2
I have a question.

I know that I can initialize an array:
@array = ("foo","bar");

I have a variable $var that equals ("foo","bar"); and I want to turn that into an array. @array = $var doesn't do it.

regards and thanx,

Tony
Feb 21 '07 #5
TonyV
2
OK, I just found 'split'. neat!

Tony
Feb 21 '07 #6
OK, I just found 'split'. neat!

Tony
Another approach would be:
Expand|Select|Wrap|Line Numbers
  1. $var = '( "foo", "bar" )';
  2. @arr = $var =~ m/"(.*?)"/g;
  3.  
  4. map { print $_ . "\n" } @arr;
Greetz, Doc
Feb 21 '07 #7

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

Similar topics

2
by: Fred Zwarts | last post by:
If I am right, members of a class that are const and not static must be initialized in the initialization part of a constructor. E.g. class C { private: const int I; public: C(); };
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
19
by: Henry | last post by:
I finally thought I had an understanding of multi dimensional arrays in C when I get this: #include <stdio.h> #define max_x 3 #define max_y 5 int array;
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
15
by: Charles Sullivan | last post by:
Assume I have a static array of structures the elements of which could be any conceivable mixture of C types, pointers, arrays. And this array is uninitialized at program startup. If later in...
3
by: kk_oop | last post by:
Hi. I recently wrote a simple little template that defines an array that checks attempts to use out of bounds indexes. The only problem is that it does provide the use array style value...
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
15
by: jamx | last post by:
How can you initialize an array, in the initialization list of a constructor ?? SomeClass { public: SomeClass() : *init here* { } private: int some_array; };
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.