Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code sample: possible improvements

Status
Not open for further replies.

arcnon

Programmer
Aug 12, 2003
242
US
I have read that linked lists in perl are slow but I can't quite find an example of one.
Is this a linked list? If so how can I change it without losing any of the functionality?
Code:
#!/usr/bin/perl -w

use strict;
system('clear');

my @data;
$data[2] = 'hi there';

my %objREF;
$objREF{poker} = \$data[2];
 
 
print "struct example\n";
print "======================================\n";
print "original value:\n";
print_me();

print "change by array value:\n";
$data[2] = 'joker';
print_me();

print "change value by ref:\n";
${$objREF{poker}} = 'batman';
print_me();

print "change the array\n";
$data[2] = 'robin';
print_me();

sub print_me{
	print "array is: $data[2]\n";
	print "Ref is: ${$objREF{poker}}\n\n";
}
 
AFAIK, linked lists are best supported by hashes, if you don't want it, delete it, then sort

Where did you hear they were slow?
--Paul

cigless ...
 
No, what you posted is not a linked list.
Have you looked at this:
perldoc -q "linked list"
 
Paul
from 1 of the perl books.... I think the perl cookbook is where I read it;

mike
no.... and I didn't even know how to use perldoc. thanks I'll have to look at that.

thanks again guys

 
well I have run into a problem. After implementing my code I can't get at my data.

Code:
my $test = first();
two($test);

sub first{
	my @data;
	my %objREF;

	$data[2] = 'hi there';
	$objREF{poker} = \$data[2];
}

sub two{
	my $objREF = shift;
	
	#print "$objREF{'poker'}";
	#print "$$objREF{'poker'}";
	#print "$objREF->{'poker'}";
	#print "$$objREF->{'poker'}";
	#print "$$$objREF{'poker'}";
	#print "$$$$objREF{'poker'}";
	#print "${$objREF{'poker'}}\n";
	#print "$${$objREF->{'poker'}}\n";

}
What am I mising here
 
arcnon, what kind of results/errors are you getting? Here is a modified version of the code you posted that should work for you.

Code:
my $test = first();
two($test);

sub first{
    my @data;
    my %objREF;

    $data[2] = 'hi there';
    $objREF{poker} = \$data[2];
    return \%objREF;
}

sub two{
    my $objREF = shift;
    print ${$objREF->{poker}}, "\n";
}
 
When I'm messing about with nested data structures, particularly when they have been written by someone else, I normally use Data::Dumper for debugging.
Code:
use Data::Dumper;
...
...
my $ref = getRefToSomethingComplicated();

print Dumper($ref);
It shows arrays delimited by [...] and hashes by {...} in a nice nested format that lets you see what's going on.

 
I restarted and it started working again. used my first ref like rharsh: ${$objREF->{poker}};

amazing what a restart will do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top