CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Substr on one liners command

 



Stefanik
User

Dec 5, 2012, 2:03 AM

Post #1 of 10 (1804 views)
Substr on one liners command Can't Post

Hi all,
I should execute a substring operation in a shell script.
I was thinking to execute the operation using perl one liners.

My code is following:

Code
$String = "Hi there"; 
$Substring = substr($String,0,2);
print "$Substring\n"


Can I execute it one liners?

Thanks


rovf
Veteran

Dec 5, 2012, 3:21 AM

Post #2 of 10 (1800 views)
Re: [Stefanik] Substr on one liners command [In reply to] Can't Post


Code
perl -le '$String = "Hi there"; $Substring = substr($String,0,2);print $Substring'


Or did I miss something in your question?????


Stefanik
User

Dec 5, 2012, 5:17 AM

Post #3 of 10 (1796 views)
Re: [rovf] Substr on one liners command [In reply to] Can't Post

Hi Rovf.
Thanks a lot! Smile


Stefanik
User

Dec 5, 2012, 6:39 AM

Post #4 of 10 (1793 views)
Re: [rovf] Substr on one liners command [In reply to] Can't Post

I've a problem to use the code with a shell variable.
Here is my code:


Code
String="some_date_interval" 

dateinterval_v=`perl -le 'print $String;$Substring = substr($String,0,4);print $Substring'`


In this way the $String print is empty.
$String is declared in unix shell.

I think there is a problem for perl module to read shell variable.


rovf
Veteran

Dec 5, 2012, 8:01 AM

Post #5 of 10 (1788 views)
Re: [Stefanik] Substr on one liners command [In reply to] Can't Post

$ENV{YOUR_SHELL_VARIABLE_GOES_HERE}


Laurent_R
Veteran / Moderator

Dec 5, 2012, 10:33 AM

Post #6 of 10 (1782 views)
Re: [rovf] Substr on one liners command [In reply to] Can't Post

In other words:


Code
String="some_date_interval"  

dateinterval_v=`perl -le '$string = ENV{String}; print $String; $Substring = substr($String,0,4); print $Substring;'`


You could also pass the value as a parameter to your Perl script:


Code
dateinterval_v=`perl -le '$string = shift; print $String; $Substring = substr($String,0,4); print $Substring;' "some_date_interval" `



rovf
Veteran

Dec 5, 2012, 12:12 PM

Post #7 of 10 (1778 views)
Re: [Laurent_R] Substr on one liners command [In reply to] Can't Post


Quote
$string = ENV{String};


Sigil is missing. Should be


Code
$string = $ENV{String};



Laurent_R
Veteran / Moderator

Dec 5, 2012, 1:49 PM

Post #8 of 10 (1758 views)
Re: [rovf] Substr on one liners command [In reply to] Can't Post

Yes of course, you are right, it should be $ENV{envrnmt_var}.


Stefanik
User

Dec 6, 2012, 12:41 AM

Post #9 of 10 (1752 views)
Re: [Laurent_R] Substr on one liners command [In reply to] Can't Post

Thanks both of you.
I try your code, but it doesn't work:


Code
String="some_date_interval"   

dateinterval_v=`perl -le '$StringPerl = $ENV{String}; print $String; print $StringPerl;$Substring = substr($StringPerl,0,4); print $Substring;'`


but the result is empty.
Instead I printout the shell variable and it has the right value.

By the way I use a "work-around" writing the variable in a temporary file, then reading that file from perl module. But this is a worst solution.


rovf
Veteran

Dec 6, 2012, 1:42 AM

Post #10 of 10 (1750 views)
Re: [Stefanik] Substr on one liners command [In reply to] Can't Post

You forgot to export your shell variable to the environment.


(This post was edited by rovf on Dec 6, 2012, 1:43 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives