There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Tag Cloud
access acer asus bios bsod computer crash drive driver drivers error ethernet excel freeze games gaming graphics hard drive hardware hdmi internet laptop malware memory monitor motherboard netgear network printer problem ram random registry router slow software sound trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless xbox
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Perl: Is it possible to convert a string to an array reference?

Reply  
Thread Tools
andynic's Avatar
Computer Specs
Member with 122 posts.
 
Join Date: May 2007
Location: Amsterdam
Experience: Beginner
03-Nov-2009, 05: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

Tags
array references, cvt array ref to string, perl

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:26 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.