[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-WebCGI] Reading/searching file problem



>Hi, I have the following problem:
>I need to read a file and search for a match according to the following
>criterias:
>
>
>$match = KIN213:;
>
>open(IN, "$file");
>
>How do I match the line in the file ???? And set the text to var $titel ?
>
>close(IN);
>}
>
>Below are some samples of lines in the file(the jidderish is supposed to
>be random text and that's the text I need to set to the variabel $titel:
>
>OSH435: jfjfdshfjhsdfkjhsdjfhdjhfdjhfjdhf
>KIN213: jfjfdshfjhsd this is a match jdhf
>JAP346: jfjfdshfjhsdfkjhsdjfhdjhfdjhfjdhf
>THA212: jfjfdshfjhsdfkjhsdjfhdjhfdjhfjdhf
>KAN234: jfjfdshfjhsdfkjhsdjfhdjhfdjhfjdhf
>KIN244: jfjfdshfjhsdfkjhsdjfhdjhfdjhfjdhf
>KIN262: jfjfdshfjhsdfkjhsdjfhdjhfdjhfjdhf
>
>Pls. someone help me.
>Your sincerely
>Jimmy Lantz

Hi Jimmy,

if all you need is to filter out lines that start with $match (i.e.
"KIN213:") and remember the text, this might be a solution:
________________

#!perl -w
$file = "data"; # for debugging only

$match = 'KIN213:'; # don't forget the quotes
open(IN, "<$file") || die "Can't open $file: $!\n";

while (<IN>) {
  chomp; # remove line end \n
  if (/^$match\s(.+)/) {

      # ^ matches begin of input or line
      # $match will be interpolated and matches 'KIN213:' in this case
      # \s matches a single white space char (space, tab, ff, lf)
      # (.+) matches and remembers (in $1) any single char (except \n)
      # one or more times

     $title = $1;
     print "match is -$match- AND title is -$title-\n"; # for debugging only
  }#end if
}#end while
close(IN);

print "done ... \n";
________________

Best regards

--Thomas
(Bremen, Germany)



==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org