Friday, July 31, 2009

Question for C programming. Create a double array stuff with 5 elements.?

please go to www.programmersheaven.com


you will find there

Question for C programming. Create a double array stuff with 5 elements.?
While grep is handy it it not efficient as you always search the whole array even if you find a match in the first element. Assuming your '100X' are unique product ids this is probably the most efficient way (although by no means the only way) to do it (the last means we stop searching at the first match. If we find a match string will be defined. If there is not match it will be undef. If you have comma separated data split will work fine. Substring works on the assumption of fixed width records so if someone enters say '1010 ' you would end up getting ' ,string' back which is not what you want. The correct syntax to get all the stuff after 5 chars (4 digits and the comma) is $string = substr $line, 5;





Your grep is broken because when you say $scalar = grep... you get the number of matches not the actual array elements(s). If you call grep in array context @ary = grep... then you get an array of all the elements that matched - if there is only one it will be $ary[0]. Many functions in perl exhibit this schizophrenic behaviour - it is called scalar/array context.





my @ary = %26lt;DATA%26gt;;


my $find_id = 1006;


my ($code, $string );


for my $line( @ary ) {


next unless $line =~ m/^$find_id/;


chomp $line;


( $code, $string ) = split ',', $line;


last;


}





if ( $string ) {


print "Found '$string'\n";


}


else {


print "No match!\n";


}
Reply:double *stuff = (double *)calloc(5,sizeof(double));


No comments:

Post a Comment