Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reg exp and value assignment

Status
Not open for further replies.

LaylaNahar

Programmer
Oct 25, 2006
26
US
Hi,

I have a file with lines in this form:

; MAC=0004f2171cf0

I have detected the lines using /MAC/

is there a way that I can use a kind of 'reverse' of the detection to assign to my variable everything in that line that is not '; MAC='?

thanks ever so much.

LN
 
Code:
if (/MAC/) {
} else {
   # do something
}
I prefer this, because it's more readable to me

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Something like this should work for you:
Code:
if (/MAC=(.+)/) {
  my $temp = $1;
  # Do stuff with $temp
}
 
Hi,
To match if the line does not contain '; MAC='

Code:
open( FH, "filename" ) ;
while( <FH> )
{
  if ( $_ !~ /; MAC=/ )
  {
    my $var = $_ ;
  } 
 }


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Thanks everybody for you answers. I really appreciate your time. I guess a code snippet would have made my question clearer - I'm still enough of a beginner to think that english is better than code. (Sorry)

What I want to do is assign only the hex number to '$listofmac[$i]'.

(Rharsh - I tried to work your solution into my code, but I couldn't figure out how :(

thanks again
LN



open FILE, "< $sipfile" or die $!;
my @linesofsip = <FILE>;
close FILE;

$size = @linesofsip;
$i = 0;
my @listofmac;
$j = 0;

while ($i<$size)
{

## the line coming in looks like: ; MAC=0004f216f022
## if I detect 'MAC' i want to assign
## only the hex digit to '$listofmac[$i]'

if ($linesofsip[$i] =~/MAC/)
{
$listofmac[$j] = $linesofsip[$i];
$j++;
}
$i++;
}
 
Not what I read it as ... but, anyhoo
Code:
open FILE, "< $sipfile" or die $!;
while (<FILE>) {
  if (/MAC(.+)/) {
     push(@maclist, $1);
  }
}
close FILE;
HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
How about something like this?
Code:
open FILE, "< $sipfile" or die $!;
my (@LinesOfSip, @ListOfMac, $line);
while ($line = <FILE>) {
  push @LinesOfSip, $line;
  if ($line =~ m/MAC=([0-9A-Fa-f]+)/) {
    push @ListOfMac, $1;
  }
}
If you don't need all the lines from the text file for anything else, you can get rid of @LinesOfSip from the my statement and the line push @LinesOfSip, $line;
 
thank you ladies & gentlemen (as appropriate)

the $1, $2 thing was really what I needed!
 
Woops, I guess I should have looked again - Paul had already posted what you needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top