header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

loading text file into hash table
Last Post 06 Feb 2008 07:46 PM by RichS. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
kctalbotUser is Offline
New Member
New Member
Posts:14
Avatar

--
04 Feb 2008 04:49 PM  

How would you go about loading a state & capitol file into a hashtable?  Example of a hashtable that I can grasp shows state (key) and then state capitol (value).  No one is going to key that in more than once, so how would you go about creating the 51 entries and then loading them into script?  And then, how would I save a hash table to a text file to reuse later?

 

 

RichSUser is Offline
New Member
New Member
Posts:41
Avatar

--
04 Feb 2008 05:15 PM  

The first part of your problem is load a file into a hash table

Create a file called states.csv with this data

State, capital
Alabama,Montgomery
Alaska,Juneau
Arizona,Phoenix
Arkansas,Little Rock
California,Sacramento
Colorado,Denver
Connecticut,Hartford
Delaware,Dover
Florida,Tallahassee
Georgia,Atlanta
Hawaii,Honolulu
Idaho,Boise
Illinois,Springfield
Indiana,Indianapolis
Iowa,Des Moines
Kansas,Topeka
Kentucky,Frankfort
Louisiana,Baton Rouge
Maine,Augusta
Maryland,Annapolis
Massachusetts,Boston
Michigan,Lansing
Minnesota,Saint Paul
Mississippi,Jackson
Missouri,Jefferson City
Montana,Helena
Nebraska,Lincoln
Nevada,Carson City
New Hampshire,Concord
New Jersey,Trenton
New Mexico,Santa Fe
New York,Albany
North Carolina,Raleigh
North Dakota,Bismarck
Ohio,Columbus
Oklahoma,Oklahoma City
Oregon,Salem
Pennsylvania,Harrisburg
Rhode Island,Providence
South Carolina,Columbia
South Dakota,Pierre
Tennessee,Nashville
Texas,Austin
Utah,Salt Lake City
Vermont,Montpelier
Virginia,Richmond
Washington,Olympia
West Virginia,Charleston
Wisconsin,Madison
Wyoming,Cheyenne

I assume it is coorect

then use this to load it

$stcap

= @{}

Import-Csv

states.csv | ForEach-Object { $stcap[$_.State] = $_.Capital}

$stcap

into the hash table

 

Richard Siddaway
Microsoft MVP - PowerShell
UK PowerShell User group Leader
www.get-psuguk.org.uk
halr9000User is Offline
PowerShell MVP, Site Admin
Basic Member
Basic Member
Posts:335
Avatar

--
04 Feb 2008 05:17 PM  
Funny you should need that exact example, because that (state capitols) is what the Scripting Guys used in their hashtable article: http://www.microsoft.com/technet/scriptcenter/resources/pstips/sept07/pstip0914.mspx

I'd find a table online with the entries, paste into Excel, save as CSV, then use the Import-CSV cmdlet. That won't make a hashtable though, that will make a custom object with a property for each column in the sheet. I'm curious to know from someone with more CS knowledge as to why you might want to use a hashtable over the import-csv method. Regardless--it would work.

As far as saving to text, that's easy, pipe your hashtable to set-content or out-file. Read the help files, there are some subtle differences. Don't forget the -encoding parameter otherwise it'll come out Unicode which some programs have issues dealing with.
Community Director, PowerShellCommunity.org
Co-host, PowerScripting Podcast
Author, TechProsaic
kctalbotUser is Offline
New Member
New Member
Posts:14
Avatar

--
04 Feb 2008 05:32 PM  

Good reason, I read the article just couldn't see how to apply it to load and unload.  For all the good books, articles, and webcasts it always seems they demonstrate one variable and real life has multiple variables.

If I had used Spokane as capital of Washington I would have removed all doubt as to source of question.

RichSUser is Offline
New Member
New Member
Posts:41
Avatar

--
04 Feb 2008 05:58 PM  

In terms of long term storage for the hash table for reuse try this

$stcap | Export-Clixml -Path state.xml

where $stcap is the hash table we created earlier

To reuse

$stc1 = Import-Clixml -Path state.xml

Richard Siddaway
Microsoft MVP - PowerShell
UK PowerShell User group Leader
www.get-psuguk.org.uk
kctalbotUser is Offline
New Member
New Member
Posts:14
Avatar

--
06 Feb 2008 03:33 PM  

Reading and not remembering clearly or understanding completely.  Statement was made that saving as XML will preserve more "object" information.  I am not sure what I am gaining with xml versus csv or even text.  I do see in my real life problem that once I parse a file into meaningful parts, redoing the same input file would be a poor use of time.  I am not conversant enough with XML to know why I should like that better.  My background goes back to IBM dos 1.0 and CPM, I've spent time writing Korn shell scripts, DBA, programmer, etc.  I am dedicated to finding the easiest way to do something no matter how much effort it takes to get there and have learned that one time only programs/patches are still running 15 years later.

kct

RichSUser is Offline
New Member
New Member
Posts:41
Avatar

--
06 Feb 2008 07:46 PM  

For soemthing like the example of the state capitals I would go with the XML as you are guaranteed to be loading the same data each time.  If the data will be ammended then consider csv\txt file

Richard Siddaway
Microsoft MVP - PowerShell
UK PowerShell User group Leader
www.get-psuguk.org.uk
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer