regex


double task if pattern matches in Perl


I wander if I can do 2 different things for a single pattern match:
I'm reading a file with the following line:
( 1,<2010-01-12 15:19:51.32 +0100>,<marc>,<telescope1>,
I am trying this strategy:
open(FILE, "<tmpinp.txt");
while (<FILE>){
{$expstart = timelocal($6,$5,$4,$2,($3-1),$1) && $usr=$7} if
/.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*>,<(\S+)>,<(\S+)>.+\n/;
};
close(FILE);
};
Thus I wanna get the epoch time and the username (marc in this example)
in one run. It just doesnt work. Any help!?
Thanx a lot!
The clearest way would be to use the non-postfix form of if:
while (<FILE>){
if (/.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*>,<(\S+)>,<(\S+)>.+\n/) {
# this is a block; you can put as many statements in here as you want
$expstart = timelocal($6,$5,$4,$2,($3-1),$1);
$usr = $7;
}
}
If you really, really want to use the postfix if for some reason, you could still use it with a block, thanks to do:
while (<FILE>){
do {
# this is a block; you can put as many statements in here as you want
$expstart = timelocal($6,$5,$4,$2,($3-1),$1);
$usr = $7;
} if /.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*>,<(\S+)>,<(\S+)>.+\n/;
}
Or you can combine the two statements into one in some esoteric manner. This can be useful for micro-optimizing a condition within a very tight loop. For example, you could use the comma operator to combine the two assignments:
while (<FILE>){
($expstart = timelocal($6,$5,$4,$2,($3-1),$1)), ($usr = $7)
if /.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*>,<(\S+)>,<(\S+)>.+\n/;
}
Or if you know timelocal will always return a single scalar value (never a list of zero, or more than one value), then you can use list assignment to set both $expstart and $usr:
while (<FILE>){
($expstart, $usr) = (timelocal($6,$5,$4,$2,($3-1),$1), $7)
if /.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*>,<(\S+)>,<(\S+)>.+\n/;
}
Or if you know timelocal will always be returning a "true" value (i.e not 0, or the empty string, or undef), you could use && as you were attempting:
while (<FILE>){
($expstart = timelocal($6,$5,$4,$2,($3-1),$1)) && ($usr = $7)
if /.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*>,<(\S+)>,<(\S+)>.+\n/;
}
TIMTOWTDI.
How about this:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
open(FILE, "<tmpinp.txt");
while (<FILE>){
if ($_ =~ /^.+1,<(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2}).*?>,<(\S+)>,<(\S+)>.+$/) {
print join(' => ', timelocal($6,$5,$4,$2,$3-1,$1), $7) . "\n";
}
}
close(FILE);
Prints:
1291213191 => marc

Related Links

Why isn't ModRewrite returning my regular expression as expected?
Regexp to selectively match email
Regex match two strings but not third (in random order)
Test but not select with regex
Pattern to delete lines causes the whole contents of the file to be deleted in Sed
Using a regex in a conditional vs. an outright awk/sed?
Regex for a valid month
Remove everything except a certain pattern
Hubot Not Responding to regex
RegExp to find first letter of words before #
Creating Index based on another field in logstash
Bug in R's grepl? [closed]
Regular Expression Google Form only numbers in a range
MS Word VBA macro to search and replace (Regex)
Repeat capture group ignoring whitespaces
regex help in notepad++ marking embedded values and copying into a new list

Categories

HOME
azure
typescript
actions-on-google
angular2-forms
apt-get
asana
weight
gluu
jasypt
pyusb
ceph
binary-search
square-connect
openframeworks
development-environment
session-cookies
sparkpost
openmodelica
jscrollpane
oracle-agile-plm
accumulo
eigen3
lookup-tables
gyroscope
teamcity-9.0
jpos
netcdf
mediator
grouping
listbox
codewarrior
mobile-center
charts.js
capstone
advanced-installer
sieve-of-eratosthenes
transpose
sagepay
eve
subscription
invalidate
multisite
ctl
gcc4.8
kony
maven-versions-plugin
unityscript
cmsmadesimple
webloadui
untagged
angular-directive
webdeploy-3.5
enhanced-ecommerce
scrutinizer
mongolab
skrollr
flexigrid
dnx
openocd
biojava
json4s
oboe.js
multiscreen
telescope
window-server
iife
adserver
android-design-library
excel-charts
sbrk
knockout-mvc
jquery-backstretch
xml.modify
delphi-2009
disabled-input
execcommand
deap
mainwindow
sizzle
tempo
datadesign
icon-fonts
nspersistentdocument
eject
cellphone
data-oriented-design
emacs-ecb
nscollectionviewitem
winamp
maemo
polyglot
non-relational-database
greensoftware
user-testing
plumtree

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App