This snippet

while(<>) {
        for my $word ( split(/\s+/) ) {
                $idx{$word}{$page_number} = 1;
        }
        $page_number++ if ( /\014/ );
}

is part of a perl script which indexes words by page (I'm trying to rewrite it in python). I do not understand it, or rather, I can guess a part of it: in particular, if someone can explain the meaning of ( /\014/ ).
Thanks

Recommended Answers

All 2 Replies

\014 looks to be octal for a carraige return or newline. so /\014/ is a regexp looking for that pattern in $_. Probably it's counting lines, not sure why it does it like that though. Perl has a variable that tells you how many lines are in a file: '$.'

Thanks KevinADC.
In any way here is the complete snippet

#!/usr/bin/perl

use warnings;
use strict;

my $page_number = 1;
my %idx;

while(<>) {
        for my $word ( split(/\s+/) ) {
                $idx{$word}{$page_number} = 1;
        }
        $page_number++ if ( /\014/ );
}

foreach my $word (sort keys %idx) {
        printf "%-20s ", $word;
        print comma_sep(sort {$a <=> $b} keys %{$idx{$word}});
        print "\n";
}

sub comma_sep {
        my $ret = "";
        foreach (@_) { $ret .= $_ . ", "; }
        chop $ret;
        chop $ret;
        return $ret;
}

As output it gives

word1 - 1,4,6
word2 - 5,9,23
word3 - 7,,44,88

where the numbers are the page numbers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.