Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Processing words in a file.

by kcott (Chancellor)
on Feb 12, 2013 at 07:24 UTC ( #1018311=note: print w/replies, xml ) Need Help??

in reply to Processing words in a file.

G'day brtch,

Welcome to the monastery.

Providing a little more context to your question would have been preferable. I'm assuming the first three fields are: month day hours:minutes:seconds. I'll leave you to extrapolate from there.

Perl has different operators for string and numerical comparisons. '==' is the numerical equality operator; 'eq' is for strings. See perlop for all the different operators; perlop - Equality Operators specifically discusses '==' and 'eq'.

How you go about breaking up your line for comparison will depend on how much detail you want (e.g. do you want to look at 'yy:yy:yy' as a whole or are you interested in the subfields). I see two main options you might pursue: using the split function or using a regular expression.

Using split can be as simple as:

$ perl -Mstrict -Mwarnings -E ' my $line = q{xxx yy yy:yy:yy xxx/xxxxx xxx xxxx xxx xxx}; my @fields = split / / => $line; say $fields[2]; ' yy:yy:yy

The problem with this level of simplicity is when further down your code you hit $fields[7] and have to backtrack to determine which field index 7 refers to. Ways around this include giving symbolic names to the indices or capturing each field into a meaningfully named variable:

$ perl -Mstrict -Mwarnings -E ' use constant { MONTH => 0, DAY => 1, TIME => 2, }; my $line = q{xxx yy yy:yy:yy xxx/xxxxx xxx xxxx xxx xxx}; my @fields = split / / => $line; say $fields[TIME]; ' yy:yy:yy
$ perl -Mstrict -Mwarnings -E ' my $line = q{xxx yy yy:yy:yy xxx/xxxxx xxx xxxx xxx xxx}; my ($month, $day, $time, $rest) = split / / => $line; say $time; ' yy:yy:yy

If you want to get at the subfields, then a regular expression solution might be better:

$ perl -Mstrict -Mwarnings -E ' my $line = q{xxx 1 12:34:56 xxx/xxxxx xxx xxxx xxx xxx}; my $line_re = qr{^(\w+) (\d+) (\d+):(\d+):(\d+) (.*)}; my ($month, $day, $hour, $min, $sec, $rest) = $line =~ m{$line_re}; say $hour; ' 12

All of those parts in parentheses are called Capture Groups. The link I've provided discusses these (as well as Named Capture Groups which I'll leave you to research if you're interested).

-- Ken

Replies are listed 'Best First'.
Re^2: Processing words in a file.
by brtch (Initiate) on Feb 12, 2013 at 09:44 UTC
    Thanks Monks, The issue got resolved.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018311]
[Lady_Aleena]: Having to rewrite some parts of my site for mobile devices is such a pain in the butt.
[shmem]: Lady_Aleena: what editor are you using, on what platform?
[Lady_Aleena]: shmem, geany on Debian jessie.
[shmem]: some experience with vim?
[Lady_Aleena]: None
[shmem]: I don't know geany, and don't know whether it has support for ctags.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-04-27 12:03 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (505 votes). Check out past polls.