CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Adding to array...

 



chrisdeffer
Novice

Nov 12, 2002, 10:21 AM

Post #1 of 12 (1336 views)
Adding to array... Can't Post

My problem deals with content of

an array I am working with.

@natsources =

ip nat inside source static 167.125.250.193 10.76.250.193
ip nat inside source static 167.125.250.194 10.76.250.194
ip nat inside source static 167.125.250.195 10.76.250.195
ip nat inside source static 167.125.250.196 10.76.250.196
ip nat inside source static 167.125.250.197 10.76.250.197
ip nat inside source static 167.125.250.198 10.76.250.198
ip nat inside source static 167.125.250.199 10.76.250.199

How do I add "no" right before "ip" onto each one of the $elements? I am using a foreach loop and join but it always adds "no" into each element like so:

"ip nat inside source static 167.125.250.199 10.76.250.199no"

I need the "no" with a space at the beginning of the $elements.

There is something very simple I am missing here!!! Please help ;)

Thanks


thodi
stranger

Nov 12, 2002, 11:27 AM

Post #2 of 12 (1333 views)
Re: [chrisdeffer] Adding to array... [In reply to] Can't Post

Ah, Cisco :-)

Code
foreach (@natsources) { 
$_ = "no $_";
}

That's just one of many ways to do it.


chrisdeffer
Novice

Nov 13, 2002, 9:15 AM

Post #3 of 12 (1321 views)
Re: [thodi] Adding to array... [In reply to] Can't Post

The problem with that is "no" is still in the wrong place.

foreach $individuals (@natsources) {
$individuals = "no $individuals";

produces:

ip nat inside source static 167.125.250.217 10.76.250.217no

If i newline the code, the result puts no on it's own line.

How do I get the output so it looks like this:

no ip nat inside source static 167.125.250.217 10.76.250.217

Thanks again!


Paul
Enthusiast

Nov 13, 2002, 9:50 AM

Post #4 of 12 (1319 views)
Re: [chrisdeffer] Adding to array... [In reply to] Can't Post

Thodi's code should be fine so either you haven't explained properly or you are doing something wrong.


chrisdeffer
Novice

Nov 13, 2002, 11:52 AM

Post #5 of 12 (1315 views)
Re: [RedRum] Adding to array... [In reply to] Can't Post

Of course I am doing something wrong. That is why I am here.

I used grep to create an array and now I am trying to manipulate the items in the array. Both of the pieces of code do the same thing but not what is needed.

The problem is explained perfectly well so that any grade schooler out there could understand. *With example output to boot*


Paul
Enthusiast

Nov 13, 2002, 12:53 PM

Post #6 of 12 (1312 views)
Re: [chrisdeffer] Adding to array... [In reply to] Can't Post

If you notice you didn't copy Thodi's code sample correctly. In order for it to work you need to use $_ and not $individuals


mhx
Enthusiast / Moderator

Nov 13, 2002, 1:32 PM

Post #7 of 12 (1310 views)
Re: [chrisdeffer] Adding to array... [In reply to] Can't Post

Hi Chris,

indeed, you explained the problem to the best of your knowledge.
But, as RedRum already said, there's no problem with thodi's code.

However, you seem to get a "wrong" result. I think the problem is that your input strings in the @natsources array are already not the way you think they are. Whenever you expect some data to be in a certain way, but you're not sure, use the Data::Dumper module. It's part of standard Perl, and it's invalueable for debugging.

Try this script, it should do exactly what you want:


Code
#!/usr/bin/perl -w 
use strict;
use Data::Dumper;

my @natsources = (
'ip nat inside source static 167.125.250.193 10.76.250.193',
'ip nat inside source static 167.125.250.194 10.76.250.194',
'ip nat inside source static 167.125.250.195 10.76.250.195',
'ip nat inside source static 167.125.250.196 10.76.250.196',
'ip nat inside source static 167.125.250.197 10.76.250.197',
'ip nat inside source static 167.125.250.198 10.76.250.198',
'ip nat inside source static 167.125.250.199 10.76.250.199',
);

print Dumper( \@natsources );

$_ = "no $_" for @natsources;

print Dumper( \@natsources );


Now, put in the code you're using to acquire @natsources:


Code
#!/usr/bin/perl -w 
use strict;
use Data::Dumper;

my @natsources = ... ; # your code

print Dumper( \@natsources );

$_ = "no $_" for @natsources;

print Dumper( \@natsources );


Run the script again and if it does not work the way you expect it, please copy and paste the exact output in your reply.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



jryan
User

Nov 14, 2002, 12:33 AM

Post #8 of 12 (1304 views)
Re: [RedRum] Adding to array... [In reply to] Can't Post


Code
foreach $individuals (@natsources) { 
$individuals = "no $individuals";
}


There's nothing at all wrong with that code; $individuals will be a reference to the current array element the iterator is at. It is the same as:


Code
foreach $_ (@natsources) { 
$_ = "no $_";
}


or the shorthand form:



Code
foreach (@natsources) { 
$_ = "no $_";
}


I suspect the real problem is that there is a newline at the beginning of each line; For instance:


Code
no  
*NEWLINE* ip nat inside source static 167.125.250.193 10.76.250.193 no
*NEWLINE* ip nat inside source static 167.125.250.194 10.76.250.194 no
*NEWLINE* ip nat inside source static 167.125.250.195 10.76.250.195 no
*NEWLINE* ip nat inside source static 167.125.250.196 10.76.250.196



Paul
Enthusiast

Nov 14, 2002, 1:46 AM

Post #9 of 12 (1303 views)
Re: [jryan] Adding to array... [In reply to] Can't Post

Yes I know it is the same Wink, however in your first example $individuals is being created and then nothing happens, whereas using $_ the elements of the original array are modified.


mhx
Enthusiast / Moderator

Nov 14, 2002, 7:16 AM

Post #10 of 12 (1301 views)
Re: [RedRum] Adding to array... [In reply to] Can't Post


In Reply To
...however in your first example $individuals is being created and then nothing happens, whereas using $_ the elements of the original array are modified.


Nope, the for(each) variable is always an alias to the current array element, regardless of it's name.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Nov 14, 2002, 7:18 AM

Post #11 of 12 (1299 views)
Re: [mhx] Adding to array... [In reply to] Can't Post

So it is. You learn something new every day :)


chrisdeffer
Novice

Nov 14, 2002, 10:25 AM

Post #12 of 12 (1294 views)
Re: [RedRum] Adding to array... [In reply to] Can't Post

Thanks for all the suggestions!

The problem was that every element in my array had a "\n" in front of it. I kind of fixed it by using "substr" to get the newline out. Now I have to figure out how to use this fix on all the elements. I'll try using "Data::Dumper" more often to see exactly what is happening with my arrays.

Thanks for the help,

Chris

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives