?
Solved

PERL SIMPLE HELP

Posted on 2003-03-27
35
Medium Priority
?
257 Views
Last Modified: 2012-05-04
Hey,
        I have a problem... when i do this piece of code:

open (stats, "ID.txt")|| die "SORRY CANT OPEN\n";
my ($stats) = (<stats>);
close(stats);
my ($ID) = $stats +1;  
open (save, ">ID.txt")|| die "SORRY CANT OPEN";
select(save);
print "$ID";
close(save);
########################
anything that i put after it, does nothing... its like it doesnt do anything after that CODE.. Why, and what can i do
0
Comment
Question by:markyello
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 21
  • 12
  • +1
35 Comments
 
LVL 1

Expert Comment

by:zerofill
ID: 8220631
This is because you have selected "save" as default filehandle on line 6: select(save);

Put this line at the end of code - after close(save):

select (STDOUT);

and everything will be fine.

Or just dont select default filehandle and do it this way:
open (save, ">ID.txt");
print save "$ID";
close (save);
0
 

Expert Comment

by:manniakk
ID: 8221009
it will be more simple just to put select(STDOUT) after the given example...
0
 

Author Comment

by:markyello
ID: 8221352
Thanks... now i have another problem
i wanna click on a ad that has an ID number,
then it goes to the next page which opens a file adn searches for that ID number and then strippes it...
then i want it to override the file with the new file that doesnt have that id number anymore...
here is my code, all it does it take the ID number out...
but im having trouble saving it to a variable and then overriding the file with the variable with the new stuff...
here's my code:

#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header();


my $line;
my ($id) = ($q->param("id"));
my ($delete) = ($q->param("delete"));
my ($type) = ($q->param("type"));
my $file;



if($type eq 'Wanted To Buy'){
$file = "buy.txt";
}elsif($type eq 'For Sale'){
$file = "sell.txt";
}else{
print "We Cant Determine if this ad is, Wanted -or- Selling<br> Please Email Site Admin... Or Try Again Later";
}

if($delete eq 'yes'){
delyes();
}elsif($delete eq 'no'){
delno();
}


sub delyes{
open(file, "$file")|| die "Cant freaking Open File";
my $line;
my @line = (<file>);
while ($line = pop @line){
unless($line =~ /$id/){
print "File: $line<br><br>";
close(file);
}
}
}

sub delno{
print "AN ERROR HAS OCCURRED";
}





WHAT IS THE PROBLEM WITH THAT AND HOW DO I SAVE(OVERRIDE) EVERYTHING THAT DOESNT CONTAIN THE ID NUMBER, TO THE FILE..???
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:riceri
ID: 8223875
with:

sub delyes{
open(file, "$file")|| die "Cant freaking Open File";
my $line;
my @line = (<file>);
while ($line = pop @line){
unless($line =~ /$id/){
print "File: $line<br><br>";
close(file);
}
}
}

Do you what to remove only the line that have the id and save all the rest in the file?
0
 

Accepted Solution

by:
riceri earned 80 total points
ID: 8224345
If thats what you want then this is something that can do it.

sub delyes {
  open (File,"+<$file") || die "Cant freaking Open File";
  seek (File,0,0);
  my @in = <File>;
  my @out;

  foreach $row (@in) {
    chomp($row);
    if ($row ne $id) {
      push @out, "$row\n";
    }
  }

  seek (File,0,0);
  print (File @out);
  close (File);
}

0
 

Author Comment

by:markyello
ID: 8228522
riceri:
thats what i want...
i want it for my computer posting site called eposting

www.yeago.net/eric

goto it.. and see, but ill try your advice and see if it works...
THANKs
0
 

Author Comment

by:markyello
ID: 8228574
Riceri:
That didnt work...
something weird happened thought...
it didnt do nothing when it was like this:

push @out, "$row\n";
but did something when you replace the \n with a <br>

push @out, "$row<br>";
but that that did was put everything on the same line... and still didnt delete the ID number from the file


Weird Huh...
 Any More Suggestions.. please adn thankyou
Please BE ASAP if possible
0
 

Author Comment

by:markyello
ID: 8228603
Riceri:
That didnt work...
something weird happened thought...
it didnt do nothing when it was like this:

push @out, "$row\n";
but did something when you replace the \n with a <br>

push @out, "$row<br>";
but that that did was put everything on the same line... and still didnt delete the ID number from the file


Weird Huh...
 Any More Suggestions.. please adn thankyou
Please BE ASAP if possible
0
 

