Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] Re: Reading Files

Expand Messages
  • Ambroise Didier
    I got my file working correctly using the new line character, however, I still have a few problems outputing segments of the file; It s suposed to read the
    Message 1 of 4 , Jul 12, 2005
    • 0 Attachment
      I got my file working correctly using the new line
      character, however, I still have a few problems
      outputing segments of the file; It's suposed to read
      the title of entrie under a certain date; to read the
      date I use a tripod module called TripodDate, there is
      function that adds one day do the date, if you go to
      the prior dates; it doesn't stop for the next date, it
      prints the articles for the next date also, it's
      supposed to print the entries only for the given date.
      here is a simple code the interior loops are used to
      print the article for the if segment and the titles of
      the articles for the else segment.Titre: stands for
      title. Instead of printing the 10 prior dates, or, the
      dates entered in the file, it print only one.it The
      format of the file is:

      date
      Titre:
      Title
      Article
      Titre:
      Title
      Article
      date
      Titre:
      Title
      Article
      and so on...


      use CGI;
      require TripodDate;
      $CGI = new CGI;
      $DATE = new TripodDate;

      if ($CGI->param('month')){

      $month = $CGI->param('month');
      $day = $CGI->param('day');
      $year = $CGI->param('year');
      $date = join('/', $month, $day, $year);
      my @abbr = qw(January February March April May
      June July August september October November December);
      $Adate = "$abbr[$month-1] $day";
      }
      else {
      $date = $DATE->currentDate();
      $Adate ="Today"
      }
      print "
      <html>
      <head>
      <title>Didier's Gazette</title>
      </head>
      <body bgcolor=\"#00B601\" text=\"#BFE943\"
      link=\"#000099\" vlink=\"#990099\">
      <h1 align=\"center\">Didier's Gazette</h1><hr>
      <b>$Adate\'s articles<br><br><hr><table
      border=\"0\"><tr><td rowspan=\"2\" width=\"60%\">";

      $nextdate = $DATE->addNDaysToDate($date, '1');
      if($CGI->param('titre')){
      $titre = $CGI->param('titre');
      $titre =~ s/\%20/ /g;
      open(TODAY,"<gazette.data")||print "Could not
      open file.";
      if(-e "gazette.data"){

      @dataline = <TODAY>;
      $outerflag1 = 1;
      $innerflag1 = 1;
      $i = 0;
      while($outerflag1 && $dataline[$i]){

      if($dataline[$i] =~ /$titre/i){
      print "$date <br>";
      print "$dataline[$i]<br>";
      $j=1;
      while($innerflag1 && $dataline[$i+$j]){

      if(($dataline[$i+$j] =~
      /$nextdate/i) || ($dataline[$i+$j] =~ /Titre:/i)){
      $innerflag1 = 0;
      }
      if($innerflag1){
      print "$dataline[$i+$j]<br>";
      }
      $j++;
      }
      $outerflag1 = 0;
      }
      elsif($i == $#dataline && $outerflag1){
      print "Could not Find Article you're
      Looking For."
      }
      $i++;
      }
      print "<a href=\"index.pl\">Back to index
      page</a>";

      }
      }
      else{
      open(TODAY,"<gazette.data")||print "Could not
      open file.";
      if(-e "gazette.data"){

      @dataline = <TODAY>;
      $outerflag2 = 1;
      $innerflag2 = 1;
      $i = 0;
      while($outerflag2 && $dataline[$i]){
      if($dataline[$i] =~ /$date/i){
      print "$date<br>";
      $j = 1;
      while($innerflag2 && $dataline[$i+$j]){

      if($dataline[$i+$j] =~
      /$nextdate/i){
      $innerflag2 = 0;

      }
      if($dataline[$i+$j] =~ /Titre:/i){
      print "<a href
      =\"index.pl?titre=$dataline[$i+$j+1]&month=$month&day=$day&year=$year\">$dataline[$i+$j+1]</a><br>"

      }

      $j++;
      }
      $outerflag2 = 0;

      }
      elsif($i==$#dataline && $outerflag2){
      print "No messages for this date.";
      }
      $i++;
      }
      }
      }
      close(TODAY);

      open(NEWS,"<gazette.data") || print "Could not open
      file.";

      if(-e "gazette.data"){
      @dataline = <NEWS>;

      for($i = 0; $i <10; $i++){
      $date = $DATE->addNDaysToDate($date, '-1');
      $flag = 1;
      for($i=0; $i<=$#dataline; $i++){

      if($dataline[$i] =~ /$date/i){

      ($month, $day, $year) =
      split(/\//,$date);
      print "<a
      href=\"index.pl?month=$month&day=$day&year=$year\">$date</a><br>";

      $flag = 0;
      }
      elsif($i == $#dataline && $flag){
      print "No messages for previous
      dates.";
      }
      }
      }
      }
      close(NEWS);



      --- Damien Carbery <daymobrew@...> a écrit :


      ---------------------------------
      --- In perl-beginner@yahoogroups.com, "didier2309"
      <didier2309@y...>
      wrote:
      > I 'm writing a program that read files reading
      individual lines
      > using a special separator instead of the new line
      character because
      > I'm having a hard time using it, byt my script still
      won't execute,
      > here's a sampke code:
      >
      > $/="-nl-";
      >
      > $titre = $CGI->param('titre');
      > $titre =~ s/\%20/ /g;
      > open(TODAY,"<gazette.data")||print "Could not
      open file.";
      > if (-e "gazette.data"){
      > @dataline = <TODAY>;
      >
      > for($i=0; $i<=$#dataline; $i++){
      > if($dataline[$i] =~ /$titre/i){
      > print "$date <br>";
      > print "$dataline[$i]<br>";
      >
      > for($j=1; !($dataline[$i+$j] =~
      /Titre:/i) && $dataline
      > [$i+$j]; $j++){
      > print "$dataline[$i+$j]<br>";
      > }
      > last;
      > }
      > elsif($i == $#dataline){
      > print "Could not Find Article you're
      Looking For."
      > }
      >
      > }
      >
      > }
      > }

      I got portions of this working. I reads the file
      correctly, breaking
      it into sections separated by "-nl-". It does NOT
      break it into
      individual lines.

      As you code does not have comments I am not sure what
      your second
      'for' look is trying to do. In the gazette.data file I
      created this
      look caused a runtime warning:
      "Use of uninitialized value in pattern match (m//) at
      ./gazette.pl
      line 18, <TODAY> chunk 3." (line 18 = for($j=1; ...)
      You don't tell us the format of the file, beyond the
      '-nl-' separator.

      Of course, nor do you tell us exactly what the problem
      you are having
      is.

      > but my script still won't execute
      chmod +x your_script.pl; ./your_script.pl

      > open(TODAY,"<gazette.data")||print "Could not open
      file.";
      > if (-e "gazette.data"){
      Yeuch. If you are unable to open the file your code
      tries to read the
      file - mistake. Most replies to this group suggest:
      open(TODAY,"<gazette.data")||die "Could not open
      file: $!";
      This way the script quits if the file could not be
      opened.




      Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html



      ---------------------------------
      YAHOO! GROUPS LINKS


      Visit your group "perl-beginner" on the web.

      To unsubscribe from this group, send an email to:
      perl-beginner-unsubscribe@yahoogroups.com

      Your use of Yahoo! Groups is subject to the Yahoo!
      Terms of Service.


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









      ___________________________________________________________________________
      Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger
      Téléchargez cette version sur http://fr.messenger.yahoo.com
    • Prasanna Goupal
      Hi, Is it possible for u to give me sample data from input file and expected output? Regards, Prasanna didier2309 wrote: I m writing a
      Message 2 of 4 , Jul 18, 2005
      • 0 Attachment
        Hi,

        Is it possible for u to give me sample data from input file and expected output?

        Regards,
        Prasanna

        didier2309 <didier2309@...> wrote:
        I 'm writing a program that read files reading individual lines
        using a special separator instead of the new line character because
        I'm having a hard time using it, byt my script still won't execute,
        here's a sampke code:

        $/="-nl-";

        $titre = $CGI->param('titre');
        $titre =~ s/\%20/ /g;
        open(TODAY,"<gazette.data")||print "Could not open file.";
        if (-e "gazette.data"){
        @dataline = <TODAY>;

        for($i=0; $i<=$#dataline; $i++){
        if($dataline[$i] =~ /$titre/i){
        print "$date <br>";
        print "$dataline[$i]<br>";

        for($j=1; !($dataline[$i+$j] =~ /Titre:/i) && $dataline
        [$i+$j]; $j++){
        print "$dataline[$i+$j]<br>";
        }
        last;
        }
        elsif($i == $#dataline){
        print "Could not Find Article you're Looking For."
        }

        }

        }
        }





        Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html



        SPONSORED LINKS
        C programming language Computer programming languages The c programming language C programming language List of programming languages Programming languages

        ---------------------------------
        YAHOO! GROUPS LINKS


        Visit your group "perl-beginner" on the web.

        To unsubscribe from this group, send an email to:
        perl-beginner-unsubscribe@yahoogroups.com

        Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


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



        __________________________________________________
        Do You Yahoo!?
        Tired of spam? Yahoo! Mail has the best spam protection around
        http://mail.yahoo.com

        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.