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:
Reading results back that were dumped?

 

First page Previous page 1 2 Next page Last page  View All


hwnd
User

Apr 12, 2013, 1:57 PM

Post #26 of 45 (1107 views)
Re: [Chris Charley] Reading results back that were dumped? [In reply to] Can't Post

Ok I see what you mean. My question with this is, by using Storable to (store, retreive) to a file or (freeze, thaw) to memory, the format would be hash ref->hash>hashes since I am using selectall_hashref for my needed data, should I format my own data I am retrieving to my dump file so that on one line, it would look like so:


Code
  

id, date, head



Or can I just use the dump file Storable stores and reference to find what I need by using references to my hashes?


Laurent_R
Veteran / Moderator

Apr 12, 2013, 2:03 PM

Post #27 of 45 (1105 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post

When you "thaw" a data structure stored ("frozen") with Storable, you recreate the data structure into memory.


hwnd
User

Apr 15, 2013, 6:57 AM

Post #28 of 45 (1081 views)
Re: [Laurent_R] Reading results back that were dumped? [In reply to] Can't Post

In referencing to closing my filehandles before each instance of running this, can I just use a simple method like below instead of getting into the CORE to find opened handles?


Code
  

# Close if open handles from previous?

close_handles(q/dumpfile/);

# Go ahead and open/store the hash

store ( \%hash, q/dumpfile/ );

sub close_handles {
my $file = shift;
my $fh;

return unless defined $file;

return close($fh) if open $fh, '<', $file ||
defined fileno($fh);

}



FishMonger
Veteran / Moderator

Apr 15, 2013, 8:22 AM

Post #29 of 45 (1077 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post


Quote
# Close if open handles from previous?

What "previous"? Lexical filehandles will automatically be closed when they go out of scope.

Your close_handles sub doesn't make sense. Just use the -r file test to verify if the file is readable and use proper error handling on all open calls.


(This post was edited by FishMonger on Apr 15, 2013, 8:24 AM)


hwnd
User

Apr 17, 2013, 1:24 PM

Post #30 of 45 (1057 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Instead, using Storable I could just do something like this?


Code
  

##SNIPPET##

use Data::Dumper;
use Storable qw/ store_fd fd_retrieve /;

my $href = $dbh->selectall_hashref ( q/SELECT rec_id, rec_date,
rec_head FROM news/, q/rec_id/ );

store_DATA($href);

undef $/;
my $ref = fd_retrieve \*DATA;

print Dumper $ref;


sub store_DATA {
my($data) = @_;

open my $fh, "+>", undef or die "$0: $!\n";

*DATA = $fh;
store_fd $data, \*DATA or die "$0: print: $!";

seek DATA, 0, 0 or die "$0: seek: $!";
}



FishMonger
Veteran / Moderator

Apr 17, 2013, 1:36 PM

Post #31 of 45 (1054 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post

I see several problems with that code.

What happens when you try that?


hwnd
User

Apr 17, 2013, 1:48 PM

Post #32 of 45 (1047 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Well as I stated it was a snippet of code from my script, when I run it, it opens the undef temporary file and stores the data and dumps it out for me.


FishMonger
Veteran / Moderator

Apr 17, 2013, 1:54 PM

Post #33 of 45 (1044 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post

You should explain what you really need to accomplish.

Are you wanting to store this data structure in a file to be used later in this script, or to be retrieved and used in a script executed at some later time?

If it's to be used later in the same script, it makes no sense to store it in an external file. Just keep it in a var which has proper scope.

If it needs to be retrieved in a later script, then why not have that script query the db directly?


hwnd
User

Apr 17, 2013, 2:04 PM

Post #34 of 45 (1041 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Yes I am sorry for not clearing things up, I am wanting to retrieve and used in the same script at some later time. I want to be able to access the stored data to check if items exists and access them. In which I can do that using the hash references.


FishMonger
Veteran / Moderator

Apr 17, 2013, 2:08 PM

Post #35 of 45 (1036 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post


Quote
I am wanting to retrieve and used in the same script at some later time.


Then what you're doing is weird, wasteful and inefficient.

Just use the hash ref retrieved from the db and forget about storing the data structure in an external or in-memory file.


hwnd
User

Apr 17, 2013, 2:30 PM

Post #36 of 45 (1031 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Ok, that makes more sense to me what you have explained just now. In that case,


Code
  

my @items;
for my $k ( keys %$href )
{
if (@items == 10) {
# how would i get every 10 records out of the hash?
# would i just use the c style (for my $i, $i < 10 $i++) ?

@items = (); #clear the data
}
else {
push @items, $k;
}

# rest of data?
}



FishMonger
Veteran / Moderator

Apr 17, 2013, 2:55 PM

Post #37 of 45 (1028 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post

