Simple Perl Topic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple Perl Topic
# 1  
Old 02-01-2010
Simple Perl Topic

Hi, I am practicing a very simple perl programming, what i want to do is reverse the string

what is wrong with the following
Thanks

Code:
$string = "abcdef";
@array = split(//, $string);
$length =length @array

for ( i=1;i< $length+1;i++)
{
   print "$_\n";

}


Last edited by Scott; 02-01-2010 at 08:59 PM.. Reason: Code tags, please...
# 2  
Old 02-01-2010
First of all the basics

Code:
use strict;
use warnings;

These will help you pointing the errors

and coming to your script
Code:
$length = length (@array);

first of all
The length function simply gives you back the number of characters in a string variable.

so either it can be
Code:
$length = length ($string);

or
Code:
$length = $#array;

all variables should begin with a dollar sign
so
for ( i=1;i< $length+1;i++) is wrong

it should be
Code:
for ( my $i=1;$i< $length+1;$i++)

also arrays are stored from the index 0 and not 1

so ideally it should be
Code:
for ( my $i=0;$i< $length;$i++)

use length or length +1 depending on which variant you use.

And finally the purpose is to reverse
so instead of incrementing the for loop
you should decrement

Eg:-
Code:
for (my $i= $length;$i >=0 ;$i--) {
    print "$array[$i]\n";
}

Also just for your information there is a reverse function in perl as well
Code:
reverse (@array)

HTH,
PL

Last edited by Yogesh Sawant; 03-01-2010 at 01:44 PM.. Reason: added code tags
# 3  
Old 03-01-2010
Dear Friend,

You can use the following code to reverse the string.

Code:
 
use strict;
use warnings;
use Data::Dumper;

my $str="welcome";
my @array;
 @array=split(//,$str);
 @array=reverse(@array);
print Dumper \@array;

# 4  
Old 03-01-2010
Using unpack,

Code:
use strict;
use warnings;
my $str="welcome";
print reverse unpack("A1" x length($str),$str);

# 5  
Old 03-01-2010
or even
Code:
print scalar reverse $string;


Last edited by radoulov; 03-01-2010 at 11:17 AM.. Reason: Code tags, please!
# 6  
Old 03-01-2010
Or:

Code:
% perl -E'say~~reverse"abc"'
cba

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple Perl question

Hello, I'm completely new to Perl and I'm just looking for a quick answer to some code I'm trying to come up with. I'm trying to access a website, part of the URL I want the user to be able to define via standard input. As you can see below I'm still trying to get the syntax. ... (2 Replies)
Discussion started by: wxornot
2 Replies

2. Shell Programming and Scripting

Can Help me in simple perl code

Hi all i want a program have input file .txt and process it then got output Ex: input: King Saud University say hi Mr.Ahmed Cena have a car perl is good ! Dr.john is good! output: KSU say hi Mr.AC have a car perl is good! Dr.John is good! so the process take the String has... (3 Replies)
Discussion started by: abdulelah252
3 Replies

3. Shell Programming and Scripting

Simple perl help - converting numbers

Hi friends, I'm very new to perl and got some requirement. I've input numbers which has size of 17 characters like below: -22500.0000000000 58750.00000000000 4944.000000000000 -900.000000000000 272.0000000000000 I need to convert these numbers from negative to positive and positive... (4 Replies)
Discussion started by: ganapati
4 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Shell Programming and Scripting

Simple Perl error

Hello All, I'm trying to run a simple perl script and have the below error. Tried to google it but could not get a precise solution. Could you please help me out. Can't locate object method "are" via package "equal" (perhaps you forgot to load "equal"?) at ./equal.pl line 9, <STDIN> line 2. ... (3 Replies)
Discussion started by: lovesaikrishna
3 Replies

6. Shell Programming and Scripting

Hopefully a simple script, bash or perl...

I'm attempting to parse a file whose contents follow this format; 4:/eula.1028.txt: 8:/eula.1031.txt: 19:/eula.1033.txt: 23:/eula.1036.txt: 27:/eula.1040.txt: 31:/eula.1041.txt: 35:/eula.1042.txt: 39:/eula.2052.txt: 43:/eula.3082.txt: The number of lines of the file... (4 Replies)
Discussion started by: CudaPrime
4 Replies

7. Shell Programming and Scripting

Simple perl question

I am totally new to perl. I am modifying someone else's script. I have the following output: # ./some-perlscript A B C D E B - E, is generated through the print command that I put in the script. I want to remove A, it seems it is generated automatically by a custom OS it is querying when... (3 Replies)
Discussion started by: streetfighter2
3 Replies

8. Shell Programming and Scripting

simple socket programming in perl

hi i want to write simple socket program which will listen on socket . here is the code ## read msg on socket #! /usr/bin/perl use IO::Socket::INET; my $MySocket= IO::Socket::INET->new(LocalPort=>1234, Proto=>'udp') ; while ()... (2 Replies)
Discussion started by: zedex
2 Replies

9. UNIX for Dummies Questions & Answers

a simple perl

not sure if i should post here or "shell programming" anyway, i am just start learning perl. 1 #!/usr/local/bin/perl 2 3 $test1="Iam"; 4 if ($test1=="anything") 5 { 6 print "show me\n"; 7 } when i run the program, it display "show me" all... (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question