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!! |
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 Code:
name1@domain.com\nname2\n@domain.com\n Code:
name1@domain.com Code:
#!/usr/local/bin/perl -w HTH Jamie... |
thanks a lot jamie!!! :D
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!!:) |
You can replace the whitespace characters (\s)
with nothing using this line of code: Code:
$name =~ s/\s+//g; Code:
#!/usr/local/bin/perl -w |
All times are GMT -5. The time now is 09:02 AM. |