mtrasp has asked for the wisdom of the Perl Monks concerning the following question:

Iam working on below script .In the script langfile.txt is not creating and what is happening in for loop .i am not understanding for loop part especially    $tmp =~ s/^\s+//; and $tmp =~ s/ \s+$//;please tell me how can i understand this part.

$query = "pr_SelectLanguages $Number, $operatingsystem"; @results = `osql -U$DB_USERNAME -P$DB_PASSWORD -S$DB_SERVER -Q\"$q +uery\"`; $langlist1 = ""; open(dest,">langfile.txt") or writelog(logdie,"can't open langfile +.txt for writing"); for ($i=2; $i<$#results-1; $i++) { $tmp=@results[$i]; $mytmp=@results; $tmp =~ s/ //; print "$tmp"; print dest $tmp; $tmp =~ s/^\s+//; $tmp =~ s/ \s+$//; chomp($tmp); $langlist1 = $langlist1 . uc $tmp . " "; $langs=$mytmp; } $langlist1 =~ s/\s*$//; close(dest);

Replies are listed 'Best First'.
Re: File is not creating to see the Result
by moritz (Cardinal) on Mar 09, 2010 at 22:34 UTC
    The perlretut document contains all you need to know to understand these expressions.
Re: File is not creating to see the Result
by pemungkah (Priest) on Mar 09, 2010 at 22:56 UTC
    The s/// operator is substitution: find the regex in the first part, and replace it with the string in the second part.

    So those two statements find spaces at the front of the string, and susbstitute nothing for them, and find spaces at the end of the string, and substitute nothing for them.

      Thank you very much for your reply

        The second one probably has a typo. As it stands, it will only remove a space followed by one or more whitespace characters. If there's as single space at the end of the line, that substitution will leave it there.