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

Reg Expression 1

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
I have a file I need to check everyday to see if a word has changed.

In this word only the third and fourth numbers change but I dont know how to check for changes.

The word to check is called: 1367ftot where the third(6) and fourth number (7)
need to be checked everyday for changes.

Please I need alot of help with getting this to work. I dont even know if I am in the right direction??

[tt]
open(A,&quot;<file1&quot;) || die &quot;error: $!&quot;;
@arrayA = (<A>);
foreach (@arrayA)
$file1 =~ /13\d{2}ftot/;
close(A);

#Somehow make a copy of the file each day and check if it has changed??
open(B,&quot;<file2&quot;) || die &quot;error: $!&quot;;
@arrayB = (<B>);
foreach (@arrayB)
$file2 =~ /13\d{2}ftot/;
close(B);

if ($file1 != $file2)
{
print &quot;Data has been changed\n&quot;;
}
[/tt]

 
Hi,

Does the string you're looking for occur on only one line of the file? Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Mike,

Yes it only occurs in one line.
 
You were on the right track:

### 1st, get the old word ###
my $prev_days_word_file = &quot;/path/to/prev_days_word_file&quot;;
open(PREV, &quot;<$prev_days_word_file&quot;) || die &quot;Couldn't open &quot; .
$prev_days_word_file!&quot;;
my $old_word = &quot;&quot;;
while (<PREV>) {
$old_word = chomp($_);
}

### Next, get the new word ###
my $word_file = &quot;/path/to/word/file&quot;;
open(NEW, &quot;<$word_file&quot;) || die &quot;Couldn't open $word_file!&quot;;
my $new_word = &quot;&quot;;
while (<NEW>) {
chomp; # get rid of newline at the end
if ($_ =~ /(13\d\dftot)/) {
$new_word = $1;
}
}

### See if they're different ###
if ($new_word ne $old_word) {
print &quot;The word changed!\n&quot;;

### Do whever here - word changed ###
}

close(PREV);
close(NEW);

### Write out the new word for tomorrow's test ###
open(PREV, &quot;>$prev_days_word_file&quot;) || die &quot;Can't open &quot; .
&quot;$prev_days_word_file for *output*!&quot;;
print PREV $new_word;

close(PREV);

------------------------------------------------------

Note the use of &quot;>&quot; in the final &quot;open(PREV&quot; - that will
*replace* that file if that file already existed - otherwise
it will create it. It will NOT append.

Please note that this is completely untested - use it
as a starting point, but hopefully it's close.

HTH. Hardy Merrill
 
Hardy,

Many many thanks for your answer! My script now works.

 
hello all the perl experts,

I am relatively new in Perl. I am writing a program which reads a text file containing 96 numbers on 8 rows (12 numbers on each line), and take each number (float) multiply with 50 and a number prompted from the user. I am able to read and store the file into an array. I can't seem to be able to take each number individually to perform the multiplication. It seems like my array has 8 elements instead of 96.

Thanks for any help

DNAmatics
 
I assume it's something related to this:

@array = <FILE>;

It takes each line in the file and stores it as an element of @array. You would have to go through each element and get all the different numbers out of it. Hope the tid-bit helps. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
or like this maybe
[tt]
while(<>){ # take each line from file
chomp; # remove newline character from end of line
@ary = split; # split it into an array
foreach (@ary){ # take each number on the line
$_ *= 50; # do the multiplication
$_ *= $user_number;
}
print &quot;@ary\n&quot;; # output the modified values
}
[/tt]
Mike
______________________________________________________________________
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top