Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel firefox hard drive hardware hdmi hijackthis internet itunes keyboard laptop malware monitor network networking outlook problem ram recovery router screen slow sound spyware trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Perl: Is it possible to convert a string to an array reference?

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

 
Thread Tools
andynic's Avatar
Computer Specs
Member with 49 posts.
 
Join Date: May 2007
Location: Amsterdam
Experience: Beginner
03-Nov-2009, 04:52 AM #1
Perl: Is it possible to convert a string to an array reference?
Hi,
The snippet of code along with its output below is a simplificationn of something I am trying to do via html <INPUT type=hidden> tags in perl CGI. The end-result is the same.

My question is this: considering the variable $str near the end of the snippet, is there a way to "re-make" it into a bona fide array reference so that the error message is not produced, and so that it functions as the original $ptrToX.

#!/usr/bin/perl -w
use strict;

my @x = ("apples", "pears", "bannas");
print "x[1] = $x[1]\n"; # displays "pears"

my $ptrToX = \@x;
print "ptrToX->[1] = $ptrToX->[1]\n"; #displays "pears"

print "ptrToX = $ptrToX\n"; #displays the array reference.

open (OUTFILE, ">x.dat") || die "Can't open file for output";
print OUTFILE "$ptrToX\n";
close (OUTFILE);

open (INFILE, "<x.dat") || die "Can't open file for input";
my $str = <INFILE>;
close (INFILE);

print "str = $str\n"; #displays the array ref (but it's really a string here)
print "str->[1] = $str->[1]\n"; #displays error."Can't use string ("ARRAY(...)") as an ARRAY ref while "strict refs" in use at x.pl line 23"

The actual output:
=============
$ perl -w x.pl
x[1] = pears
ptrToX->[1] = pears
ptrToX = ARRAY(0x10082ade8)
str = ARRAY(0x10082ade8)

Can't use string ("ARRAY(0x10082ade8)") as an ARRAY ref while "strict refs" in use at x.pl line 24.

Thanks for your help.
Andynic
Reply Bookmark and Share

Tags
array references, cvt array ref to string, perl

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 05:07 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.