473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Perl Arrays

mehj123
55
Hi....

I was playing around with some perl arrays when I stumbled on this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. @arr = ('hi','hello','hithere');
  3. foreach $e (@arr)
  4. {
  5. $e =~ s/h//g;
  6. push(@newarr,$e);
  7. }
  8. print "Old Array:@arr\n";
  9. print " New Array:@newarr";
  10.  
The output I get is

Expand|Select|Wrap|Line Numbers
  1.  Old Array:i ello itere 
  2. New Array:i ello itere
  3.  
Why is the old array being affected??
Sep 28 '07 #1
3 1317
KevinADC
4,059 Expert 2GB
because you are modifying $e, which is each element of the array. then you push it into the new array. Make a copy of $e and modify the copy then push the copy into the new array.
Sep 28 '07 #2
mehj123
55
HI Kevin .. thanks for that fast reply... But just to make myself clear.. I thought that $e will be taken as a separate variable which is not related to the array... is this because I am using foreach??
Sep 28 '07 #3
numberwhun
3,509 Expert Mod 2GB
HI Kevin .. thanks for that fast reply... But just to make myself clear.. I thought that $e will be taken as a separate variable which is not related to the array... is this because I am using foreach??
$e is the variable of the foreach that will hold each consecutive line of the array. As Kevin said, it is the foreach's variable.

Regards,

Jeff
Sep 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Xah Lee | last post by:
Today we'll write a program that can sort a matrix in all possible ways. Here's the Perl documentation. I'll post a Perl and Python version in 2 days. ----------- sort_matrix( $matrix, , ,...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
5
by: John Smith | last post by:
Can someone point me to an example of how to implement and access the kind of object shown below? Most of the examples if found are an object that contains one other object. I need to create an...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
3
by: wombat | last post by:
I have a question about using arrays in Perl, involving connecting to MySQL databases. In the past I've used PHP to access databases and I've been playing around now with Perl. In PHP to access...
2
by: arkascha | last post by:
Hi , we are looking for an efficient method to transport array structures from a php script to some other engine. Currently we offer phps internal serialize function and (if present) the json...
2
by: rathankar | last post by:
dear friends thanks for the mail sent today i have one more question: 1. since in perl arrays need not be declared, , what is the maximum limit of elements an array could have? 2. in java/ c/...
3
by: poolboi | last post by:
hi guys, i dunno if this post should be in Perl or MySQL but i'm using Perl DBI to do manupilations now in MySQL i've got problem trying to input 2 arrays of data into 2 fields at the same...
9
by: grocery_stocker | last post by:
How are references and aliases in perl different than references in aliases in C++?
8
by: Palindrom | last post by:
Hi everyone ! I'd like to apologize in advance for my bad english, it's not my mother tongue... My girlfriend (who is a newbie in Python, but knows Perl quite well) asked me this morning why...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.