Expert Comment

by:riceri
ID: 8228608
A <br> is HTML and only works in webbrowsers.
\n is a line feed in a text file.

If you want it on more then one row you need to have the \n there.

My code looks for the value $id.
I don't know how your textfile looks like you have never told .

I think you want it like this: (random values)
4234234
4242342
4253466
6456456
6456456
6765657
7567465

If $id is any of those values it will be removed.
0
 

Author Comment

by:markyello
ID: 8228615
hey do you have an Email address or an AOL s/n
cause im online right now and it seems that you are too
and i have some questions
0
 

Expert Comment

by:riceri
ID: 8228625
You can find me on IRC or ICQ
0
 

Author Comment

by:markyello
ID: 8228638
Ok here's how it works...
my  website is a posting, a computer posting website...
the first one is what type of ad it is (Buying, or For Sale), the next one is the name, then the computer type, then the location, then the date(timestamp) then the description then the picture URL, then the ID number...
if you goto my website...
www.yeago.net/eric
and post an ad then you will understand more... then when you view the ads, you can press delete, adn it will send you an email and some other stuff ill figure out later... but the last thing on the line is the ID number... and i want when you click on the ad it takes that whole line away(or the posted ad) away...

this is what my textfile looks like
##############################
##############################
For Sale~NEW~Eric~NEW~Desktop PC~NEW~someone@somewere.com~NEW~Region/Other~NEW~March-28-2003~NEW~Descriptions~NEW~http://www.yeago.net/eric/Images/elogo.jpg~NEW~167
##############################
##############################


you probably didnt get any of taht
so whats your AOL or AIM, s/n so i can talk to you
im only 14yrs old.. and alittle stuck on my site's improvement and i thank you for helping me
0
 

Author Comment

by:markyello
ID: 8228641
whats IRC adn the other thing??
0
 

Author Comment

by:markyello
ID: 8228647
were do i do that at?
0
 

Expert Comment

by:riceri
ID: 8228661
Don't got any AIM and no AOL.

I still don't get this....

Why do you have the sales and buy in the same file??
Why not one file for one thing?
0
 

Author Comment

by:markyello
ID: 8228666
i do have it like that... if the ad is a buying ad then the ad gets saved into buy.txt
and if its a sell ad it gets sent to sell.txt
0
 

Expert Comment

by:riceri
ID: 8228667
http://www.mirc.com - A windows IRC client(I don't got time to help you learn it)
http://www.icq.com - What Microsoft Messanger is a copy of.
0
 

Author Comment

by:markyello
ID: 8228668
whats IRC, can i talk to u there
0
 

Expert Comment

by:riceri
ID: 8228679
So the goal of this is to collect data from a web form and then later display it once?
0
 

Author Comment

by:markyello
ID: 8228681
im telling you... goto www.yeago.net/eric
adn u will understand alittle more


either that or i can send u every one of my files, for the website and u can look at them


that IRC will take to long
i have Microsoft instant messanger
0
 

Expert Comment

by:riceri
ID: 8228686
You can find me on Efnet with the nick Greyh0und.
0
 

Author Comment

by:markyello
ID: 8228688
well, my post.cgi, displayes whats inside the files, buy or sell and when a new post is submitted and u refresh the posts page it will show the new ad at the top..
so pretty much yea u got it
0
 

Author Comment

by:markyello
ID: 8228695
well, my post.cgi, displayes whats inside the files, buy or sell and when a new post is submitted and u refresh the posts page it will show the new ad at the top..
so pretty much yea u got it
0
 

Expert Comment

by:riceri
ID: 8228697
I have checked your site, and i DON'T see the scripts there only the result.

How shall i see what you do in your scripts?
0
 

Author Comment

by:markyello
ID: 8228700
Which Server Are You On??
0
 

Expert Comment

by:riceri
ID: 8228705
I am on the Efnet network.
0
 

Author Comment

by:markyello
ID: 8228709
here is my post.cgi file

#################################


#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header();

my ($day, $month, $year, $tm, $count, $file);
my ($input_type) = $q->param("what");

print "<html>";

require "Includes/head.txt";

my ($title);

if($input_type eq "buy"){
  $file= "buy.txt";
  $title = "Buying";
}elsif($input_type eq "sell"){
  $file= "sell.txt";
  $title= "Selling";
}else{
  print "
<center>
<font color='green' size='6'>Viewing Ads</font><br>
<a href='posts.cgi?what=sell'>View Selling Ads</a>
&nbsp;&nbsp;&nbsp; || &nbsp;&nbsp;&nbsp;
<a href='posts.cgi?what=buy'>View Buying Ads</a>

";
exit();
}

