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!

use of arrays

Status
Not open for further replies.

NesPes

Programmer
Dec 15, 2007
17
US
I would like to copy an entire file in an array and search for a keyword "Search". When this keyword is found, i then want to copy next 6 lines from this keyword. This task needs to be repeated for the entire file. Can somebody guide me how to go about this?

Thanks.
 
maybe something like
Code:
for my $num (0..$#array) {
 if ($array[$num] =~ /Search/) {
  for my $tnum (1..6) {
   print "$array[$num + $tnum]\n";
  }
 }
}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
It works. Now i want to do additional processing on these 6 lines of code extracted. So i want to move these 6 lines again to a seperate array and somehow keep an index for these 6 lines so that when iam done with the first search, the pointer should again move to the first line of the next 6 lines.
 
You can use push to push them into a new array.. but I'm not sure what you meant by the rest of you statement. You should take the code, modify it and post it back with your questions.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I have modified your code to print based on two search parameters. To continue, i want to move 4 lines of code which are retrieved in the first part of the if statement to array1 and 1 line of code retrieved in the second part of the elsif statement to array2.

Code:
open(f1,"input.log");

@array = <f1>;
for my $num(0..$#array)
{
if($array[$num] =~ /Search string 1/)
{
for my $tnum(1..4)
{
print "$array[$num+$tnum]\n";
push @array1 = $array[$num+$tnum];
}
}
elsif($array[$num] =~ /Search string 2/)
{
for my $tnum(0)
{
print "$array[$num+$tnum]\n";
}
}
}
 
well besides adding a second push statement that looks like what the code is doing.

Code:
elsif($array[$num] =~ /Search string 2/)
           {
            for my $tnum(0)
            {
             print "$array[$num+$tnum]\n";
            }
          }
can be written as
Code:
elsif($array[$num] =~ /Search string 2/)
           {
             print "$array[$num]\n";
             push @array2, $array[$num];

          }

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I did not get you. I am trying this code in the elsif, but when i try to print the contents of array2, i see blank lines. Am i missing something?

My Code:
elsif($array[$num] =~ /Search string 2/)
{
for my $tnum(0)
{
print "$array[$num+$tnum]\n";
push @array2, $array[$num];
}
}
 
I changed my code a little bit. I am moving selective contents of the array into files (A1 and A2). I now want to process these files. But when i run the code, the control never goes to the while loop although i am able to see these files created. I am unable to understand this behaviour. Please help me on this.


open(f1,"input.log");
open(f2,"+> A1.log");
open(f3,"+> A2.log");

@array = <f1>;
for my $num(0..$#array)
{
if($array[$num] =~ /String 1/)
{
for my $tnum(1..4)
{
printf(f2 "%s\n",$array[$num+$tnum]);
}
}
elsif($array[$num] =~ /string 2/)
{
for my $tnum(0)
{
printf(f3 "%s",$array[$num]);
}
}
}

while(<f3>) {
if($_ == /string 3/) {
@temp = split(/\W+/, $_);
$time_stamp = $temp[1];
print "$time_stamp\n";
}
}

 
pretty doubtful this condition will ever evaluate to true:

if($_ == /string 3/) {

see if you can figure out way.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You've opened the f3 filehandle in readwrite mode. However, you are at the end of the stream since you've just did a bunch of writing. To go back to the beginning of the file either use seek or close and reopen the file in readonly mode.

Code:
[url=http://perldoc.perl.org/functions/seek.html][black][b]seek[/b][/black][/url] f3, [fuchsia]0[/fuchsia][red];[/red]

[olive][b]while[/b][/olive][red]([/red]<f3>[red])[/red] [red]{[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top