From: Piranha on
Hello NG,

I have some sets of data in a file, containing an int and a string per
line.
Obviously its a file, so on start all string(s), but I have to convert
the first entry to int, because I need some mathematical operations
with it.
The amount of lines is variable same as the length of the string(s).

Example:

123 some text
345 some other text

I would like to create an array out of it that allows me to:
- search a specific int and output the related string
- skip a specific int and output the remaining strings
- search a specific text and output the related int

Furthermore I need this array so often throughout my script that I
would rather create a global array than re-read the file all the time

My problems are:
- How do I define the array without to know its length?
- How do I define the strings within the array without to know their
length?
- How do I add/delete/modify an entry in the array later on, without
to leave blank entries?

Usually there will be only 4 or 5 entries, but the max is over 1000,
usually a string won´t have more than 15 characters, but the max is
much longer,
so I think the usual
char[max length + 1]
would be an awful waste of space 99% of times,
also I mostly need a string as std::string, so I might do better with
std::string upon reading already, but how to create an array of
std::string´s?

I´m kinda free on the data format within the file, so if it was easier
to separate int and string in the file by some character other than a
space, that would be possible
From: Ulrich Eckhardt on
Piranha wrote:
> I have some sets of data in a file, containing an int and a string per
> line.
> Obviously its a file, so on start all string(s), but I have to convert
> the first entry to int, because I need some mathematical operations
> with it.
> The amount of lines is variable same as the length of the string(s).
>
> Example:
>
> 123 some text
> 345 some other text
>
> I would like to create an array out of it

Okay, the first question to answer is what language you are writing, because
the solutions depend on that. Anyway, the general approach would be to read
line by line, convert them into the integer and string they consist of and
add them to a container. Note that your approach to use an array already
unnecessarily limits your possibilities, maybe a linked list would be the
better approach.

> that allows me to:
> - search a specific int and output the related string
> - skip a specific int and output the remaining strings
> - search a specific text and output the related int

No problem for any kind of container, they only differ in how fast or
complicated they are.

> My problems are:
> - How do I define the array without to know its length?

For C++, take a look at e.g. std::vector<>, for C take a look at
malloc()/realloc()/free().

> - How do I define the strings within the array without to know their
> length?

Strings are only variable-length containers for chars. In C, you would use
the same functions as above, in C++ you would use std::string though.

> - How do I add/delete/modify an entry in the array later on, without
> to leave blank entries?

In an array, you can't easily do that. What you can do is swap the element
to delete with the last one, so you only have blanks at the end. Otherwise,
it means copying each element to the one before in order to fill the gap.
This is much easier with a linked list.

> Usually there will be only 4 or 5 entries, but the max is over 1000,
> usually a string won�t have more than 15 characters, but the max is
> much longer,
> so I think the usual
> char[max length + 1]
> would be an awful waste of space 99% of times,
> also I mostly need a string as std::string, so I might do better with
> std::string upon reading already, but how to create an array of
> std::string�s?

Ah, so you are using C++. Well, I think you don't want an array of strings
but rather an array of a string and an integer each, for which you use a
struct. Now, if you know how to create an array of ints, you simply
replace 'int' with some other type to get an array of a different type.


Note that there are several different things here that you should tackle
separately, e.g. using structures, using container template classes, using
the std::string class, reading files line by line and parsing each line.

Uli

From: Ivan Novick on
On Dec 28, 7:49 am, Piranha <eu_pira...(a)gmx.net> wrote:
> 123 some text
> 345 some other text
>
> I would like to create an array out of it that allows me to:
> - search a specific int and output the related string
> - skip a specific int and output the remaining strings
> - search a specific text and output the related int

Read in each line of text into a std::string using std::getline.

For each line split the line into an int and a string using
std::istringstream.

Than for each line put the data into 2 std::map's. one where the key
is a string and the other where the key is the int.

Than do whatever you want to with the data.

As a side note writing "scripts" like this in perl is trivial... its
basically the exact problem perl is designed for.

Regards,
Ivan Novick
http://www.0x4849.net
From: Piranha on
On 28 Dez., 17:39, Ulrich Eckhardt <dooms...(a)knuut.de> wrote:
> Now, if you know how to create an array of ints, you simply
> replace 'int' with some other type to get an array of a different type.
>

There I got my problem with the size of the array, the only way I know
of, how to create an array of std::string´s is

std::string mystringarray[] = {
std::string(""), std::string(""), std::string("")
}

Thats easy to handle, but obviously requires to know in advance, how
many entries the array has, it allows deleting, modifying or loop
through the entries, but no add and for an array of over 1000 entries,
this way of declaration is not really practical anymore
From: Piranha on
On 28 Dez., 17:39, Ivan Novick <i...(a)0x4849.net> wrote:
>
> Read in each line of text into a std::string using std::getline.
>
> For each line split the line into an int and a string using
> std::istringstream.
>
> Than for each line put the data into 2 std::map's.  one where the key
> is a string and the other where the key is the int.
>
> Than do whatever you want to with the data.
>
> As a side note writing "scripts" like this in perl is trivial... its
> basically the exact problem perl is designed for.
>

Thanks for the answer, and nice guess :)
I´m actually trying to convert a php script I´ve written long ago and
implement it into a C++ application.
In php I´ve written this in a few minutes, no need to declare anything
before first use, no need to convert string to int, etc.
I can simply create one single array, fill it with all data, and end
up with an array where all even entries are my int´s and entry + 1 is
the related string
I can add an entry as
aray[count(array)] = newEntry
and so on

But in C/C++ I´m a beginner and I´m trying to figure out what all I
need to declare everything before first use, convert everything to the
required type and (biggest problem) if I need 2 separate arrays for
int and string, then how do I make sure I don´t lose the relations?
 |  Next  |  Last
Pages: 1 2 3
Prev: High Speed IRQ Timer/Clock in C
Next: DMA programming