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

Break array in parts at desired desired string.

Status
Not open for further replies.

Mayur09

Programmer
May 13, 2016
12
US
My array contains Huge log file, with each array element holding each line of the log.

Eg Log File:

I am Fresher line 1
I am Fresher line 2
I am Fresher line 3
.
.
.
I am Fresher line 64
I am Fresher line 65
Yo… I am not a fresher, starting my assignments line 66
Start of the first assignment line 67
Start of the Second assignment line 68
.
.
.
Start of the last assignment ine25880

My array contains 25880 elements starting from 0 to 25879.
I want split my array on line (Element) “Yo… I am not a fresher, starting my assignments line 66


So
new first array will be (of 65 elements in this example)
I am Fresher line 1
I am Fresher line 2
I am Fresher line 3
.
.
.
I am Fresher line 65
And
new Second array will be (of 25815 elements in this example)
Yo… I am not a fresher, starting my assignments line 66
Start of the first assignment line 67
Start of the Second assignment line 68
.
.
.
Start of the last assignment ine25880
 
Like this
Code:
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use warnings[/b][/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array0[/color] = [COLOR=#ff00ff]qw/[/color][COLOR=#ff00ff]a b c You 1 2 3 4[/color][COLOR=#ff00ff]/[/color];
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array1[/color] = ();
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array2[/color] = ();

[COLOR=#0000ff]# search for index[/color]
[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$index[/color] = [COLOR=#ff00ff]0[/color];
[COLOR=#804040][b]until[/b][/color] ([COLOR=#008080]$array0[$index][/color] =~ [COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]Yo[/color][COLOR=#804040][b]/[/b][/color]) {
  [COLOR=#008080]$index[/color]++;
}
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]index = [/color][COLOR=#008080]$index[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

[COLOR=#0000ff]# split array into two[/color]
[COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@array1[/color], [COLOR=#008080]@array0[[/color][COLOR=#ff00ff]0.[/color].([COLOR=#008080]$index[/color]-[COLOR=#ff00ff]1[/color])[COLOR=#008080]][/color];
[COLOR=#804040][b]push[/b][/color] [COLOR=#008080]@array2[/color], [COLOR=#008080]@array0[$index[/color]..[COLOR=#008080]$#array0][/color];

[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]array0 = ([/color][COLOR=#008080]@array0[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]array1 = ([/color][COLOR=#008080]@array1[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]array2 = ([/color][COLOR=#008080]@array2[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

Output
Code:
$index = 3
$array0 = (a b c You 1 2 3 4)
$array1 = (a b c)
$array2 = (You 1 2 3 4)
 
Little typo: It should be
Code:
print "\@array0 = (@array0)\n";
and so on ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top