LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2001, 10:08 PM   #1
katana
Member
 
Registered: Jun 2001
Posts: 100

Rep: Reputation: 15
perl:foreach


hi!
I'm a perl new newbie recently i tried to do some programming....i got this xxx file, which has:

suzana, roslan

i want to append '@kym.edu.my' to each of the name, so the file would look like this:

suzana@kym.edu.my
roslan@kym.edu.my

so i did this in vi:

#!/usr/local/bin/perl
$file='/directory/of/file/xxx';
open(INFO,$file);
$lines=<INFO>;
close(INFO);
$add="\@kym.edu.my";
@line=split(/,/,$lines);
foreach $name(@line){
print "$name$add\n";
}

but instead i got this result:

suzana@kym.edu.my
roslan
@kym.edu.my

why is this? i've read some documentations but i still cant work out why it turned out the way it did...some help would be nice!

THANKS!!

Last edited by katana; 07-22-2001 at 10:22 PM.
 
Old 07-23-2001, 05:55 AM   #2
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
Uhm... I wouldn't say that anything is wrong! Its just the last entry in the file (that appears to be comma delimited, going by your split statment) will have a \n on the end. If you input file takes the form of
Code:
name1,name2\n
then your code will produce
Code:
name1@domain.com\nname2\n@domain.com\n
which is gonna look like
Code:
name1@domain.com
name2
@domain.com
Try chomping the line so your code will look something like
Code:
#!/usr/local/bin/perl -w
$file = '/directory/of/file/xxx';

open(INFO,$file);
$lines = (<INFO>);
close(INFO);

$add = "\@kym.edu.my";
@line = split(/,/,$lines);
foreach $name (@line) {
    chomp($name);
    print "$name$add\n";
}
You always want to run Perl with -w that way it will give you lots of useful messages about potential problems.

HTH

Jamie...
 
Old 07-23-2001, 09:45 PM   #3
katana
Member
 
Registered: Jun 2001
Posts: 100

Original Poster
Rep: Reputation: 15
thanks a lot jamie!!!
well, it works!...when i ran the program, i got the output:

suzana@kym.edu.my
[space]roslan@kym.edu.my

that's good enough, but right now i'm trying to make the output look like this:

suzana@kym.edu.my
roslan@kym.edu.my

i supposed this is because in the xx file, it has:

suzana, roslan

how can i remove both the , and the space with split?

thanks a lot...i really appreciate you help!!

Last edited by katana; 07-23-2001 at 09:48 PM.
 
Old 07-24-2001, 01:05 AM   #4
sykkn
Member
 
Registered: Apr 2001
Location: Texas
Distribution: Mandrake 8.0, RedHat 7.1
Posts: 48

Rep: Reputation: 15
You can replace the whitespace characters (\s)
with nothing using this line of code:
Code:
$name =~ s/\s+//g;
The whole file would look like this:
Code:
#!/usr/local/bin/perl -w
$file = '\\file.txt';

open(INFO,$file);
$lines = (<INFO> );
close(INFO);

$add = "\@kym.edu.my";
@line = split(/,/,$lines);
foreach $name (@line) {
    chomp($name);
    $name =~ s/\s+//g;
    print "$name$add\n";
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use foreach on filenames with spaces? BrianK Linux - General 3 08-09-2005 12:15 PM
PHP posting value to session using foreach Erhnam Programming 15 02-16-2005 08:31 AM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
PHP Mail and Foreach function Gerardoj Programming 6 10-08-2004 05:24 PM
foreach loops chunky Linux - General 2 07-02-2004 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration