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: Intermediate:
how to split a table

 



technoman007
New User

Mar 4, 2014, 6:42 AM

Post #1 of 5 (1943 views)
how to split a table Can't Post

Hi All,

i have table in a variable.

$result has the entire table in it - >
name age subjects
------ ----- --------
George 25 bio
mark 23 physics
hamond 22 english
jeremy 24 maths


i am unable to think of a method to split them.
like name=>George, age => 25 , subject=>bio
and so on with others in the table

i would love to use hashes or arrays.

help is really appreciated.
Thanks in advance.


(This post was edited by technoman007 on Mar 4, 2014, 6:51 AM)


FishMonger
Veteran / Moderator

Mar 4, 2014, 7:17 AM

Post #2 of 5 (1939 views)
Re: [technoman007] how to split a table [In reply to] Can't Post

You would use the split function to separate the fields.
http://perldoc.perl.org/functions/split.html

Show us your code.


technoman007
New User

Mar 4, 2014, 8:11 AM

Post #3 of 5 (1935 views)
Re: [FishMonger] how to split a table [In reply to] Can't Post


Code
#!/usr/bin/perl 
use strict;
use Data::Dumper;

my $dataserver=$ARGV[0];
my $username=$ARGV[1];
my $pass=$ARGV[2];

if(@ARGV < 1)
{

print "Usage :\n";
print " ./test.pl username dataserver pass\n";
exit;
}

my $result = qx { isql -U$username -S$dataserver -P$pass -s"," -b <<EOF

set nocount on
select * from class where subject="maths"
exit
EOF
};

print $result;

so im stuck thinking how to split if the $result has this

name age subjects
------ ----- --------
George 25 maths
mark 23 maths
hamond 22 maths
jeremy 24 maths


i need the output as
name=>george
age=>25
subject=>maths
and so on.


(This post was edited by FishMonger on Mar 4, 2014, 8:27 AM)


FishMonger
Veteran / Moderator

Mar 4, 2014, 8:54 AM

Post #4 of 5 (1927 views)
Re: [technoman007] how to split a table [In reply to] Can't Post

Please use the code tags when posting code. I added them for you in this case, but please remember to use them in the future.

First, you need to work on using proper code indentation. Proper indentation makes the code easier to read, understand, and maintain.

Second recommendation would be to use the DBI module when interacting with databases instead of using shell commands. The DBI module will give you greater flexibility and control over the db interaction and would allow you to retrieve the records and build the hash in 1 simple statement.

http://search.cpan.org/~timb/DBI-1.631/DBI.pm

In order to parse your current $result data, you need to use the split function to separate each line placing them into an array.


Code
my @result_set = split /\n/, $result;


Next, use either the shift or splice function to strip off the header and dashed separator line, leaving just the data.
http://perldoc.perl.org/functions/shift.html
http://perldoc.perl.org/functions/splice.html

Then loop over the remaining array elements and split the line into its 3 fields/vars and use those 3 vars to assign that row of data to a HoH (Hash of Hashes) or AoH (Array of Hashes). The choice of data structure will depend on the name field. If there is a possibility of having duplicate names, then you'll want to use the AoH, otherwise I'd use a HoH.

I'm describing what needs to be done rather than giving the complete code for 2 reasons. 1) You've posted this in the Intermediate area which make us assume you have some Perl experience. 2) I get the feeling that this is a class homework assignment which you need to work out.


technoman007
New User

Mar 4, 2014, 9:54 AM

Post #5 of 5 (1923 views)
Re: [FishMonger] how to split a table [In reply to] Can't Post

I am sorry about not adding the code tags and i will use your suggestions for future.

I agree DBI is better and i have tried a basic implementation on my dev box.
Sadly at work we are still stuck at 5.8.0 and sybase DBI needs min 5.8.1 .

Thanks for the idea about split and shift/splice .
i will try that out now.

I am a beginner with perl( between beginner and intermediate :P ) but i do have decent understanding of hashes and your description is very useful.

once again, thanks for explaining.

 
 


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

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