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:
How to join reversed words into s atring

 



zak100
User

Feb 18, 2017, 3:35 AM

Post #1 of 7 (2300 views)
How to join reversed words into s atring Can't Post

Hi,
I am able to reverse the words in a string & store them in an array, Now i want to create a string consisting of these reversed words. I am using join but its not working.

Code
# reversing words in a string 
$string = 'Yoda said, "Can you see this?"';
@words = split(" ", $string);
print @words;
@revWords = reverse(@words);
print "\n reverse words are: @revWords";
$revWordsStr = join("", @revWords);
print "\n reverse words string is $revWordStr ";


I am getting no output for join.
>perl revWords.pl
Yodasaid,"Canyouseethis?"
reverse words are: this?" see you "Can said, Yoda
reverse words string is
>
Some body please guide me.

Zulfi.


FishMonger
Veteran / Moderator

Feb 18, 2017, 8:25 AM

Post #2 of 7 (2297 views)
Re: [zak100] How to join reversed words into s atring [In reply to] Can't Post

You have a typo.

$revWordsStr is not the same var as $revWordStr

You should ALWAYS use the strict and warnings pragmas in every perl script. Those pragmas would have told you about the problem.


Code
#!/usr/bin/perl 

use warnings;
#use strict;

# reversing words in a string
$string = 'Yoda said, "Can you see this?"';
@words = split(" ", $string);
print @words;
@revWords = reverse(@words);
print "\n reverse words are: @revWords";
$revWordsStr = join("", @revWords);
print "\n reverse words string is $revWordStr ";


Quote
c:\test>Perl-1.pl
Name "main::revWordStr" used only once: possible typo at C:\test\Perl-1.pl line 13.
Name "main::revWordsStr" used only once: possible typo at C:\test\Perl-1.pl line 12.
Yodasaid,"Canyouseethis?"
Use of uninitialized value $revWordStr in concatenation (.) or string at C:\test\Perl-1.pl line 13.
reverse words are: this?" see you "Can said, Yoda
reverse words string is


Uncomment the use strict; statement and it will tell you about more problems.

Quote
Possible unintended interpolation of @revWords in string at C:\test\Perl-1.pl line 11.
Global symbol "$string" requires explicit package name at C:\test\Perl-1.pl line 7.
Global symbol "@words" requires explicit package name at C:\test\Perl-1.pl line 8.
Global symbol "$string" requires explicit package name at C:\test\Perl-1.pl line 8.
Global symbol "@words" requires explicit package name at C:\test\Perl-1.pl line 9.
Global symbol "@revWords" requires explicit package name at C:\test\Perl-1.pl line 10.
Global symbol "@words" requires explicit package name at C:\test\Perl-1.pl line 10.
Global symbol "@revWords" requires explicit package name at C:\test\Perl-1.pl line 11.
Global symbol "$revWordsStr" requires explicit package name at C:\test\Perl-1.pl line 12.
Global symbol "@revWords" requires explicit package name at C:\test\Perl-1.pl line 12.
Global symbol "$revWordStr" requires explicit package name at C:\test\Perl-1.pl line 13.
Execution of C:\test\Perl-1.pl aborted due to compilation errors.



zak100
User

Feb 18, 2017, 9:45 AM

Post #3 of 7 (2291 views)
Re: [FishMonger] How to join reversed words into s atring [In reply to] Can't Post

Hi,
Thanks for your advise. I am able to solve my problem.
The correct code is:

Code
#!/usr/bin/perl  

use warnings;
use strict;

# reversing words in a string
my $string = 'Yoda said, "Can you see this?"';
my @words = split(" ", $string);
print @words;
my @revWords = reverse(@words);
print "\n reverse words are: @revWords";
my $revWordsStr = join("", @revWords);
print "\n reverse words string is $revWordsStr ";


This means I have to use the word "my" (without quotes) before using the variables.

Zulfi.


Laurent_R
Veteran / Moderator

Feb 18, 2017, 12:22 PM

Post #4 of 7 (2285 views)
Re: [zak100] How to join reversed words into s atring [In reply to] Can't Post


In Reply To
This means I have to use the word "my" (without quotes) before using the variables.


Yes, this is called declaring the variables, more precisely, in this case, lexical variables. This, together, with:

Code
use strict; 
use warnings;

makes it possible for the compiler to tell you about many of your mistakes, and it will save you a lot of debugging time.

There is more to it, though. Using lexical variables enables you to precisely decide the scope of your variable, i.e. the part of the code where it exists. And this, in turn, will prevent other often subtle bugs. But that's a different story.


zak100
User

Feb 18, 2017, 7:41 PM

Post #5 of 7 (2279 views)
Re: [Laurent_R] How to join reversed words into s atring [In reply to] Can't Post

Hi,
Thanks for your comments.
This is what i got from a web link
"That is, one needs to declare the variable using my before its first use.."

In my opinion, it helps me to identify what value is assigned to the variable at the time of declaration. But this is a digression from other languages and even most perl books donot emphasize on this.

Zulfi.


Laurent_R
Veteran / Moderator

Feb 19, 2017, 1:15 AM

Post #6 of 7 (2274 views)
Re: [zak100] How to join reversed words into s atring [In reply to] Can't Post


In Reply To

In my opinion, it helps me to identify what value is assigned to the variable at the time of declaration. But this is a digression from other languages and even most perl books donot emphasize on this.


Old Perl books don't emphasize this (usually because it did not exist when they were written), but recent books on Perl do. Check for example Modern Perl, by chromatic, which is available as a printed book or under a free-download e-format:

http://modernperlbooks.com/books/modern_perl_2014/
or:
http://onyxneon.com/books/modern_perl/index.html


zak100
User

Feb 19, 2017, 4:55 AM

Post #7 of 7 (2269 views)
Re: [Laurent_R] How to join reversed words into s atring [In reply to] Can't Post

Thanks very good.

Zulfi.

 
 


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

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