9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] bc and < >
@ 2022-01-10 11:49 Benjamin Riefenstahl
  2022-01-10 12:14 ` Eckard Brauer
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Benjamin Riefenstahl @ 2022-01-10 11:49 UTC (permalink / raw)
  To: 9front

Hi all,

I'm trying to write a script in rc that needs to compare numbers.  It
seems that rc does not do math, so I try to use bc for that.  But I
encounter this:

  term% echo '1 < 2' | bc
  stdin:1 syntax error
  2

man bc says this should work, right?

Thanks, benny



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 11:49 [9front] bc and < > Benjamin Riefenstahl
@ 2022-01-10 12:14 ` Eckard Brauer
  2022-01-10 12:29   ` Benjamin Riefenstahl
  2022-01-10 12:14 ` 有澤 健治
  2022-01-10 12:23 ` Eckard Brauer
  2 siblings, 1 reply; 13+ messages in thread
From: Eckard Brauer @ 2022-01-10 12:14 UTC (permalink / raw)
  To: 9front

> I'm trying to write a script in rc that needs to compare numbers.  It
> seems that rc does not do math, so I try to use bc for that.  But I
> encounter this:
>
>   term% echo '1 < 2' | bc
>   stdin:1 syntax error
>   2
>
> man bc says this should work, right?

maybe as a workaround:

% a=1; b=2; x=`{echo $a-$b | bc | cut -c1}; if (~ $x -) echo less
less

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 11:49 [9front] bc and < > Benjamin Riefenstahl
  2022-01-10 12:14 ` Eckard Brauer
@ 2022-01-10 12:14 ` 有澤 健治
  2022-01-10 13:25   ` Benjamin Riefenstahl
  2022-01-10 12:23 ` Eckard Brauer
  2 siblings, 1 reply; 13+ messages in thread
From: 有澤 健治 @ 2022-01-10 12:14 UTC (permalink / raw)
  To: 9front


TEST(1) TEST(1)

    n1 -eq n2  True if the integers n1 and n2 are arithmetically
               equal.  Any of the comparisons -ne, -gt, -ge,
               -lt, or -le may be used in place of -eq.


On 2022/01/10 20:49, Benjamin Riefenstahl wrote:
> Hi all,
>
> I'm trying to write a script in rc that needs to compare numbers.  It
> seems that rc does not do math, so I try to use bc for that.  But I
> encounter this:
>
>    term% echo '1 < 2' | bc
>    stdin:1 syntax error
>    2
>
> man bc says this should work, right?
>
> Thanks, benny
>
>


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 11:49 [9front] bc and < > Benjamin Riefenstahl
  2022-01-10 12:14 ` Eckard Brauer
  2022-01-10 12:14 ` 有澤 健治
@ 2022-01-10 12:23 ` Eckard Brauer
  2022-01-10 13:24   ` Benjamin Riefenstahl
  2 siblings, 1 reply; 13+ messages in thread
From: Eckard Brauer @ 2022-01-10 12:23 UTC (permalink / raw)
  To: 9front

>   term% echo '1 < 2' | bc
>   stdin:1 syntax error
>   2
>
> man bc says this should work, right?

just found that cut isn't included in P9P, but from the linux system,
so better use:

echo 'if (1<2) print 1' | bc


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 12:14 ` Eckard Brauer
@ 2022-01-10 12:29   ` Benjamin Riefenstahl
  0 siblings, 0 replies; 13+ messages in thread
From: Benjamin Riefenstahl @ 2022-01-10 12:29 UTC (permalink / raw)
  To: 9front

Hi Eckard,

Eckard Brauer writes:
> maybe as a workaround:

Thanks.   Yes, there are a number of workarounds.

benny

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 12:23 ` Eckard Brauer
@ 2022-01-10 13:24   ` Benjamin Riefenstahl
  2022-01-10 14:00     ` Eckard Brauer
  0 siblings, 1 reply; 13+ messages in thread
From: Benjamin Riefenstahl @ 2022-01-10 13:24 UTC (permalink / raw)
  To: 9front

Eckard Brauer writes:
> echo 'if (1<2) print 1' | bc

Interesting.  That actually works.  Although there does not seem to be
an "else".  So the best workaround so far still seems to be a different
language.

benny

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 12:14 ` 有澤 健治
@ 2022-01-10 13:25   ` Benjamin Riefenstahl
  0 siblings, 0 replies; 13+ messages in thread
From: Benjamin Riefenstahl @ 2022-01-10 13:25 UTC (permalink / raw)
  To: 9front

有澤 健治 writes:
> TEST(1)

Ah, the manual does not mention that "test" exists.  Somebody should
probably add that to the "SEE ALSO" of rc.

Thanks, benny

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 13:24   ` Benjamin Riefenstahl
@ 2022-01-10 14:00     ` Eckard Brauer
  2022-01-11  6:29       ` cinap_lenrek
  0 siblings, 1 reply; 13+ messages in thread
