> Q1) while(<>){
> $data=$data . $_;
> print $data;
> print "loop\n"
> }
>
> and calling it like this c:\>perl simple.pl data.txt
> it should print all the lines of data.txt once but it
> doesn't but it print all the lines 2-3 times before
> next ?
> $data=$data . $_;
here you are appaending the contents of each line to the scalar $data.
Then you print $data. So each time the loop executes you will print the
preceeding lines again and again.
If you just need to print each line then...
while(<>){
$data = $_;
print $data;
print "loop\n"
}
Of course there is no reason to store the contents of $_ in $data - use
one of these instead:
while($data = <>){
print $data;
print "loop\n"
}
while(<>){
print;
print "loop\n"
}
> Q2)i want to write a foreach sort of loop such that
> for each pattern matched, it should return me the
> exact data matched for ex. :
Something liek this:
while(my $data = <>) {
my ($matched) = $data =~ /(xy\w*)/;
print "$matched\n" if defined $matched;
}
Which can be simplified to:
while(<>) {
print "$1\n" if /(\w*xy\w*)/;
}
--
Simon Oliver
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs