Question about using chomp and other functions together 
Author Message
 Question about using chomp and other functions together

Dear All,

I wrote a function to mimic the behavior of the trim function in
Javascript as follows:

sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;

Quote:
}

Then I am using it as follows:

$finalstate = chomp(trim(<STDIN>));

When I try and run it, I get the following message:
Can't modify non-lvalue subroutine call in chomp at ./trans2autnew.pl
line 63, near "))"
Execution of ./trans2autnew.pl aborted due to compilation errors.
(of course, strict and warnings are on).

Could you help me resolving this as I new to Perl (but not in
programming in general)?

Regards,
George



Fri, 04 May 2012 18:45:54 GMT  
 Question about using chomp and other functions together

Quote:

> Dear All,

> I wrote a function to mimic the behavior of the trim function in
> Javascript as follows:

> sub trim($)
> {
>     my $string = shift;
>     $string =~ s/^\s+//;
>     $string =~ s/\s+$//;
>     return $string;
> }

> Then I am using it as follows:

> $finalstate = chomp(trim(<STDIN>));

> When I try and run it, I get the following message:
> Can't modify non-lvalue subroutine call in chomp at ./trans2autnew.pl
> line 63, near "))"
> Execution of ./trans2autnew.pl aborted due to compilation errors.
> (of course, strict and warnings are on).

> Could you help me resolving this as I new to Perl (but not in
> programming in general)?

chomp($finalstate = trim(<STDIN>));

--
RGB



Fri, 04 May 2012 18:54:03 GMT  
 Question about using chomp and other functions together

Quote:

> $finalstate = chomp(trim(<STDIN>));

Therein lies your problem ...

The "most useful" functionality of chomp(), that is, the removal of the
currently-defined end-of-line character from the end of a string (should
it exist) is applied to its _argument_.

The return value is the "less (but occasionally) useful" funcationality,
"Was something removed from the argument?"

Quote:
> Could you help me resolving this as I new to Perl (but not in
> programming in general)?
> Regards,
> George



Fri, 04 May 2012 20:00:56 GMT  
 Question about using chomp and other functions together

Quote:

> sub trim($)
> {
>     my $string = shift;



Quote:
>     $string =~ s/^\s+//;
>     $string =~ s/\s+$//;

       s/\s+\z//, s/\A\s+// for $string;

Quote:
>     return $string;
> }

> Then I am using it as follows:

> $finalstate = chomp(trim(<STDIN>));

No need for chomp, because trim() already
removed all whitespace at both ends.

--
Ruud



Fri, 04 May 2012 20:36:36 GMT  
 Question about using chomp and other functions together

Quote:


>> sub trim($)
>> {
>>     my $string = shift;



>>     $string =~ s/^\s+//;
>>     $string =~ s/\s+$//;

>       s/\s+\z//, s/\A\s+// for $string;

>>     return $string;
>> }

>> Then I am using it as follows:

>> $finalstate = chomp(trim(<STDIN>));

> No need for chomp, because trim() already
> removed all whitespace at both ends.

Assuming that $/ contains some kind of whitespace character(s).

John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway



Fri, 04 May 2012 23:24:25 GMT  
 Question about using chomp and other functions together

Quote:

>Dear All,

>I wrote a function to mimic the behavior of the trim function in
>Javascript as follows:

>sub trim($)
>{
>    my $string = shift;
>    $string =~ s/^\s+//;
>    $string =~ s/\s+$//;
>    return $string;
>}

>Then I am using it as follows:

>$finalstate = chomp(trim(<STDIN>));

>When I try and run it, I get the following message:
>Can't modify non-lvalue subroutine call in chomp at ./trans2autnew.pl
>line 63, near "))"
>Execution of ./trans2autnew.pl aborted due to compilation errors.
>(of course, strict and warnings are on).

>Could you help me resolving this as I new to Perl (but not in
>programming in general)?

>Regards,
>George

\s will remove trailing \n
Perldocs: "\s is a whitespace character and represents  [\ \t\r\n\f]"

If you want to generalize it even more you could do something like below.
You should avoid passing around copies of unknown length strings.
Trim() act's on the existing string (instead of a copy), the last two
return the modified string.

-sln
---------------------
 use strict;
 use warnings;

 sub trim($)
 {
        $_[0] =~ s/^\s+//;
        $_[0] =~ s/\s+$//;
        # wantarray() returns
        # true = list, false = scalar, undefined = void context
        return $_[0] if defined wantarray;
 }

 my $finalstate = <STDIN>;
 trim($finalstate);
 print $finalstate, "<-1-\n\n";

 $finalstate = trim(<STDIN>);
 print $finalstate, "<-2-\n\n";

 print trim(<STDIN>), "<-3-\n\n";



Sat, 05 May 2012 00:52:52 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Help Doing a chomp type function

2. How to convince others using perl instead bourne scripts

3. How to convince others using perl instead bourne scripts

4. Using strict and -d together

5. can USE and IF be used together??

6. Using "strict" and file variables together

7. Module naming question, and others

8. Perl 5.002 and Text::ParseWord Question and others....

9. Newbie question Perl Print function using files

10. Basic syntax question on using arrays returned from function

11. using Module::Function question

12. Question of using stat function

 

 
Powered by phpBB® Forum Software