my ($line, @line) ;
my (@raw_data);
my ($buying,$name,$accessory,$email,$state,$description,$post,$date,$picture);
my (@INCLUDE_TAGS, $html);

@INCLUDE_TAGS = ( "I", "SUP", "FONT", "P" );

print "
<LINK href='style.css' rel=stylesheet type=text/css>
<center>
<font size=5 color='green' face='Verdana'><b>You Are Viewing Ads That People Are $title</b></font>
<br><br>
";

open(DAT, $file) || die("Could not open file!\n");

print "<table width='90%'>";
@line = <DAT>;
#@line = (pop @line);

my $count = 0;
#($line = pop @line) &&

#while ( ($line = pop @line) && ($count < 2) ) {
foreach $line (@line) {
unless($line = pop @line){
}
          $line =~ s/-br-/<p>/gi;
       unless($line =~ /#/){
if($line =~ / /){


       chomp($line);

($buying,$name,$accessory,($email),$state,$date,$description,$picture)=split(/\~NEW~/,$line);

print " 
  <tr>
    <td bgcolor='#999999'>
      &nbsp;&nbsp;&nbsp;<font class='words'><b>Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font class='orange_sm'>$name</font>
      <br>
     &nbsp;&nbsp; <font class='words'><b>Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font class='orange_sm'>$date</font>
      <br>
     &nbsp;&nbsp; <font class='words'><b>Item:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font class='orange_sm'>$accessory</font>
      <br>
    &nbsp;&nbsp;  <font class='words'><b>E-mail:&nbsp;&nbsp;&nbsp;</b></font><a href='mailto:$email'><font class='email'>$email</font></a>
      <br>
    &nbsp;&nbsp;  <font class='words'><b>Location:&nbsp;</b></font><font class='orange_sm'>$state</font>
      <br>
     &nbsp;&nbsp; <font class='words'><b>Description:\n</b></font>
      <hr width=250 align=left>
     
    &nbsp;&nbsp;  <font class='orange_sm'>$description</font>  
      <hr width='250' align='left'>
     
";
#$pic = ' <a href='picture.cgi?picture=$picture'><font size='3'>View Picture</font></a> ';
my ($picture) = ($picture);
print "
  &nbsp;&nbsp;<font size='2' color='white' face='verdana'>Interested about this Ad?</font><br>

<a href='mail.cgi?email=$email'><font class='green_a'>Email Them</font></a>

&nbsp;&nbsp;&nbsp; <font color='white' face='verdana' size='2'> OR </font> &nbsp;&nbsp;&nbsp;
<a href='picture.cgi?type=$buying&name=$name&accessory=$accessory&email=$email&state=$state&date=$date&description=$description&picture=$picture'><font class='green_a'>View Picture</font></a>
</td></tr>
";

  $count++;
      #}
    }
  }  
}
print "
  </table>
</center>
</BODY></HTML>
";

open (stats, "Status/posted.txt")|| die "SORRY CANT OPEN\n";
my ($stats) = (<stats>);
close(stats);
my ($add) = $stats +1;  
open (save, ">Status/posted.txt")|| die "SORRY CANT OPEN";
select(save);
print "$add";
close(save);
0
 

Author Comment

by:markyello
ID: 8228710
if u want any more files then just tell me...
0
 

Author Comment

by:markyello
ID: 8228721
ooo wait..
hang on thats the old file...
let me update it, cause i forgot i did something last night and i erased it and forgot to put the new post.cgi and other stuff

0
 

Author Comment

by:markyello
ID: 8228735
OK everythign is up and running if you wanna goto...
http://www.yeago.net/eric/posts.cgi?what=sell

or here is the new code for posts.cgi

##################

#!/usr/bin/perl -w
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $q = new CGI;
print $q->header();

my $id;
my ($day, $month, $year, $tm, $count, $file);
my ($input_type) = $q->param("what");

print "<html>";

require "Includes/head.txt";

my ($title);

if($input_type eq "buy"){
  $file= "buy.txt";
  $title = "Buying";
}elsif($input_type eq "sell"){
  $file= "sell.txt";
  $title= "Selling";
}else{
  print "
<center>
<font color='green' size='6'>Viewing Ads</font><br>
<a href='posts.cgi?what=sell'>View Selling Ads</a>
&nbsp;&nbsp;&nbsp; || &nbsp;&nbsp;&nbsp;
<a href='posts.cgi?what=buy'>View Buying Ads</a>

";
exit();
}

