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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Altering a Variable using ~ s/

Status
Not open for further replies.

nochoice

Programmer
Jun 17, 2003
72
0
0
CA
Hey everyone,

I have a file that is java script. At the beginning of the file I define an array of pictures like this:


images[0]="....stuf...";
images[1]="....stuf...";
images[2]="....stuf...";
...
images[15]="....stuf...";


Anybody can add their images to my database - pretend database-, but there is a max of 15 - i.e. when there are 15 pictures in the array, and another one is added it becomes images[0], images[1] becomes images[2], etc and images[15] is dropped off.

I want to be able to do this:

$line=~ s/images[$count/images[$count1/;

where count is initially 0 and count1 is initially 1 thus incrementing the array by 1 and dropping number 16.

note: I am reading every line from the .js file, incrementing the array and writing the incremented file over the old .js file.

I get this error:

Unmatched [ in regex; marked by <-- HERE in m/images[ <-- HERE 0/ at eng_pics.cgi line 135.


How can I substitute part of a variable for another variable?

I hope I was clear.

Any suggestions would be appreciated.
Thanks,
Michael
 
ok found the problem...

It doesn't like the [

so I replaced it with \[

and everything works perfectly.

Thanks for looking.
Michael
 
In the future, use \Q\E or quotemeta. Less thinking involved, it just escapes everything non-alphanumeric so you don't have to worry about anything.

________________________________________
Andrew - Perl Monkey
 
if you used unshift ARRAY, LIST you would not need to go through the bother of renumbering the array elements - this would do it automatically


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top