Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

A small comma parsing problem...

by chinamox (Scribe)
on Oct 03, 2006 at 15:13 UTC ( [id://576095]=perlquestion: print w/replies, xml ) Need Help??

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

I have a tab delimited that looks somthing like this:

Name City Languages Hong Xiao Chongqing English,Mandarin Rob Huston, TX English,German,Japanese Jess Springfield, IL English,French Mike Austin, TX English,Perl,Java
ect...

I have written this very cute little program to let you find out who knows which language.

#!/opt/bin/perl -w use strict; # don't allow unsafe constructs my ( $name, $city, $language, $search ); my $whole; my $count = 1; my $line; my @matched; print "What language would you like to search for? "; $search = <STDIN>; chomp $search; #open the employees file open (EMPLOYEES, "employees.txt"); #for each line while ($line = <EMPLOYEES>) { #remove the carriage return chomp $line; #splits the line between tabs and get the different elements ($name, $city, $language) = split /\t/, $line; next unless $language =~ /^$search/; print "$name speaks $search\n"; } close (EMPLOYEES);

The only problme is that right now it only works for the entry "English" because the other languages are buried English without spaces (only commas). What is the fastest way to parse the elements assigned to the $language $scaler?

Thanks for your time and any help offered.

Replies are listed 'Best First'.
Re: A small comma parsing problem...
by davorg (Chancellor) on Oct 03, 2006 at 15:20 UTC

    Use \b to match on word boundaries.

    next unless $language =~ /\b$search\b/;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks! Elegant and effective ...
Re: A small comma parsing problem...
by hgolden (Pilgrim) on Oct 03, 2006 at 15:23 UTC
    Hey

    The problem isn't that the other languages are buried without spaces. The problem is that you've used the ^ metacharacter which means that the language has to be at the beginning of the string.

    If you remove that, the search will work (as long as you don't have the name of one language embedded in another).If you're worried about that, you can split the languages string by commas and search among the results, or set up a more complicated regular expression.

    Hays

      *smacks head* Thank one of the hardest things for me with learning Perl is keeping an eye out for the small things...
Re: A small comma parsing problem...
by ikegami (Patriarch) on Oct 03, 2006 at 15:20 UTC
    Some possibilities:
    # Assumes $search matches /^\w/ and /\w\z/. next unless $language =~ /\b\Q$search\E\b/;
    my @languages = split /,/, $language; next unless grep $_ eq $search, @languages;
    my %languages = map { $_ => 1 } split /,/, $language; next unless $languages{$search};

    By the way, $language should probably be named $languages.

Re: A small comma parsing problem...
by jdporter (Paladin) on Oct 03, 2006 at 15:37 UTC

    See my solution to this other problem; it will work for this problem too - just rename a variable or two.

    We're building the house of the future together.
      Thanks for the Pointer!
Re: A small comma parsing problem...
by Jasper (Chaplain) on Oct 03, 2006 at 16:04 UTC
    Might I suggest that you check your data set? You have an american in there who knows a language other than English. Can that be right?



    BOOM BOOM!
      I actually am American, and happen to live in Beijing. Been here for a few years, and speak Mandarin as well as the next Laowai...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2025-06-15 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.