my ($line, @line) ;
my (@raw_data);
my ($buying,$name,$accessory,$email,$state,$description,$post,$date,$picture);
my (@INCLUDE_TAGS, $html);

@INCLUDE_TAGS = ( "I", "SUP", "FONT", "P" );

print "
<LINK href='style.css' rel=stylesheet type=text/css>
<center>
<font size=5 color='green' face='Verdana'><b>You Are Viewing Ads That People Are $title</b></font>
<br><br>
";

open(DAT, $file) || die("Could not open file!\n");

print "<table width='90%'>";
@line = <DAT>;
#@line = (pop @line);

my $count = 0;
#($line = pop @line) &&

#while ( ($line = pop @line) && ($count < 2) ) {
foreach $line (@line) {
unless($line = pop @line){
}
           $line =~ s/-br-/<p>/gi;
        unless($line =~ /#/){
if($line =~ / /){


        chomp($line);

($buying,$name,$accessory,($email),$state,$date,$description,$picture, $id)=split(/\~NEW~/,$line);

print " 
  <tr>
    <td bgcolor='#999999'>
      &nbsp;&nbsp;&nbsp;<font class='words'><b>Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font class='orange_sm'>$name</font>
      <br>
     &nbsp;&nbsp; <font class='words'><b>Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font class='orange_sm'>$date</font>
      <br>
     &nbsp;&nbsp; <font class='words'><b>Item:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font class='orange_sm'>$accessory</font>
      <br>
    &nbsp;&nbsp;  <font class='words'><b>E-mail:&nbsp;&nbsp;&nbsp;</b></font><a href='mailto:$email'><font class='email'>$email</font></a>
      <br>
    &nbsp;&nbsp;  <font class='words'><b>Location:&nbsp;</b></font><font class='orange_sm'>$state</font>
      <br>
     &nbsp;&nbsp; <font class='words'><b>Description:\n</b></font>
      <hr width=250 align=left>
     
    &nbsp;&nbsp;  <font class='orange_sm'>$description</font>  
      <hr width='250' align='left'>
     
";
#$pic = ' <a href='picture.cgi?picture=$picture'><font size='3'>View Picture</font></a> ';
my ($picture) = ($picture);
print "
  &nbsp;&nbsp;<font size='2' color='white' face='verdana'>Interested about this Ad?</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;<font size='2' color='white' face='verdana'>Did You Post Ad?</font><br>
<a href='mail.cgi?email=$email'><font class='green_a'>Email Them</font></a>

&nbsp;&nbsp;&nbsp; <font color='white' face='verdana' size='2'> OR </font> &nbsp;&nbsp;&nbsp;
<a href='picture.cgi?type=$buying&name=$name&accessory=$accessory&email=$email&state=$state&date=$date&description=$description&picture=$picture'><font class='green_a'>View Picture</font></a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href='deletead.cgi?delete=yes&type=$buying&id=$id'><font class='green_a'>Delete Ad</font></a>
</td></tr>
";
  $count++;
      #}
    }
  }  
}
print "
  </table>
</center>
</BODY></HTML>
";

open (stats, "Status/posted.txt")|| die "SORRY CANT OPEN\n";
my ($stats) = (<stats>);
close(stats);
my ($add) = $stats +1;  
open (save, ">Status/posted.txt")|| die "SORRY CANT OPEN";
select(save);
print "$add";
close(save);
0
 

Author Comment

by:markyello
ID: 8228741
HOW DO U SIGN UP FOR that eris network ??? i cant find a register thing
0
 

Expert Comment

by:riceri
ID: 8228745
You don't have to!!!!

The result of all this is shall be a ad rotate system or?
0
 

Expert Comment

by:riceri
ID: 8228764
I got sleep....

I think you shall get on book on HTML and one on Perl.
I will check back here later.

http://validator.w3.org/check?uri=http%3A%2F%2Fwww.yeago.net%2Feric%2Findex.cgi&charset=%28detect+automatically%29&doctype=HTML+4.01+Transitional
0
 

Author Comment

by:markyello
ID: 8228768
i know 2 years of HTML
and 1 year of Perl
i dont need that...

email me directly here:
markyello@hotmail.com


and i dont get what that last thing u said was:

You don't have to!!!!

The result of all this is shall be a ad rotate system or?


anyways, why doesnt it work
0
 

Author Comment

by:markyello
ID: 8537169
Hey thanks...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

770 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