?
Solved

File Operation

Posted on 2008-06-26
21
Medium Priority
?
195 Views
Last Modified: 2011-09-20
I have a file like this
test.txt
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
-------My String-------------------
Some text @@ Some text
Some text @@ Some text
...

What  I am trying to do is  find the "My String"  and store reset of lines after "my string" in to one array .

another one is after doing the above operation i want separate the  before , After the string "@@" in to two arrays.


I unable to find do the first operation. can any body respond to me.



0
Comment
Question by:Test 2010
21 Comments
 
LVL 2

Expert Comment

by:sqd5597
ID: 21877719
The code below should do what you want...although it is by no means clean or elegant

use strict;
my $file;
my $line;
my $text;
my $found=0;
my $i=0;
my @data;
my @before;
my @after;

open FILE, "file1.txt";
$text="MY String";
while ($found==0){
  $line=<FILE>;
  if ($text =~ /$line/){
    $found=1;
  }
}
while ($line=<FILE>){
  chomp $line;
  $data[$i++]=$line;
}
for $i (@data) {
  ($before[$i],$after[$i])=split /\@\@/,$data[$i];
}
0
 
LVL 39

Expert Comment

by:Adam314
ID: 21877837

open(my $In, "<file.txt") or die "file: $!\n";
local $/;
my $all=<$In>;
close($In);
 
$all =~ s/.*My String.*?$//sm;
my @lines = split(/\n/, $all);
 
#Then to seperate
my ($before, $after) = split(/\@\@/, $lines[1]);   #or whatever line, or loop through lines

Open in new window

0
 
LVL 85

Accepted Solution

by:
ozo earned 1500 total points
ID: 21879009
@ARGV=("test.txt");
(/My String/..0) > 1 && push @array,$_ while <>;
/(.*)\@\@(.*)/ and push @before,$1 and push @after,$2 for @array;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Test 2010
ID: 21896448
Hi
I am getting the following error while using the above code.
0
 

Author Comment

by:Test 2010
ID: 21896451
"Reference to nonexistent group in regex; marked by <-- HERE"
0
 
LVL 85

Expert Comment

by:ozo
ID: 21896475
what was the part of the error that shows the regex marked by <-- HERE
0
 

Author Comment

by:Test 2010
ID: 21896893
Reference to nonexistent group in regex; marked by <-- HERE in m/.\lost+found\te
st.txt.d242129cd9f549e4b872d325ddf4ac03@@\main\testing\1 <-- HERE
/ at C:\Documents and Settings\pmokkarala\Desktop\Label.pl line 48, <FILE> line
21.

This is the exact error
0
 
LVL 85

Expert Comment

by:ozo
ID: 21896919
why did you have
/.\lost+found\te
st.txt.d242129cd9f549e4b872d325ddf4ac03@@\main\testing\1/
in a regular expression?
None of the suggestions given had 48 lines.
what was your code?
0
 

Author Comment

by:Test 2010
ID: 21897082
use strict;
use warnings;
my $file;
my $line;
my $text;
my $found=0;
my $i=0;
my @data;
my @before;
my @after;

open FILE, "codechanges_tes.txt";
$text="Code Changes";
while ($found==0){
   $line=<FILE>;
  if ($text =~ /$line/){
    $found=1;
  }
}
while ($line=<FILE>){
  chomp $line;
   $data[$i++]=$line;
}
for $i (@data) {
        ($before[$i],$after[$i])=split /\@\@/,$data[$i];
}

i am using this  the error is hsowing at line  "if ($text =~ /$line/)" .

That is part of the file which i am using /.\lost+found\te
st.txt.d242129cd9f549e4b872d325ddf4ac03@@\main\testing\1/
0
 
LVL 85

Expert Comment

by:ozo
ID: 21897126
sqd5597 probably meant to say
$line =~ /$text/

did you try http:#a21879009
0
 

Author Comment

by:Test 2010
ID: 21897149
ozo:
Can you explain the concept of your program.

@ARGV=("test.txt");
(/My String/..0) > 1 && push @array,$_ while <>;
/(.*)\@\@(.*)/ and push @before,$1 and push @after,$2 for @array;
0
 
LVL 85

Expert Comment

by:ozo
ID: 21897190
@ARGV=("codechanges_tes.txt"); #file to read
(/Code Changes/..0) > 1 && push @array,$_ while <>; # store reset of lines after "Code Changes" into one @array
/(.*)\@\@(.*)/ and push @before,$1 and push @after,$2 for @array; # separate the  before , After the string "@@" into two arrays.
0
 

Author Comment

by:Test 2010
ID: 21897216
I understand what the program is doing and which statement is what. But what i want to know is what that push operation  statement exactly doing.
1. I understand with this (/Code Changes/..0) > 1 ur serching for the line contains the "code changes"  after that what that push operation
2.
0
 
LVL 85

Expert Comment

by:ozo
ID: 21897310
perldoc -f push
       push ARRAY,LIST
               Treats ARRAY as a stack, and pushes the values of LIST onto the
               end of ARRAY.  The length of ARRAY increases by the length of
               LIST.  Has the same effect as

                   for $value (LIST) {
                       $ARRAY[++$#ARRAY] = $value;
                   }

               but is more efficient.  Returns the number of elements in the
               array following the completed "push".
so push @array,$_ adds the line to @array
0
 

Author Comment

by:Test 2010
ID: 21897339
I know push operation.

But i aksed about the logic you have written.
Ok.   $_ is contains the all the lines in the file. When it find's the "code changes" string in the line that particular line will move in to array.( I am assuming @array is initially emprty until string found.)
but  I didn't understand how the rest of lines are stroing in the array.

This is the my main concern i am asking from my first post. may be i am communicating properly sorry for that,.
0
 

Author Comment

by:Test 2010
ID: 21897344
may be i am not communicating properly, sorry for that,.
0
 
LVL 85

Expert Comment

by:ozo
ID: 21897379
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr/(.*)\@\@(.*)/)->explain"
The regular expression:

(?-imsx:(.*)\@\@(.*))

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \@                       '@'
----------------------------------------------------------------------
  \@                       '@'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

so when it matches, the push @before,$1 adds the string before '@@' to the @before array, and the push @after,$2 adds the string after the @@ to the @after array
0
 
LVL 85

Expert Comment

by:ozo
ID: 21897399
if you were wondering about scalar
/My String/..0
it becomes true when /My String/ becomes true, and stays true until after $.==0 becomes true (which is never, so the .. stays true for the rest of the file)

see
perldoc perlop
0
 

Author Comment

by:Test 2010
ID: 21931367
ozo:

Can i know how to keep the error checking for you code. Like If i didn't find the string i have to rise some error flag right?...
0
 
LVL 85

Expert Comment

by:ozo
ID: 21931668
can you check whether there's anything in @before or @after?
0
 

Author Comment

by:Test 2010
ID: 21932017
i didn't get what you are asking . are you asking about your code.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question