J
Johnny Google
Here is an example of the type of data from a file I will have:
Apple,4322,3435,4653,6543,4652
Banana,6934,5423,6753,6531
Carrot,3454,4534,3434,1111,9120,5453
Cheese,4411,5522,6622,6641
The first position is the info (the product) I want to retreive for the
corresponding code. Assuming that the codes are unique for each product
and all code data is on one line.
So - I know the code is '9120' and I want to read the file line by line
and build an array for each line seperating on the commas.
Is there simple way to do this using a library or something already
created for reading simple data files?
Trying to convert from my perl code to C ...
In perl this would be about 10 or so lines...
$prod_code = '9120';
find_product($prod_code);
sub find_product {
$find_code = shift;
foreach $line (read_file($datafile)) {
@data = split /,/,$line; # split the line into an array
foreach $code (@data) {
# find a match from the list of elements
if ($code == $find_code) {
return $data[0]; # return the first element in the list
} # end if
} # end foreach
} # end foreach
} # end sub
I would imagine that this is something that is done often enough that
there is a library or some already built solution? But I am willing to
learn how to do this in long C hand... I just didn't want to re-invent
the wheel.
BTW, I could change the file format to suit a better solution in C if
there is one? (i.e. Apple=1234,5677,3432) using the = instead of just
the first element.
Thanks,
John
Apple,4322,3435,4653,6543,4652
Banana,6934,5423,6753,6531
Carrot,3454,4534,3434,1111,9120,5453
Cheese,4411,5522,6622,6641
The first position is the info (the product) I want to retreive for the
corresponding code. Assuming that the codes are unique for each product
and all code data is on one line.
So - I know the code is '9120' and I want to read the file line by line
and build an array for each line seperating on the commas.
Is there simple way to do this using a library or something already
created for reading simple data files?
Trying to convert from my perl code to C ...
In perl this would be about 10 or so lines...
$prod_code = '9120';
find_product($prod_code);
sub find_product {
$find_code = shift;
foreach $line (read_file($datafile)) {
@data = split /,/,$line; # split the line into an array
foreach $code (@data) {
# find a match from the list of elements
if ($code == $find_code) {
return $data[0]; # return the first element in the list
} # end if
} # end foreach
} # end foreach
} # end sub
I would imagine that this is something that is done often enough that
there is a library or some already built solution? But I am willing to
learn how to do this in long C hand... I just didn't want to re-invent
the wheel.
BTW, I could change the file format to suit a better solution in C if
there is one? (i.e. Apple=1234,5677,3432) using the = instead of just
the first element.
Thanks,
John