From: Eckard Brauer @ 2022-01-10 14:00 UTC (permalink / raw)
  To: 9front

> Interesting.  That actually works.  Although there does not seem to be
> an "else".  So the best workaround so far still seems to be a
> different language.

So I'd extend to:

echo 'r=0; if (1<2) r=1 print r' | bc

Works with P9P, don't have a real 9front here @work.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-10 14:00     ` Eckard Brauer
@ 2022-01-11  6:29       ` cinap_lenrek
  2022-01-12  8:00         ` Eckard Brauer
  0 siblings, 1 reply; 13+ messages in thread
From: cinap_lenrek @ 2022-01-11  6:29 UTC (permalink / raw)
  To: 9front

# this misses semicolon after r=1:
term% echo 'r=0; if (1<2) r=1 print r' | bc
stdin:1 syntax error
0

# fixed version works:
term% echo 'r=0; if (1<2) r=1; print r' | bc
1

--
cinap

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-11  6:29       ` cinap_lenrek
@ 2022-01-12  8:00         ` Eckard Brauer
  2022-01-12  9:13           ` Marc Chantreux
  2022-01-13  5:22           ` umbraticus
  0 siblings, 2 replies; 13+ messages in thread
From: Eckard Brauer @ 2022-01-12  8:00 UTC (permalink / raw)
  To: 9front

> # this misses semicolon after r=1:
> term% echo 'r=0; if (1<2) r=1 print r' | bc
> stdin:1 syntax error
> 0
>
> # fixed version works:
> term% echo 'r=0; if (1<2) r=1; print r' | bc
> 1

Correct... mea culpa.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-12  8:00         ` Eckard Brauer
@ 2022-01-12  9:13           ` Marc Chantreux
  2022-01-13  5:22           ` umbraticus
  1 sibling, 0 replies; 13+ messages in thread
From: Marc Chantreux @ 2022-01-12  9:13 UTC (permalink / raw)
  To: 9front

hello,

i really think heredocs are much more readable than echo.

> > # fixed version works:
> > term% echo 'r=0; if (1<2) r=1; print r' | bc
> > 1

<<. bc
  r=0
  if (1<2) r=1
  r
.

regards
-- 
Marc Chantreux
Direction du numérique de l'Université de Strasbourg
Pôle de Calcul et Services Avancés à la Recherche (CESAR)
http://annuaire.unistra.fr/p/20200

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-12  8:00         ` Eckard Brauer
  2022-01-12  9:13           ` Marc Chantreux
@ 2022-01-13  5:22           ` umbraticus
  2022-01-13 12:17             ` sirjofri
  1 sibling, 1 reply; 13+ messages in thread
From: umbraticus @ 2022-01-13  5:22 UTC (permalink / raw)
  To: 9front

depending on what you want you can also do Peano-like stuff...

; one = one
; two = (one $one)
; three = (one $two)
; five = ($two $three)
; four = $five($#two-)
; quinque = `{seq 5}
; if(~ $#five $#quinque) echo equal
equal

umbraticus

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [9front] bc and < >
  2022-01-13  5:22           ` umbraticus
@ 2022-01-13 12:17             ` sirjofri
  0 siblings, 0 replies; 13+ messages in thread
From: sirjofri @ 2022-01-13 12:17 UTC (permalink / raw)
  To: 9front


13.01.2022 06:22:34 umbraticus@prosimetrum.com:

> depending on what you want you can also do Peano-like stuff...
>
> ; one = one
> ; two = (one $one)
> ; three = (one $two)
> ; five = ($two $three)
> ; four = $five($#two-)
> ; quinque = `{seq 5}
> ; if(~ $#five $#quinque) echo equal
> equal

Iirc, someone built a math rc library using this technique.

Also you can just use numbers:

valuefive=`{seq 5}
if(~ $#valuefive 5) echo is five
combined=($valuefive $valuefive)
if(~ $#combined 10) echo is ten

sirjofri

PS: it's a nice toy. But don't do that. Use the right tool.

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-01-14  4:07 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-10 11:49 [9front] bc and < > Benjamin Riefenstahl
2022-01-10 12:14 ` Eckard Brauer
2022-01-10 12:29   ` Benjamin Riefenstahl
2022-01-10 12:14 ` 有澤 健治
2022-01-10 13:25   ` Benjamin Riefenstahl
2022-01-10 12:23 ` Eckard Brauer
2022-01-10 13:24   ` Benjamin Riefenstahl
2022-01-10 14:00     ` Eckard Brauer
2022-01-11  6:29       ` cinap_lenrek
2022-01-12  8:00         ` Eckard Brauer
2022-01-12  9:13           ` Marc Chantreux
2022-01-13  5:22           ` umbraticus
2022-01-13 12:17             ` sirjofri

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).