Quick perl question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick perl question
# 1  
Old 11-24-2003
Quick perl question

Hey everyone,

I am a newbie with perl, and I just have a quick question that may seem really stupid. Like I said, I'm new, so please bear w/ me Smilie

I'm trying to make a really simple program where there are two inputs. First one is just a string, and the next one is a number. Once the user inputs these two things, then the string is printed the X number of times, X being the number entered by the user.

Anybody know of how to do this..? I'll probably be kicing myself for asking the question once I figure out how to do it.

Thanks everyone!

-Jason
# 2  
Old 11-24-2003
Provided this sound too simple for a homework problem ......

Use the x operator.

e.g.

#!/usr/bin/perl

# test.pl
my ($string, $num) = @ARGV;
print $string x $num;



Then try to run by

perl test.pl "ABC" 3

Last edited by cbkihong; 11-24-2003 at 10:11 PM..
# 3  
Old 11-24-2003
Is this homework? I havent written any Perl other than a few simple excercises in the "Learning Perl" by OReilly. This is really a pretty good book, and I would probably learn alot more Perl if I were able to use it at work. As it stands now, my client does not allow any open source software!

Anyway, if I were in your shoes and had to learn something, I would start with this (First focus on making it work, then work at making it efficient):

Read in your arguments. I believe Perl has the C equivalent of argv and argc to process values off of the command line. See the variable <STDIN> to read in values.

Once you have figured out that much, then all you have to do is use a for loop and loop for the number of times entered in from the command line. For each iteration, print the string.

Because there are a number of ways to do things in Perl, you will more than likely be able to accomplish this task with very little code! However, you will get much more out of this excercise if you learn the structure of the program, make the program work like you want it to and then, learn the Perl way of doing things (i.e. fewest lines of source code possible)

Good Luck
# 4  
Old 11-24-2003
As google pointed out learning perl is a very good book to start with. You can access collection of books online at http://perl.hamtech.net
# 5  
Old 11-24-2003
How do they get away with putting the "Perl CD Bookshelf" on the web? Costs about 130 bucks to buy!! I have the Bookshelf on CD and its great....though didnt know I could have pulled it off someones site for free. Are there other O'Reilly CD bookshelves online that you know of?
# 6  
Old 11-25-2003
Another site
http://iis1.cps.unizar.es/Oreilly/perl/

Content seem to be the same though..But these are nice places to pull the docs from

# 7  
Old 11-26-2003
Hey guys,

Thanks for the replies, I was able to get it work with that, I appreciate it. It's not a homework assignment for me. I think it may have been for a friend of mine, it's from a book called "A Little Book on Perl".

My friend took a UNIX class and he got that book with it. I got the book from him, and I'm going through the excercises right now just for fun. I don't have any of the O'Reilly books on Perl, but perhaps I should pick one of those up since they seem to be recommended quite a bit.

I have another quick question here... Another pretty stupid one that seems like it should be pretty obvious, but for some reason when I try it, it doesn't work.

Let's say I have the user enter a four-digit number on the keyboard. Then I want to output the two middle numbers. How would I do something simple like this? If someone could give me a few ideas, that'd be sweet... Thanks Smilie

-Jason
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Matching - Quick Perl Question

Hi everyone I have a quick perl matching question. I have the following file, and I want to use perl to search through the 2nd column and see if it finds any of the month names (e.g.: Jan, Feb, Mar, ... Dec). Here's the file I'm trying to search, and here's the code I have so far. Any help... (5 Replies)
Discussion started by: lucshi09a
5 Replies

2. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

3. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

4. Shell Programming and Scripting

[B]Perl sort quick question[/B]

I've done a quick Google about this, but could not find the answer I want. Say, there is an array like this: @A = qw(cd1 a1 ef a2 hij a12 b2 b4 b22); I want to sort the array in the first order: @sorted = qw(a1 a12 a2 b2 b22 b4 cd1 ef hij); And second order: @sorted = qw(a1 a2 a12 b2 b4... (5 Replies)
Discussion started by: zx1106
5 Replies

5. AIX

quick question

Hi, At best I'm a junior admin with a big problem. My developers have got my root password and mgmt insists they need it. I can't even change it when people knowing it leave. I'm certain they've hardcoded it into routines. I've searched my servers and grepped everything & can't find it. ... (5 Replies)
Discussion started by: keith.m
5 Replies

6. UNIX for Dummies Questions & Answers

Quick question

Hello all, Quick question from a fairly new to Unix developer. if then completedLogFile=$logfile.$(date +%Y%m%d-%H:%M:%S) mv $logfile $completedLogFile fi I understand that this portion of code is simply copying a tmp logfile to a completed logfile when a condition is true. The... (2 Replies)
Discussion started by: JohnnyBoy
2 Replies

7. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

8. Shell Programming and Scripting

Ok quick question

Hi i just wanted to know is there anyway to log the keystrokes on a remote computer? For example i let my nieces play on my other computer downstairs *my computer and the one downstairs are on a LAN* and i want to see everything they type in to make sure they arent doing anything they are supposed... (1 Reply)
Discussion started by: Corrail
1 Replies

9. UNIX for Advanced & Expert Users

Quick VI question

This "SHOULD" be a simple question, but looking through several books has turned up nothing, so I turn once again to the experts!! How do you vi a file so that you can see special characters. I believe my /etc/passwd file is being corrupted during an upgrade process, however the files... (6 Replies)
Discussion started by: Recon
6 Replies

10. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies
Login or Register to Ask a Question