my @parts = split ' ', $str;
my ($ip, $path, $user, $pass, $port) =
(@parts[0, 1], map { (split /:/)[-1] } @parts[2,3,4]);
gav^ | [reply] [d/l] |
A clever solution!
If things get out of order, though, things could get messy. A parameter could be missing too. Slower, but more thoroughly:
my @parts = split (' ', $str);
my ($ip, $path) = @parts[0,1];
my ($user, $pass, $port) = @{{map {split(/:/)} @parts[2,3,4]}}{'l','p'
+,'port'};
This creates an anonymous hash with entries according to your 'x:y' specification, though no checking is made for 'x:y:z', which is presumably invalid and liable to break this badly. Assuming that's alright, then what happens is the 'l', 'p' and 'port' values are extracted, in that specific order. If one is not specified, it comes out as undef.
| [reply] [d/l] |
Is it possible for the password to contain whitespace? | [reply] |
this is p3rldud3 again.
thanks gav,i took your approach, looks good, but it doesnt work correctly for me!
lets get back to some another example input line.
211.46.248.1 /the / directory / is / here/ l:user p:pass port:21
the $path can be long and also have several whitespaces in it. at the first expression, i used a expression to get the path starting at the first "/" and the last "/".
then it should work fine!
would you help me inserting this fix in your code? many thanks!
perldude
| [reply] |
my $foo = "211.46.248.1 /the / directory / is / here/ l:user p:pass po
+rt:21";
my @bar = $foo =~ /^(\d+\.\d+\.\d+\.\d+) +(.+)(?= l:) +l:(\S+) p:(\S+)
+ port:(\d+)/;
print join ',', @bar;
# prints 211.46.248.1,/the / directory / is / here/,user,pass,21
You may want to qr// the RegEx if you plan to use it
inside of a loop while parsing your file.
--perlplexer | [reply] [d/l] |