Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Get variables from if with regex

by Saved (Beadle)
on Mar 07, 2013 at 14:24 UTC ( #1022239=perlquestion: print w/ replies, xml ) Need Help??
Saved has asked for the wisdom of the Perl Monks concerning the following question:

How could I gather the variables from a multi-part if with RegEx's , or do I need to break it down into separate parts?
while(<IFILE>) { if ((@LINE =~ /#\s+(\w+)\s+(\d+).+/) && (@LINE =~ /!\s+(\w+)\s+(\d+ +).+/)) { $Word=$1; $TimeStamp=$2; $Word2=$3; $TimeStamp2=$4; print "X $Word X $TimeStamp X $Word2 X $TimeStamp2 x"; } }
Thanx, all help much appreciated.
# add 1362072030 dc=ifdsgroup,dc=com dn: uid=nce9834xdho,ou=user,ou=nce,ou=prod ! port 1362 dc=isgrp,dc=com changetype: add ifastCreateDate: 1362072030649 objectClass: account objectClass: ifastUser objectClass: top ifastModifyDate: 1362072030649 uid: nce9834xdho cn: nce9834xdho ifastProducts: WEB ifastGivenName: Omar ifastLanguage: en ifastFailedLogons: 0 ifastLastLogon: 15764 ifastEmailAddr: omar.hafez@td.com ifastSurname: Hafez structuralObjectClass: account entryUUID: e587875c-1616-1032-93e5-4b7d087c5768 createTimestamp: 20130228172030Z modifyTimestamp: 20130228172030Z # end add 1362072030 # add 1362072030 dc=ifdsgroup,dc=com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # end add 1362072030
The suggestion by choroba seems to work, and is part of the solution, but I can't seem to get it working with more than one instance of it in the same if statement: Works:
undef $/; $/="\n\n"; while(<IFILE>) { my @RESULT=(); my @LINES=split (/\n/, $_); if ((grep /objectClass:\s+account/, @LINES) && (grep /objectClass:\s+ifastUser/, @LINES) #&& (my ( $PRODS ) = /ifastProducts:\s+(.+)\s*$/, $_) && (my ( $USER ) = /dn: uid=(\w+?),.+/, $_)) { push(@RESULT, $USER); #push(@RESULT, $PRODS, $USER); print join "|", @RESULT; #print OFILE join "|", @RESULT; #print OFILE "\n"; print "\n"; } }
But this does not:
undef $/; $/="\n\n"; while(<IFILE>) { my @RESULT=(); my @LINES=split (/\n/, $_); if ((grep /objectClass:\s+account/, @LINES) && (grep /objectClass:\s+ifastUser/, @LINES) && (my ( $PRODS ) = /ifastProducts:\s+(.+)\s*$/, $_) && (my ( $USER ) = /dn: uid=(\w+?),.+/, $_)) { #push(@RESULT, $USER); push(@RESULT, $PRODS, $USER); print join "|", @RESULT; #print OFILE join "|", @RESULT; #print OFILE "\n"; print "\n"; } }

Comment on Get variables from if with regex
Select or Download Code
Re: Get variables from if with regex
by daxim (Chaplain) on Mar 07, 2013 at 14:29 UTC
    Please provide some sample input.
Re: Get variables from if with regex
by choroba (Abbot) on Mar 07, 2013 at 14:53 UTC
    In your particular case, it is not so hard to combine the regexes into one:
    while (<IFILE>) { if (my ($word, $time_stamp) = /[#!]\s+(\w+)\s+(\d+)./) { print "[ $word $time_stamp ]\n"; } }
    Note that the binding operator =~ imposes a scalar context on its left operand, so matching an array does not do what you think.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank You
      Thanx, all help much appreciated.
      # add 1362072030 dc=ifdsgroup,dc=com dn: uid=nce9834xdho,ou=user,ou=nce,ou=prod ! port 1362 dc=isgrp,dc=com changetype: add ifastCreateDate: 1362072030649 objectClass: account objectClass: ifastUser objectClass: top ifastModifyDate: 1362072030649 uid: nce9834xdho cn: nce9834xdho ifastProducts: WEB ifastGivenName: Omar ifastLanguage: en ifastFailedLogons: 0 ifastLastLogon: 15764 ifastEmailAddr: omar.hafez@td.com ifastSurname: Hafez structuralObjectClass: account entryUUID: e587875c-1616-1032-93e5-4b7d087c5768 createTimestamp: 20130228172030Z modifyTimestamp: 20130228172030Z # end add 1362072030 # add 1362072030 dc=ifdsgroup,dc=com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # end add 1362072030
      The suggestion by choroba seems to work, and is part of the solution, but I can't seem to get it working with more than one instance of it in the same if statement: Works:
      undef $/; $/="\n\n"; while(<IFILE>) { my @RESULT=(); my @LINES=split (/\n/, $_); if ((grep /objectClass:\s+account/, @LINES) && (grep /objectClass:\s+ifastUser/, @LINES) #&& (my ( $PRODS ) = /ifastProducts:\s+(.+)\s*$/, $_) && (my ( $USER ) = /dn: uid=(\w+?),.+/, $_)) { push(@RESULT, $USER); #push(@RESULT, $PRODS, $USER); print join "|", @RESULT; #print OFILE join "|", @RESULT; #print OFILE "\n"; print "\n"; } }
      But this does not:
      undef $/; $/="\n\n"; while(<IFILE>) { my @RESULT=(); my @LINES=split (/\n/, $_); if ((grep /objectClass:\s+account/, @LINES) && (grep /objectClass:\s+ifastUser/, @LINES) && (my ( $PRODS ) = /ifastProducts:\s+(.+)\s*$/, $_) && (my ( $USER ) = /dn: uid=(\w+?),.+/, $_)) { #push(@RESULT, $USER); push(@RESULT, $PRODS, $USER); print join "|", @RESULT; #print OFILE join "|", @RESULT; #print OFILE "\n"; print "\n"; } }
        Do you really need to split the paragraph into lines? You can match a multiline string against a regex, too:
        #!/usr/bin/perl use warnings; use strict; $/ = "\n\n"; while (<>) { my @result; if ( /objectClass:\s+account/ and /objectClass:\s+ifastUser/ and my ($prods) = /ifastProducts:\s+(.+?)\s*$/mg # The /m is +important here. and my ($user) = /dn: uid=(\w+?),./g) { push @result, $prods, $user; print join '|', @result; print "\n"; } }

        But if you need the array, you can have it. The , $_) part of your code does nothing, so remove it. Here is my attempt to do what you need:

        #!/usr/bin/perl use warnings; use strict; $/ = "\n\n"; while (<>) { my @result; my @lines = split /\n/; if ( ( grep /objectClass:\s+account/, @lines) and (grep /objectClass:\s+ifastUser/, @lines) and (my ($prods) = map /ifastProducts:\s+(.+?)\s*$/g, @lines) and (my ($user) = map /dn: uid=(\w+?),.+/g, @lines +)) { push @result, $prods, $user; print join '|', @result; print "\n"; } }
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Get variables from if with regex
by TomDLux (Vicar) on Mar 09, 2013 at 19:29 UTC

    named captures.

    From the perlre page ...

    " ... as of Perl 5.10.0 you may use named capture buffers .... The notation is (?<name>...) to declare ..." (and $+{name} to access the captured value in code.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1022239]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-09-17 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (99 votes), past polls