LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   functions in perl such as tr, s etc (https://www.linuxquestions.org/questions/programming-9/functions-in-perl-such-as-tr-s-etc-845107/)

ghantauke 11-18-2010 11:38 AM

functions in perl such as tr, s etc
 
The functions such as s can only be used for variables such as $variable.
Is there any way to use it with arrays too so that it substitutes for each string in all of the array indexes.
I know we can do it using foreach loop or any other loop. But is there any inbuilt function to do it? It'd save me a lot of time. Thanks in advance.

paulsm4 11-18-2010 03:07 PM

No.

"Regular expressions" act on "strings".

Not arrays.

Sorry :)

theNbomr 11-18-2010 05:27 PM

perldoc -f map

--- rod.

paulsm4 11-18-2010 09:11 PM

Hi, theNBomr -

Cool - I never knew that :) Thank you.

However, perldoc also goes on to say that a straightforward "foreach" is usually preferable:
Quote:

Note that $_ is an alias to the list value, so it
can be used to modify the elements of the LIST.
While this is useful and supported, it can cause
bizarre results if the elements of LIST are not
variables. Using a regular "foreach" loop for
this purpose would be clearer in most cases.

theNbomr 11-19-2010 12:52 AM

Yes, as usual in Perl, there's more than one way to do almost anything.
--- rod.

ghantauke 11-19-2010 08:57 AM

Quote:

Originally Posted by theNbomr (Post 4163985)
Yes, as usual in Perl, there's more than one way to do almost anything.
--- rod.

Thanks for the reply guyz.


All times are GMT -5. The time now is 06:38 PM.