I need help movingworking code into library function and calling it obj13-2.pl


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions I need help movingworking code into library function and calling it obj13-2.pl
# 1  
Old 09-27-2014
I need help movingworking code into library function and calling it obj13-2.pl

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
I need help moving working code into library function called obj13-lib.pl and call the same function in obj13-2.pl
I am a Linux newbie and this certificate is my first step towards a Linux admin position, hopefully. This question is my last one for this course and I really need your help.
I was able to get obj14-1.pl to work with array and calling a function but those same style is not working on this question.
I think the sub pick is easier to copy over but the @results and @results2 is not working and the code is not working

1. The problem statement, all variables and given/known data:
We are going to re-write the card shuffling script again, this time copy the script, obj12.pl, as obj13-2.pl. Remove the code that does the shuffling and include it as another function in obj13-lib.pl. After the first "hand", first five elements of the first shuffled array, is dealt(printed), call the shuffling function again before "dealing"(printing) another, different "hand"(first five elements of the second shuffled array). Make sure all cards(elements) printed are unique. Because there is a built-in Perl function named "shuffle" do not call your new function "shuffle".

When you are finished, turn in your obj13-2.pl and obj13-lib.pl files.


2. Relevant commands, code, scripts, algorithms:
This is the code I started and prints out 2 sets of random cards:
Code:
sub pick (\@) { my $startingdeck = shift; return splice @{$startingdeck}, rand @{$startingdeck}, 1; } @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", "9 H","10 H","J H","Q H","K H","A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", "9 D","10 D","J D","Q D","K D","A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", "9 C","10 C","J C","Q C","K C", "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", "9 S","10 S","J S","Q S","K S"); 
my @results = (); push @results , pick @startingdeck for 1..5; 
my @results2 = ();
push @results2, pick @startingdeck for 1..5;
Print "@results\n";
Print "@results2\n"

3. The attempts at a solution (include all code and scripts):
OBJ13-lib.pl
Code:
sub pick (\@) { my $startingdeck = shift; return splice @{$startingdeck}, rand @{$startingdeck}, 1; return ($startingdeck) 
sub results {
my @results = (); push @results , pick @startingdeck for 1..5; 
my @results2 = ();
push @results2, pick @startingdeck for 1..5;
Print "@results\n";
Print "@results2\n"; 
} 
} 1;

This is my code for calling the function in obj13-2.pl
Code:
$I = pick(@startingdeck);
$j = results(@results);

@startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H", "9 H","10 H","J H","Q H","K H","A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D", "9 D","10 D","J D","Q D","K D","A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C", "9 C","10 C","J C","Q C","K C", "A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S", "9 S","10 S","J S","Q S","K S"); 

print "@results\n";
print "@results2\n";

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Oreilly School, Sebastopol, CA, USA, Kelly Peterson,
It gives me this error with link to my school:
You are only allowed to post URLs once you have at least 5 posts.


Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by cllinuxhelp; 09-27-2014 at 03:35 PM.. Reason: code tags, removed formatting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

3. Shell Programming and Scripting

Calling two function

Hi, I need to run start_load function for two tables. Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed" Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too. Please let me know how to... (5 Replies)
Discussion started by: sandy162
5 Replies

4. AIX

Calling functions from main program from dlopened library function

Hello All, I am trying to call a function from the calling main program from a dlopened library function, below is the entire code, when I execute it it crashes with sigill. Can you guys help me out I guess I am missing out on the linker flag or something here. besides I am new to AIX and... (1 Reply)
Discussion started by: syedtoah
1 Replies

5. Shell Programming and Scripting

Scripting: Calling Another Function

Hi Guys, How to make a code/script which behaves like some kind of "front-end" then upon choosing from the options within it, it will call another script for that option. To make it clearer, let's say: a. I have a first script which will list all the names of Students. b. Then, once this... (1 Reply)
Discussion started by: rymnd_12345
1 Replies

6. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

7. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

8. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

9. UNIX for Dummies Questions & Answers

Calling on function from file??

This may sounds dumb, but can I call on a function from a file? For example, I have a function file full of functions like below (no shell designation): func { echo "blah blah blah 1" } func2 { echo "blah blah blah 2" } func3 { echo "blah blah blah 3" } Am I able to call on any one... (3 Replies)
Discussion started by: douknownam
3 Replies

10. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies
Login or Register to Ask a Question