regex and parsing

J

Justin Rodino

trying to utilise as few modules as possible, does anyone know how to
parse a line to get/set variable information? For example, here is my
line:

User="1233" Location="somewhere" Phone="1111"

and what i want to do is make variables $user=1233,
$location=somewhere, $phone=1111. There might be more than 3, there
might be less than 3 items...

Any help greatly appreciated. Thanks!
 
G

Garry Short

Justin said:
trying to utilise as few modules as possible, does anyone know how to
parse a line to get/set variable information? For example, here is my
line:

User="1233" Location="somewhere" Phone="1111"

and what i want to do is make variables $user=1233,
$location=somewhere, $phone=1111. There might be more than 3, there
might be less than 3 items...

Any help greatly appreciated. Thanks!

Purely to print the info out, this'll work:

1 my $a = "User=\"1233\" Location=\"somewhere\" Phone=\"1111\"";
2
3 my @fields = split / /, $a;
4 foreach $f (@fields) {
5 my ($var, $val) = split /=/, $f;
6 $val =~ s/"//g;
7 print "Var = $var, Val = $val\n";
8 }

Running it gives:
me@home:~> perl test.pl
Var = User, Val = 1233
Var = Location, Val = somewhere
Var = Phone, Val = 1111

Personally, I'd leave the quotes out. If you want to be able to use spaces
in the values, use a different record delimiter (newlines would do nicely).

HTH,

Garry
 
A

A. Sinan Unur

(e-mail address removed) (Justin Rodino) wrote in

Here is an example line that will be read in (it might get wrapped,
but you can be assured its always one line):

<ExecutionEnvironment ExecutionContext="Admin" LogonName=""
LogonDomain="" LogonPassword="" Interactive="false"
AllowUserExecution="true" UserNotifyTimeout="false" RunForUsers="First
user who logs in" CanRunWhen="whether or not a user is logged on"
StartWindow="Normal" ScheduleRetry="true" MinConnectionSpeed="0"/>

and if the file is in XML format as you indicate, why not use
XML::parser?

C:\develop\src\misc>cat txml.pl
#! C:/Perl/bin/perl.exe -w

use diagnostics;
use strict;
use warnings;

use XML::parser;

my $p = new XML::parser(Handlers => {Start => \&handle_start});

while(<DATA>) {
$p->parse($_);
}

sub handle_start {
my $Expat = shift;
my $Element = shift;
my ($attr, $val);
while((defined ($attr = shift)) && (defined ($val = shift))) {
print $attr, " = ", '"', $val,'"', "\n";
}
}

__DATA__
<ExecutionEnvironment ExecutionContext="Admin" LogonName=""
LogonDomain="" LogonPassword="" Interactive="false"
AllowUserExecution="true" UserNotifyTimeout="false"
RunForUsers="First user who logs in"
CanRunWhen="whether or not a user is logged on" StartWindow="Normal"
ScheduleRetry="true" MinConnectionSpeed="0"/>

Note that the above is entered as a single line, but wrapped in the
newsreader.

Output:

C:\develop\src\misc>txml.pl
ExecutionContext = "Admin"
LogonName = ""
LogonDomain = ""
LogonPassword = ""
Interactive = "false"
AllowUserExecution = "true"
UserNotifyTimeout = "false"
RunForUsers = "First user who logs in"
CanRunWhen = "whether or not a user is logged on"
StartWindow = "Normal"
ScheduleRetry = "true"
MinConnectionSpeed = "0"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

RegEx 0
Tasks 1
Robust regex 2
regex; ^ and $ 6
Parsing multiple lines from text file using regex 0
Parsing (a Series of) Variables 21
Weird Behavior with Rays in C and OpenGL 4
Parsing an Array of Hashes 3

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,301
Latest member
BuyPureganics

Latest Threads

Top