Help - Search - Members - Calendar
Full Version: Perl help...
4peeps.com Forums > General Hardware/Software > Programmers Corner
FLuXx
I'm learning Perl and I feel I am doing better than I have with Python, Basic, C, C++ you name it...

However my little test program has come to a slight halt...

It gathers information from the user and when done puts the information into a file. Here is my code...

CODE
#!/usr/bin/perl



$fname = &getfname;

$lname = &getlname;

$bday = &getbday;

$interest = &getinterest;

$connection = &getconnection;



$alldone = "n Name: $lname, $fnamen Birthday: $bdayn Interests: $interestn Connection Type: $connectionn";



sub getfname {

    print "First Name: ";

    $fname = <>;

    chop($fname);

    $fname;

}



sub getlname {

    print "Last name: ";

    $getlname = <>;

    chop($getlname);

    $getlname;

}



sub getbday {

    print "Birthday (January 01, 1985): ";

    $bday = <>;

    chop($bday);

    $bday;

}



sub getinterest {

    print "List a few interests: ";

    $interest = <>;

    chop($interest);

    $interest;

}



sub getconnection {

    print "Connection Type: ";

    $connection = <>;

    chop($connection);

    $connection;

}



print $alldone;

open(OUT, ">peoplelist.dat");

print(OUT $alldone);



I've tested this on my FreeBSD shell and it works...kinda. The peoplelist.dat is created except if the program is run again, the list is restarted. So...


How can I make my code here add ONTO the list, instead of overwriting it everytime?
AceHigh
@files = <*.dat>;
foreach $file (@files)
{
open(IN_FH,"<$file"); # open for reading
open(OUT_FH,">new_$file);
@lines = <IN_FH>;
foreach $line (@lines);
{
$new_line = $line . "|$item1|$item2|$item3n");
print(OUT_FH $new_line);
}
}
close(IN_FH);
close(OUT_FH);
ldonyo
Is there an 'APPEND' option for the output directive?

Instead of

open(OUT, ">peoplelist.dat");

something like

open(APPEND, ">peoplelist.dat");

I know squat about Perl, but you ought to be able to append data to a file with it somehow.
FLuXx
QUOTE
@files = <*.dat>;
foreach $file (@files)
{
  open(IN_FH,"<$file"); # open for reading
  open(OUT_FH,">new_$file);
  @lines = <IN_FH>;
  foreach $line (@lines);
  {
      $new_line = $line . "|$item1|$item2|$item3n");
      print(OUT_FH $new_line);
  }
}
close(IN_FH);
close(OUT_FH);


I think I understand that...but I'm not sure where to put it in with the rest of the code...all that replaces

open(OUT, ">peoplelist.dat");
print(OUT $alldone);

right?
AceHigh
As far as I can tell fluxx, you you can use the variables or hard code, but you need to use open in and out and close them to append.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.