Skip to main content
Commonmark migration
Source Link

#Powershell, 47 Bytes#

Powershell, 47 Bytes

assumes script is named a.ps1

0
[int]$n,$t=(gc a.ps1)[0..1];,(++$n),$t>a.ps1

The script will overwrite itself replacing the 0 on the first line with 1,2,3 and so on.

could also save another 8 bytes by replaceing both instances of a.ps1 with 1 and saving the script as a file named 1 although this is a bit far out for me.

Replace second line with this if the file is not saved as 'a.ps1'.

[int]$n,$t=(gc($s=$MyInvocation.MyCommand.Name))[0..1];,(++$n),$t>$s

0 on first line to initialize the count

Linebreak provides the easiest way to split the file in two

[int]$n,$t=(gc a.ps1)[0..1]

this takes the file 'a.ps1' and reads it in as an array of lines, we then iterate through it with [0..1] and set that to the variables $n which is cast as [int] and $t respectively, so that the 0 on the first line becomes $n and the 'code' on the second line becomes $t

,(++$n),$t>a.ps1

This used the ,1,2 array notation, to create an array of two elements, one being the number stored in $n pre-incremented and outputted to stdout by the use of implicit brackets, the second being the second line of text from the file, and then also output it to the file named 'a.ps1'

as both input and output are arrays of strings there is minimal formatting required, and almost everything is assumed by the interpreter.

#Powershell, 47 Bytes#

assumes script is named a.ps1

0
[int]$n,$t=(gc a.ps1)[0..1];,(++$n),$t>a.ps1

The script will overwrite itself replacing the 0 on the first line with 1,2,3 and so on.

could also save another 8 bytes by replaceing both instances of a.ps1 with 1 and saving the script as a file named 1 although this is a bit far out for me.

Replace second line with this if the file is not saved as 'a.ps1'.

[int]$n,$t=(gc($s=$MyInvocation.MyCommand.Name))[0..1];,(++$n),$t>$s

0 on first line to initialize the count

Linebreak provides the easiest way to split the file in two

[int]$n,$t=(gc a.ps1)[0..1]

this takes the file 'a.ps1' and reads it in as an array of lines, we then iterate through it with [0..1] and set that to the variables $n which is cast as [int] and $t respectively, so that the 0 on the first line becomes $n and the 'code' on the second line becomes $t

,(++$n),$t>a.ps1

This used the ,1,2 array notation, to create an array of two elements, one being the number stored in $n pre-incremented and outputted to stdout by the use of implicit brackets, the second being the second line of text from the file, and then also output it to the file named 'a.ps1'

as both input and output are arrays of strings there is minimal formatting required, and almost everything is assumed by the interpreter.

Powershell, 47 Bytes

assumes script is named a.ps1

0
[int]$n,$t=(gc a.ps1)[0..1];,(++$n),$t>a.ps1

The script will overwrite itself replacing the 0 on the first line with 1,2,3 and so on.

could also save another 8 bytes by replaceing both instances of a.ps1 with 1 and saving the script as a file named 1 although this is a bit far out for me.

Replace second line with this if the file is not saved as 'a.ps1'.

[int]$n,$t=(gc($s=$MyInvocation.MyCommand.Name))[0..1];,(++$n),$t>$s

0 on first line to initialize the count

Linebreak provides the easiest way to split the file in two

[int]$n,$t=(gc a.ps1)[0..1]

this takes the file 'a.ps1' and reads it in as an array of lines, we then iterate through it with [0..1] and set that to the variables $n which is cast as [int] and $t respectively, so that the 0 on the first line becomes $n and the 'code' on the second line becomes $t

,(++$n),$t>a.ps1

This used the ,1,2 array notation, to create an array of two elements, one being the number stored in $n pre-incremented and outputted to stdout by the use of implicit brackets, the second being the second line of text from the file, and then also output it to the file named 'a.ps1'

as both input and output are arrays of strings there is minimal formatting required, and almost everything is assumed by the interpreter.

Source Link
colsw
  • 3.4k
  • 3
  • 14
  • 24

#Powershell, 47 Bytes#

assumes script is named a.ps1

0
[int]$n,$t=(gc a.ps1)[0..1];,(++$n),$t>a.ps1

The script will overwrite itself replacing the 0 on the first line with 1,2,3 and so on.

could also save another 8 bytes by replaceing both instances of a.ps1 with 1 and saving the script as a file named 1 although this is a bit far out for me.

Replace second line with this if the file is not saved as 'a.ps1'.

[int]$n,$t=(gc($s=$MyInvocation.MyCommand.Name))[0..1];,(++$n),$t>$s

0 on first line to initialize the count

Linebreak provides the easiest way to split the file in two

[int]$n,$t=(gc a.ps1)[0..1]

this takes the file 'a.ps1' and reads it in as an array of lines, we then iterate through it with [0..1] and set that to the variables $n which is cast as [int] and $t respectively, so that the 0 on the first line becomes $n and the 'code' on the second line becomes $t

,(++$n),$t>a.ps1

This used the ,1,2 array notation, to create an array of two elements, one being the number stored in $n pre-incremented and outputted to stdout by the use of implicit brackets, the second being the second line of text from the file, and then also output it to the file named 'a.ps1'

as both input and output are arrays of strings there is minimal formatting required, and almost everything is assumed by the interpreter.