I'm not sure what you're trying to achieve, but I suspect that you're trying to break up the data so you can "page" over the results. If that's the case, then you should look at Data::Page or one of its brothers.

http://search.cpan.org/~lbrocard/Data-Page-2.02/lib/Data/Page.pm

and/or you should look at using the modulus operator.


Chris Charley
User

Apr 17, 2013, 4:27 PM

Post #38 of 45 (1017 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post

A better approach:

Code
my @items;  
for my $k ( keys %$href )
{
push @items, $k;
if (@items == 10) {
# how would i get every 10 records out of the hash?
# would i just use the c style (for my $i, $i < 10 $i++) ?

# print for 1 page of ten items
for my $id (@items) {
print $id, ' ', $href->{$id}{date}, ' ', $href->{$id}{head}, "\n";
}

@items = (); # (clear the data)
}
}

# proces any leftover items after the loop above
for my $id (@items) {
print $id, ' ', $href->{$id}{date}, ' ', $href->{$id}{head}, "\n";
}



FishMonger
Veteran / Moderator

Apr 17, 2013, 4:43 PM

Post #39 of 45 (1013 views)
Re: [Chris Charley] Reading results back that were dumped? [In reply to] Can't Post

Hmm, that's just a very verbose way to write:

Code
for my $key ( keys %$href ) { 
print "$key $href->{$key}{date} $href->{$key}{head}\n";
}


More context from the OP would be needed to suggest any specific code.


(This post was edited by FishMonger on Apr 17, 2013, 4:46 PM)


Chris Charley
User

Apr 17, 2013, 6:04 PM

Post #40 of 45 (1007 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Right, but he wants to limit each page to 10 items. Somewhere in that print routine, he would probably direct it to a separate page somehow?


FishMonger
Veteran / Moderator

Apr 17, 2013, 6:50 PM

Post #41 of 45 (1004 views)
Re: [Chris Charley] Reading results back that were dumped? [In reply to] Can't Post

That's why I suggested Data::Page

Why reinvent the wheel?


hwnd
User

Apr 17, 2013, 8:16 PM

Post #42 of 45 (996 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Right I am familar with using data page, I am trying to do it another way for the reason of not being able to install certain modules on my server. Thanks so far for the help and feedback though. That was the reason initially that I was using selectall_arrayref to split the results with mysql limit, and then trying to use Storable to store the complete hash results into memory or a file and page the dump file by results.


(This post was edited by hwnd on Apr 17, 2013, 8:29 PM)


FishMonger
Veteran / Moderator

Apr 17, 2013, 8:43 PM

Post #43 of 45 (991 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post


Quote

I am trying to do it another way for the reason of not being able to install certain modules on my server

Quote

What makes you think you can't install "certain modules"? The only reason I can think of is that's a requirement of a class homework assignment. Is that the case here?

If you can write to your home directory, you can install pretty much any module you wish.


FishMonger
Veteran / Moderator

Apr 17, 2013, 8:51 PM

Post #44 of 45 (989 views)
Re: [hwnd] Reading results back that were dumped? [In reply to] Can't Post

If you don't wish to, not can't, install modules, then look over the source code of those modules and learn from them and utilize the sections of them that you need to accomplish your task.


hwnd
User

Apr 18, 2013, 5:33 PM

Post #45 of 45 (968 views)
Re: [FishMonger] Reading results back that were dumped? [In reply to] Can't Post

Ok, so I have managed to get everything working. Thanks for the help so far everyone, I have learned multiple ways of doing this.


Code
   

use strict;
use warnings;

use constant PER_PAGE => 10;

use lib qw( /home/88/64/2016488/lib/perl5/lib/perl5 );
use hwnd;

use CGI;
use Data::Page;

my $q = CGI->new;
my $h = hwnd->new;

my $page_id =
defined $q->param('page') ? $q->param('page') : 1;

my $dbh = $h->connect('DBI:mysql:*****:*****',
'*****', '*****');

my $href = $dbh->selectall_hashref ( q/SELECT rec_id, rec_date,
rec_head FROM news/, q/rec_id/ );

my @keys = ( defined $href ? sort {$a <=> $b} keys %$href : () );

my $page = Data::Page->new(scalar @keys, PER_PAGE, $page_id);
my @items = $page->splice(\@keys);

print $q->header;

print $q->h1("Page $page_id (of ", $page->last_page, ')');

my $i = 0;
foreach (@items) {
print ++$i, ": $_, $href->{$_}->{rec_date}, $href->{$_}->{rec_head}\n";
}

print $q->a({-href => '?page=' . $page->previous_page}, 'Prev') if $page->previous_page;
print $q->a({-href => '?page=' . $page->next_page}, 'Next') if $page->next_page;



(This post was edited by hwnd on Apr 18, 2013, 5:39 PM)

First page Previous page 1 2 Next page Last page  View All
 
 


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

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