Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Follow up question for rhash on Array Help 1

Status
Not open for further replies.

ejaggers

Programmer
Feb 26, 2005
148
US
Thanks for the tip on finding the array element number. T was thinking there was a special variable that keep that element number. But no my question is on the if statment you wrote:
foreach my $i (0..$#array) {
if (($array[$i] || "") =~ m/HELP/) {
$index_number = $i;
last;
}
}
I know what this is: if ($array[$i] =~ m/HELP/)

But what is this: if (($array[$i] || "") =~ m/HELP/)
 
If you're using the warnings pragma (you have use warnings; and use strict; somewhere near the top of your script, right?) you'll get a warning similar to "Use of uninitialized value in pattern match" if you run a regular expression against an uninitialized element in the array.

In the example, the only array element that has a value is index 50, all the other elements have a value of 'undef'.

On to your actual question: ($array || "") - if the value of $array is something that evaluates true use it, or else use an empty string ("").

That could be dangerous if the array elements could have a value of 0 since that would evaluate false. In that case, you'd probably want to use the defined function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top