469,338 Members | 8,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

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 9326
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

Post your reply

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

Similar topics

2 posts views Thread by Fred Zwarts | last post: by
13 posts views Thread by simondex | last post: by
19 posts views Thread by Henry | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
15 posts views Thread by Charles Sullivan | last post: by
3 posts views Thread by kk_oop | last post: by
5 posts views Thread by toton | last post: by
15 posts views Thread by jamx | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.