LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-25-2013, 07:47 AM   #1
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 26

Rep: Reputation: Disabled
assign the contents of an array to a new variable in perl


I am writing one of my first perl scripts and I need to create a new variable with the elements of an array. I need to take an IP address entered by a user and manipulate the last octet so it can be used in a router configuration. I am able to split the address into it's four octets. I can also make changes to the array elements as needed, but I can not reassemble the new address after I manipulate the octet I want. Here is my sample code:
Code:
#!/usr/bin/perl
# tests the use of ipcalc function in linux with matching
use strict;
use warnings;

my $ip_addr;
my $net;
my $mask;
my $cidr;
my $pe;
my $ce;
#my $nw_addr;
print "Enter IP address: e.g. 1.1.1.1/32\n";
$ip_addr = <>;
my @ipc_arr=`ipcalc -n -m -b -p $ip_addr`;
        for(@ipc_arr){
        chomp;
        print $_ . "\n";
        }
print "\n";
($net) = $ipc_arr[3] =~ m/=(.+)/;
print "The network is $net \n";
($mask) = $ipc_arr[0] =~ /=(.+)/;
print "\n";
print "The Subnet mask is $mask \n";
print "\n";
($cidr) = $ipc_arr[1] =~ /=(.+)/;
print "The CIDR notation is /$cidr \n";
print "\n";
print "You entered $net/$cidr which equals $net $mask\n";
#my @pe_arr = $net =~ /\.(\d+)$/ ; #only grabs last octet.
#my @pe_arr = $net =~ /(^\d+)\.(\d+)/ ; # returns first two octets

my @pe_arr = $net =~ /(^\d+)\.(\d+)\.(\d+)\.(\d+)/ ; # grabs all 4 octets!!!!!!!
        for (@pe_arr){
        print $_ . "\n";
        ($pe) = $pe_arr[3];
        $pe = ($pe + 1);
        chomp ($pe);
        #my ($nw_addr) = ($pe_arr[0]"."$pe_arr[1]"."$pe_arr[2]"."$pe); #doesn't work
        }
print "the last octect is $pe on the PE \n";
print "\n";
my ($nw_addr) = $pe_arr[0].$pe_arr[1].$pe_arr[2].$pe; #doesn't work
print"\n";
print $nw_addr "\n";
print "\n";
which yields the output:

Code:
Enter IP address: e.g. 1.1.1.1/32
67.54.99.136/29
NETMASK=255.255.255.248
PREFIX=29
BROADCAST=67.54.99.143
NETWORK=67.54.99.136

The network is 67.54.99.136 

The Subnet mask is 255.255.255.248 

The CIDR notation is /29 

You entered 67.54.99.136/29 which equals 67.54.99.136 255.255.255.248
67
54
99
136
the last octect is 137 on the PE 


Can't use string ("675499137") as a symbol ref while "strict refs" in use at ./ipcalc_test.pl line 47, <> line 1.
So, how can I assign $nw_addr with some of the first three elements of @pe_arr and the variable $pe? Additionally I get errors trying to add in the periods between the octets. The script fails and tells me "Scalar found where operator expected at...."
I have tried all kinds of combinations, but can not get the script to work.
Any help is appreciated.

thanks,
Robert
 
Old 04-25-2013, 08:42 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So not being a Perlite, is there no join command in Perl? (seems unlikely)

As for your example, the long way would be:
Code:
my $nw_addr = "$pe_arr[0].$pe_arr[1].$pe_arr[2].$pe";
As you can see, you still need to tell print that dot is a string (or at least this works for me )

Last edited by grail; 04-25-2013 at 08:43 AM.
 
Old 04-25-2013, 10:04 AM   #3
Regnets1
LQ Newbie
 
Registered: Feb 2012
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thanks very much Grail! After all the different combinations I tried, I can't believe I didn't run up against that one. So double quoting the variable definition allows me to create the $nw_addr. And I found if I put parenthesis around $nw_addr, I can print it.

Code:
my $nw_addr = "$pe_arr[0].$pe_arr[1].$pe_arr[2].$pe";
print"\n";
print ($nw_addr);
print "\n";
Thanks For your help! my proof of concept is working as expected now. Funny how "Working script" cures a headache faster than any pill can

here's the output I get now :
Code:
Enter IP address: e.g. 1.1.1.1/32
67.54.99.136/29
NETMASK=255.255.255.248
PREFIX=29
BROADCAST=67.54.99.143
NETWORK=67.54.99.136

The network is 67.54.99.136 

The Subnet mask is 255.255.255.248 

The CIDR notation is /29 

You entered 67.54.99.136/29 which equals 67.54.99.136 255.255.255.248
67
54
99
136
the last octect is 137 on the PE 


67.54.99.137
 
Old 04-25-2013, 11:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure why you need the brackets ... should be able to print without them. Also, you can append your newline to your print statement to with a comma:
Code:
print $nw_addr,"\n";
Maybe one of the Perl gurus will tell you if it is better practice to include the brackets or not?
 
Old 04-25-2013, 02:57 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I could hardly even be called a beginner at perl, but 30 seconds on google determined that it does indeed have a join function.

http://www.misc-perl-info.com/perl-join.html

Code:
my $string = '123.456.789.012/32' ;

my @array = split( /[.\/]/ , $string ) ;

$array[3] = "876" ;

$string = join( "." , @array[0..3] ) ;

$string = $string . "/" . $array[4] ;

print $string , "\n" ;
I got the above working in a quick test. Output: "123.456.789.876/32"

Last edited by David the H.; 04-25-2013 at 02:59 PM. Reason: small code cleanup
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash split array and assign a variable to an element eamesj Programming 3 03-02-2013 12:19 PM
how to assign a u_short variable to a u_char array ? aryan1 Programming 1 07-29-2009 06:15 AM
Confused: how to use variable as the name of an array in perl littletransformer Programming 18 03-13-2008 09:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration