468,514 Members | 1,625 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,514 developers. It's quick & easy.

Storing a variable in a variable

I need to do the following.

I want a variable ($var1) to be stored in ($var2).

This is what I have done:

#!/usr/bin/perl

@var1 = `cat ~/data.txt`; #this file contains also IPs which I need.

foreach $line (@var1){
if($line =~ /[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}/){;
print $line;
}
}

This should print out all the IPs.

It doesn't work. When I do in terminal:
grep [[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\} data.txt

That works.

Any ideas?

P.S. My final aim is to do this:
$var1 = var2;
print $var2;
Jun 27 '07 #1
2 1155
prn
254 Expert 100+
Hi yahalom,

It's not clear to me what you want to store in which variable. (or why) Your line:
Expand|Select|Wrap|Line Numbers
  1. @var1 = `cat ~/data.txt`;
suggests that you want var1 to be an array (of ip addresses?), but your lines:
Expand|Select|Wrap|Line Numbers
  1. $var1 = var2;
  2. print $var2;
suggest something else. (Perhaps you want var1 to contain the name of the second variable?) And why is that your "final aim"? What relation does that have to the IP addresses(?) that you said you were after?

Does data.txt contain anything other than IP addresses? Do you want that other stuff filtered out? What do you really want to do with the IP addresses that are in data.txt?

Best Regards,
Paul
Jun 27 '07 #2
KevinADC
4,059 Expert 2GB
Already answered on the other forum where the question is posted. Problem is escaping the meta characters in the regexp is killing the meta meaning and treats them literally:

\{1,3\}
Jun 27 '07 #3

Post your reply

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

Similar topics

2 posts views Thread by Francisco | last post: by
3 posts views Thread by dave | last post: by
12 posts views Thread by Alfonso Morra | last post: by
reply views Thread by Sandra | last post: by
2 posts views Thread by Angel | last post: by
2 posts views Thread by newjazzharmony | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.