Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create arrays from list 1

Status
Not open for further replies.

nychris

MIS
Dec 4, 2004
103
US
I have a report that looks like this. I'm looking for a way where I can store the servers for each date in an array named after the date listed.

Code:
07/28/2009
serverA

07/30/2009
serverB
serverX
serverZ

07/31/2009
serverX
[code]

I want to be able to print the array for 7/30/09 and have it list the 3 servers.  Any ideas?

--
Chris
RHCE, SCSA, AIX5L, LPIC, CNE, CCNA, MCSE
 
Going by the very slim example of data, this is a barebones of a script you can expand on:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%ReportHash[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$key[/blue][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$REPOPT[/blue] ,[red]'[/red][purple]path/to/report/file[/purple][red]'[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple][blue]$![/blue][/purple][red]"[/red][red];[/red]
[olive][b]while[/b][/olive][red]([/red] <[blue]$REPORT[/blue]>[red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [olive][b]next[/b][/olive] [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red])[/red][red];[/red][gray][i]#skip blank lines[/i][/gray]
   [olive][b]if[/b][/olive] [red]([/red][red]m#[/red][purple]^([purple][b]\d[/b][/purple]{1,2}/[purple][b]\d[/b][/purple]{1,2}/[purple][b]\d[/b][/purple][purple][b]\d[/b][/purple]{2,4})[/purple][red]#[/red][red])[/red] [red]{[/red]
      [blue]$key[/blue] = [blue]$1[/blue][red];[/red]
   [red]}[/red]
   [olive][b]else[/b][/olive] [red]{[/red]
      [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [blue]@[/blue][red]{[/red][blue]$ReportHash[/blue][red]{[/red][blue]$key[/blue][red]}[/red][red]}[/red],[blue]$_[/blue][red];[/red]
   [red]}[/red]
[red]}[/red]
[olive][b]for[/b][/olive] [red]([/red][blue]@[/blue][red]{[/red][blue]$ReportHash[/blue][red]{[/red][red]'[/red][purple]07/30/2009[/purple][red]'[/red][red]}[/red][red]}[/red][red])[/red] [red]{[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$_[/blue],[red]"[/red][purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this line:

Code:
if (m#^(\d{1,2}/\d{1,2}/\d\d{2,4})#) {

better written as:

Code:
if (m#^(\d{1,2}/\d{1,2}/\d{2,4})#) {

if the date is always in MM/DD/YYYY you could just use that pattern exclusively:

Code:
if (m#^(\d\d/\d\d/\d\d\d\d#) {

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin, this is excellent! Thank you very much!

--
Chris
RHCE, SCSA, AIX5L, LPIC, CNE, CCNA, MCSE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top