Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Most Efficient Way to match this

by jmneedhamco (Novice)
on Mar 30, 2015 at 20:40 UTC ( [id://1121898]=perlquestion: print w/replies, xml ) Need Help??

jmneedhamco has asked for the wisdom of the Perl Monks concerning the following question:

I am reading in a file whose line looks like this:

userid:access_stuff:more_foo...

My script needs to match a string in a variable which happens to be a userid and I need to match the userid in the file...first "word" to that first colon.

example: userid matches userid so bale.

So, basically, the jist is: 1) the following code is my idea (it is paraprhased, because I am not going to actually write the code for opening files, etc here.) 2) I need to figure out the most efficient way to code the idea of: "if line to first colon matches variable".

if($linefraq eq $user) { return; #or otherwise abort from sub } else { #do whatever to the file }

The linefraq variable would hold just the userid "substring" from the line read from the file. Of course, in practice, each line will be read and checked for the userid.

Thank-you for your help!

Replies are listed 'Best First'.
Re: Most Efficient Way to match this
by Laurent_R (Canon) on Mar 30, 2015 at 21:06 UTC
    To get the first field of the line, assuming the line is stored in the $line variable you could do this, using the split function:
    my @fields = split /:/, $line; my $linefraq = $fields[0];
    Or, shorter:
    my $linefraq = (split /:/, $line)[0];
    You could also use a regular expression:
    $linefraq = $1 if $line =~ /^(\w+?):/;

    Je suis Charlie.
      One more way:
      my ($linefraq) = split ':', $line;
Re: Most Efficient Way to match this
by Anonymous Monk on Mar 30, 2015 at 21:10 UTC

    What you want to do can be accomplished fairly easily if you read perlintro and perlretut. The last bit of information you need is that you can embed variables into regular expressions, usually one uses \Q...\E aka quotemeta for this, to prevent special characters inside the variables from being interpreted as regular expression metacharacters.

    my $user = "one"; while (my $line=<DATA>) { chomp($line); print "match on $line\n" if $line=~/^\Q$user\E:/; } __DATA__ foo:bar:quz one:two:three a:b:c

    Prints "match on one:two:three".

Re: Most Efficient Way to match this
by aaron_baugher (Curate) on Mar 31, 2015 at 08:33 UTC

    The "most efficient" method would depend on what you're going to do with the lines that don't match. If you're going to be splitting them into fields, then you might as well split the line first and check the userid with a simple eq:

    while(<$file>){ my @fields = split ':'; last if $fields[0] eq $userid; }

    But if you're not doing that, then it's probably more efficient to leave the line as it is and do a simple regex check:

    while(<$file>){ last if /^$userid:/; # do whatever }

    One more note: if there's any chance that the userid field (or perhaps other fields) could contain colons, either escaped or quoted, you should probably use a module like Text::CSV to handle that properly.

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1121898]
Approved by marinersk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found