POWERSHELL-F format operator

Source: Internet
Author: User

Today with the Netizen, the Netizen gave two lines of code, can output the following effect

1..9 | %{[email protected] ()} {$a +=$ ("{0}{1}{0}"-F ("" * (9-$_)), ("$_" * (2*$_-1))} {$a [0..8+7..0]} 1..9 | %{$a = ""}{for ($i =1; $i-le $_; $i + +) {$a + = "{0,-7}"-F "$i X $_=$ ($i *$_)"}; $a + = "' R ' n"} {$a}


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/80/32/wKiom1c6uiCBv4yJAABm3LRPrpA884.png "title=" 00.PNG "alt=" Wkiom1c6uicbv4yjaabm3lrprpa884.png "/>


At first glance, honestly admit that I really did not understand how these two lines work, studied carefully for a while, finally understand the logical relationship.

These two lines of code mainly take advantage of PowerShell inside the use of the format operator-F.

The resources are as follows

Http://ss64.com/ps/syntax-f-operator.html


There are 2 main expressions of this operator:


Expression Form 1

"String with placeholders"-F "Array of values to place into the placeholders"

For example

Get-childitem C:\Temp | Foreach-object {' Filename: {0} Created: {1} '-F $_.fullname,$_.creationtime}

You can see that the name of filename is assigned to the FullName attribute, and created is assigned to the CreationTime property.

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/2F/wKioL1c6vHGhH7nTAACTVJokZFQ600.png "title=" 11.PNG "alt=" Wkiol1c6vhghh7ntaactvjokzfq600.png "/>


Expression 2, I for position, a for space position, negative for the right plus space, positive for the left plus space, FS is some formatting options, such as hex, percentage, etc.

"{I,a:fs} {I,a:fs} {I,A:FS}:"-F "STRING0", "string1", "string2" ...


The two examples mentioned above mainly take advantage of the second form of expression. Now take a closer look.


1..9 | %{[email protected] ()} {$a +=$ ("{0}{1}{0}"-F ("" * (9-$_)), ("$_" * (2*$_-1))} {$a [0..8+7..0]}


The basic principles of the pyramid are known to count the spaces and numbers of each line, and then write loops.

First 1 to 9 passes through the pipe, and then for each number, he creates an array, {0}{1}{0} corresponds to the two strings provided, exactly the "space" + "number" + "space" format. {0} corresponds to ("" * (9-$_)), {1} corresponds to ("$_" * (2*$_-1))

Add up, $a is just the top half of the pyramid. Finally through $a[0..8] output upper half, $a [7..0] output next half ~



1..9 | %{$a = ""}{for ($i =1; $i-le $_; $i + +) {$a + = "{0,-7}"-F "$i X $_=$ ($i *$_)"}; $a + = "' R ' n"} {$a}


The idea of this example is similar. The pipe is passed in each number, listed separately, {0,-7} represents the 7 spaces to the right of the current output string; Note that in this example, the $ A created by him is an empty string, so every element added to it will not be wrapped like an array. The author adds a return line break at the end of each loop.




This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1774329

POWERSHELL-F format operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.