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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding out the number of splits

Status
Not open for further replies.

veteq

Technical User
Dec 7, 2004
23
CA
I am using the split function to load an array,
some of the records will split in 2 others only once

if it splits in 2 I need to only keep the 2 record but if it splits once, I keep the 1st record. The problem that I ran into is when the second element [1] in the array is empty, I get an error, is there a way of me knowing the number of times the string was split? this way I only search for element 2 when it splits more than once....

code:
@splitarray = split (/ /,$tmpholder);

Error:
Use of uninitialized value in string ne at Adm4_System_Processes_Restart.pl line 99, <PROCFILE> line 100.


Any help would be greatly appreciated.....I am stuck
Veteq


 
You can get the number of elements put into @splitarray by doing the following:
Code:
$number = ( @splitarray = split ( / /, $tmpholder ) );
 
Alternatively, as you always want the last element in the array, just use
Code:
my $record = $splitarray[-1];

# or even

my $record = pop(@splitarray);
 
A couple more ways:

Code:
my @array = qw(one two);

# Displays the highest index number used.
print $#array, "\n";

# OR

if (defined($array[1])){
    print "Element 2 is present\n";
} else {
    print "Element 2 is not present\n";
}
 
Thank you very much for all your help, I can always get the answers that I am looking for here

thank you.
 
You do not even need this @splitarray variable:
Code:
$lastField = (split / /, $tmpholder)[-1];

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top