473,574 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array initialization

25 New Member
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.pl x 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 9650
Studentmadhura05
25 New Member
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=$ar ray_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 Recognized Expert Specialist
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
docsnyder
88 New Member
@Studentmadhura 05

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 New Member
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 New Member
OK, I just found 'split'. neat!

Tony
Feb 21 '07 #6
docsnyder
88 New Member
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
5562
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
27037
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
4540
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
3669
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 static; if the input file contained more than entries, tough. This time I want to do it right - use a dynamic array that increases in size with...
15
4855
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 the program I wish to return this array to its startup state, can this be accomplished by writing binary zeroes to the entire memory block with...
3
1971
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 initialization when the type is instantiated. Any suggestions for a mod that would allow array initialization syntax? ***********Here's the type: ...
5
24306
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 default ctor). Is it possible to do so in the class parameter initialization using specific ctor?
15
3357
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
2166
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
9784
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 = { { 3.14 }, { 42.6 } }; f((double *)array, sizeof array / sizeof **array); return 0;
0
7808
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7732
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8063
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8243
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7822
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5302
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3754
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.