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

parsing string 2

Status
Not open for further replies.

LAdProg2005

Programmer
Feb 20, 2006
56
US
hi all again,

i am going through file parsing strings...
ie.
Date: 08/01/09 By: LAdProg
Info: This is test
by \lad:prog
20'05

i go by line match the string:
$myLine =~ /Date/

then i use substring funtion to get whatever is after ':'
$date=substr($line,5,8);
and so on...issue could be that length of string after : could be any number long

is there a better way of doing this then going through substring...i can see substring not working for info: as after : there is new lines and text with characters could be any number long.

thanks
 
anyway to get info: out of the output? and use either next empty line or 'stop' word in next line to stop looking for info data.

i tried $line =~//n\$/ -> for newline character (doesn't work)
 
LAdProg2005 said:
stop is just terminator to stop looking for info data. in my case it is either next empty line or word stop in next line.
Then try this:
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]$info_found[/blue] = [fuchsia]0[/fuchsia][red];[/red]
[black][b]my[/b][/black] [blue]$infolines[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [blue]$line[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]

[olive][b]while[/b][/olive][red]([/red][blue]$line[/blue] = <DATA> [red])[/red][red]{[/red]
  [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red]([/red][blue]$line[/blue][red])[/red][red];[/red]
  [gray][i]# at the first INFO-line[/i][/gray]
  [olive][b]if[/b][/olive] [red]([/red][blue]$line[/blue]=~[red]/[/red][purple]INFO:[/purple][red]/[/red][red])[/red] [red]{[/red]
    [gray][i]# mark when INFO-line begins[/i][/gray]
    [blue]$info_found[/blue] = [fuchsia]1[/fuchsia][red];[/red]
    [blue]$infolines[/blue] = [red]'[/red][purple][/purple][red]'[/red][red];[/red]
  [red]}[/red]
  [gray][i]# while the line is INFO-line concatenate lines[/i][/gray]
  [olive][b]if[/b][/olive] [red]([/red][blue]$info_found[/blue][red])[/red] [red]{[/red]
    [blue]$infolines[/blue] .= [blue]$line[/blue][red];[/red]
  [red]}[/red]
  [gray][i]# at the last INFO-line[/i][/gray]
  [olive][b]if[/b][/olive] [red]([/red][red]([/red][blue]$line[/blue] =~[red]/[/red][purple]stop$[/purple][red]/[/red][red])[/red] || [red]([/red][blue]$line[/blue] =~[red]/[/red][purple]^[purple][b]\s[/b][/purple]*$[/purple][red]/[/red][red])[/red][red])[/red] [red]{[/red]
    [gray][i]# mark when INFO-line ends[/i][/gray]
    [blue]$info_found[/blue] = [fuchsia]0[/fuchsia][red];[/red]
    [gray][i]# finally process result and print[/i][/gray]
    [blue]$infolines[/blue] =~ [red]s/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][purple] [/purple][red]/[/red][red]g[/red][red];[/red]
    [blue]$infolines[/blue] =~ [red]s/[/red][purple](stop|[purple][b]\$[/b][/purple])[/purple][red]/[/red][purple][/purple][red]/[/red][red]g[/red][red];[/red]
    [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][purple][b]\$[/b][/purple]infolines = '[blue]$infolines[/blue]'[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
  [red]}[/red]
[red]}[/red]


[gray][i]# your data:[/i][/gray]
[teal]__DATA__[/teal]
[teal]Date: 08/01/09          By: LAdProg[/teal]
[teal]INFO:$[/teal]
[teal]        THIS IS TEST01$[/teal]
[teal]        BY \LAD:PROG$[/teal]
[teal]        *A-OK FOR 20'05$[/teal]

[teal]Date: 09/09/09          By: mikrom        [/teal]
[teal]INFO:$[/teal]
[teal]        THIS IS TEST 02$[/teal]
[teal]        BY \MIKROM:PROG$[/teal]
[teal]        *A-OK FOR 20'05$[/teal]
[teal]stop[/teal]
[teal]Date: 09/09/09          By: mikrom        [/teal]
[teal]INFO:$[/teal]
[teal]        TEST3$[/teal]
[teal]        BY \MIKROM:PROG$[/teal]
[teal]        *A-OK FOR 20'05$stop[/teal]
[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]
Output:
Code:
$infolines = 'INFO: THIS IS TEST01 BY \LAD:PROG *A-OK FOR 20'05'
$infolines = 'INFO: THIS IS TEST 02 BY \MIKROM:PROG *A-OK FOR 20'05'
$infolines = 'INFO: TEST3 BY \MIKROM:PROG *A-OK FOR 20'05'
 
LAdProg2005,

I hope you realise mikrom's being very helpful here; I would have red-flagged this thread quite a while ago. In this thread you are continuously come back with your next question for someone else to solve.

The only reason I haven't red-flagged it now is in recognition of the amount of effort mikrom's put into answering you.

Mike

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top