Storing a variable in a variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yahalom
    New Member
    • Jun 2007
    • 1

    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;
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    Hi yahalom,

    It's not clear to me what you want to store in which variable. (or why) Your line:
    [code=perl]@var1 = `cat ~/data.txt`;[/code] suggests that you want var1 to be an array (of ip addresses?), but your lines:
    [code=perl]$var1 = var2;
    print $var2;[/code]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

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      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\}

      Comment

      Working...