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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl: how to print out d

Status
Not open for further replies.

superfly512

Technical User
Aug 12, 2013
1
US
I'm a Perl newbie and I have a question reqarding to extracting data with a specific format. I have an example file shown below. I would like to extract the data and output with certain column field formats. How can I do this using Perl? Thanks in advance for your help!


Example input file...
----------------------

formal list ( name
FirstName "Joe"
LastName "Jackson"
Address 123 Avenue C
City, TX, Zip Frisco, TX, 75034
?pet ("dog" "cat" "fish")


FirstName "Jay"
LastName "Lin"
Address 1500 St. John Drive
City, TX, Zip Dallas, TX 75001
?Pet "cat"
?HomeOwner "own"

FirstName "Dan"
LastName "Brown"
City, TX, Zip Austin, TX 78753
?Car "Ford Focus"

FirstName "Amy"
LastName "Adam"
City, TX, Zip Austin, TX 78743
?HomeOwner "lease"
?Car "Chevy Tahoe"

FirstName "Hal"
LastName "Oakley"
City, TX, Zip Austin, TX 78731
?Pet ("fish" "dog")
?Car ("Taurus" "Tacoma")
?HomeOwner "own"
)

Example desired output...
-------------------------

FirstName LastName Pet Car HomeOwner
--------- -------- --- --- ---------
Joe Jackson "dog" "cat" "fish"
Jay Lin "cat" "own"
Dan Brown "Ford Focus"
Amy Adam "Chevy Tahoe" "lease"
Hal Oakley "dog" "fish" "Taurus" "Tacoma" "own"
 
Hi

Something like this ?
Perl:
[b]while[/b] (<DATA>) {
  [b]chomp[/b];

  [b]push[/b] @l,$1 [b]if[/b] [fuchsia]m/^(?:First|Last)Name "(.+?)"/[/fuchsia];
  [b]push[/b] @l,$1 [b]if[/b] [fuchsia]m/^\?\w+ \(?([^()]+)\)?/[/fuchsia];

  [b]if[/b] (!$_ && @l) {
    [b]print[/b] [green][i]"@l\n"[/i][/green];
    @l=();
  }
}

[b]print[/b] [green][i]"@l\n"[/i][/green] [b]if[/b] @l;

__DATA__
formal list ( name
FirstName "Joe"
LastName "Jackson"
Address 123 Avenue C
City, TX, Zip Frisco, TX, 75034
?pet ("dog" "cat" "fish")


FirstName "Jay"
LastName "Lin"
Address 1500 St. John Drive
City, TX, Zip Dallas, TX 75001
?Pet "cat"
?HomeOwner "own"

FirstName "Dan"
LastName "Brown"
City, TX, Zip Austin, TX 78753
?Car "Ford Focus"

FirstName "Amy"
LastName "Adam"
City, TX, Zip Austin, TX 78743
?HomeOwner "lease"
?Car "Chevy Tahoe"

FirstName "Hal"
LastName "Oakley"
City, TX, Zip Austin, TX 78731
?Pet ("fish" "dog")
?Car ("Taurus" "Tacoma")
?HomeOwner "own"
)

Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top