Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

lowest free number in a series

8 views
Skip to first unread message

jon

unread,
Feb 15, 2003, 8:28:15 PM2/15/03
to
i have been trying to get this one for some time, now i will ask the
pros.

i have a list of numbers, ever changing that i need to to extract the
first free number in the series

example

1,2,3,67,100,10000 first free is 4
(not counting zero)

i hope to use /bin/sh on freebsd.
any and all help and is very much appreciated

thanks
jon

Ian Springer

unread,
Feb 15, 2003, 11:07:33 PM2/15/03
to

how/where is the list of #'s stored?


Stephane CHAZELAS

unread,
Feb 16, 2003, 2:50:21 AM2/16/03
to
On 15 Feb 2003 17:28:15 -0800, jon <juos...@yahoo.com> wrote:
[...]

> i have a list of numbers, ever changing that i need to to extract the
> first free number in the series
>
> example
>
> 1,2,3,67,100,10000 first free is 4
> (not counting zero)
[...]

list="1,2,3,67,100,10000"

awk 'BEGIN{RS=","}{a[$0]=1}END{
for (i=1; a[i]; i++); print i}' << EOF
$list
EOF

--
Stéphane

Heiner Steven

unread,
Feb 16, 2003, 7:49:11 AM2/16/03
to
jon wrote:

[...]


> i have a list of numbers, ever changing that i need to to extract the
> first free number in the series
>
> example
>
> 1,2,3,67,100,10000 first free is 4
> (not counting zero)

[...]

awk '
{
if ( $1 > last + 1 ) { print last + 1; exit (0); }
last = $1
}
' "$@"

Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner...@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/

Stephane CHAZELAS

unread,
Feb 16, 2003, 11:41:55 AM2/16/03
to
On Sun, 16 Feb 2003 13:49:11 +0100, Heiner Steven
<heiner...@nexgo.de> wrote:
[...]
> awk '
> {
> if ( $1 > last + 1 ) { print last + 1; exit (0); }
> last = $1
> }
> ' "$@"

Better than mine. However it fails to return 4 for "1,2,3"

awk 'BEGIN{RS=","} $1 != NR {exit} {n++} END {print n+1}' << EOF
1,2,3
EOF

--
Stéphane

laura fairhead

unread,
Feb 18, 2003, 1:32:13 PM2/18/03
to

One way you can do it is to transpose all the COMMA characters
into NEWLINEs so that each number is on a seperate line, then
feed it into 'awk',

NEXTV=$( tr , '\12' <file.in |awk 'NR!=$1{exit(x++)}END{print NR+1-x}' )

where, file.in = the name of the input data file

The first value free is the line number of the first line
which is not equal to the value on that line itself, or
the next line otherwise (if there are no values in the list
NR=0 in the END block )

If the list is in a string then you can just use the same
construct with a little modifcation

NEXTV=$( tr , '\12' <<: |awk 'NR!=$1{exit(x++)}END{print NR+1-x}'
$LISTV
:
)

This is all assuming that your list is in numerical order of course,
if it's not then simply add a 'sort -n' in the pipeline after 'tr'

bestwishes
laura

--
alt.fan.madonna |news, interviews, discussion, writings
|chat, exchange merchandise, meet fans....
|Get into the groove baby you've got to... check us out!

Stephane CHAZELAS

unread,
Feb 18, 2003, 6:20:48 PM2/18/03
to
On Tue, 18 Feb 2003 18:32:13 GMT, laura fairhead
<LoveMrs...@madonnaweb.com> wrote:
[...]

> One way you can do it is to transpose all the COMMA characters
> into NEWLINEs so that each number is on a seperate line, then
> feed it into 'awk',
>
> NEXTV=$( tr , '\12' <file.in |awk 'NR!=$1{exit(x++)}END{print NR+1-x}' )
[...]

What's the problem with RS?

Tim Cargile

unread,
Feb 18, 2003, 8:56:28 PM2/18/03
to
juos...@yahoo.com (jon) wrote in message news:<511714cd.0302...@posting.google.com>...

> i have been trying to get this one for some time, now i will ask the
> pros.

Yeah, right! :-)

>
> i have a list of numbers, ever changing that i need to to extract the
> first free number in the series
>
> example
>
> 1,2,3,67,100,10000 first free is 4
> (not counting zero)

I think Ian Springer already asked a crucial question:

Where is this list stored?

Besides that, I have a few more to ask:

1) How is the 'ever changing' list going to be accessed
so that the next number 'extracted' (you mean 'determined'
I believe) from the list occurs when the list is not
changing. This is called 'exclusive' access.

2) Can we assume the list is stored in a file?

3) Are the 'comma' delimiters there for illustration or
will they really exist in (whereever the list is stored).

And so on ...

>
> i hope to use /bin/sh on freebsd.

Good for you! But the design is more important than the
implementation language/platform.

> any and all help and is very much appreciated
>
> thanks
> jon

HTH

Tim - PITA

Brian Hiles

unread,
Feb 18, 2003, 11:17:30 PM2/18/03
to
jon <juos...@yahoo.com> wrote:
> i have a list of numbers, ever changing that i need to to extract the
> first free number in the series

I did the same for a script function designed to set alarms by number --
ten in total. The extract of the code, which uses no external SW tools and
therefore is very efficient, is:


let alrmno=0 p_alrmno=0
[[ -f ~/.alarms ]] || : >|~/.alarms
set -s -A _inst -- $(
while read -r x1 x2
do print -r -- $x1
done <~/.alarms
) 9
# find lowest alarm number
for alrmno in ${_inst[*]}
do if ((alrmno!=p_alrmno+1))
then alrmno=$p_alrmno+1 break
else let p_alrmno=alrmno
fi
done
((alrmno>9)) &&
{ print -ru2 $0: too many alarms
alrmno=0 return 1
}
((!alrmno)) && alrmno=1
print -ru2 [ $0: setting alarm number $alrmno ]

=Brian

laura fairhead

unread,
Feb 19, 2003, 4:34:36 PM2/19/03
to
On 18 Feb 2003 23:20:48 GMT, Stephane CHAZELAS <stephane...@yahoo.fr>
wrote:

If the list wasn't sorted then you can make the simple adjustment of
inserting a 'sort -n' and it wasn't specified whether the list was
sorted or not.

Also I was considering the possibility of a file of such lines;

1,2,3,4,5
6,8,10,11,12


byefornow

jon

unread,
Feb 20, 2003, 4:35:34 PM2/20/03
to
Heiner Steven <heiner...@nexgo.de> wrote in message
>
> awk '
> {
> if ( $1 > last + 1 ) { print last + 1; exit (0); }
> last = $1
> }
> ' "$@"
>
> Heiner

this was the best solution in my case, thanks all who helped

jon

0 new messages