Solved

File Operation

Posted on 2008-06-26
21
181 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:Purna2010
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 84

Accepted Solution

by:
ozo earned 500 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
 

Author Comment

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

Author Comment

by:Purna2010
ID: 21896451
"Reference to nonexistent group in regex; marked by <-- HERE"
0
 
LVL 84

Expert Comment

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

Author Comment

by:Purna2010
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 84

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:Purna2010
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 84

Expert Comment

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

did you try http:#a21879009
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Purna2010
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 84

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:Purna2010
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 84

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:Purna2010
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:Purna2010
ID: 21897344
may be i am not communicating properly, sorry for that,.
0
 
LVL 84

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 84

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:Purna2010
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 84

Expert Comment

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

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
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.…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now