English 中文(简体)
图2. 按附件分列的姓名图
原标题:Mapping of name to the filename in perl
  • 时间:2010-08-05 05:49:37
  •  标签:
  • perl

i am confused now... Here is my problem: I have a text file in this format

Tom                 //name
Washington 
account.txt             //filename
Gary                    //NAME
New York
accountbalance.png      //filename
Mary                    //name
New Jersey             
Michelle               //NAME
Larry                  //NAME
Charles                //NAME  
Washington
Real.cpp               //FILENAME
.
.goes on(large file)

我想摘取姓名和相应的档案名称。 例如,Charles是从事实际工作的人的名字。

我认为,我需要

  1. use a while loop
  2. used two if statements within it (one to extract name other to extract filenmae)
  3. end the while loop

Problem faced:I get name and filenames which are not corresponding to it...(due to no unformity of one to one relation in the text file reading) I want the name to be the key and filename to be the value and store this in the hash How to resolve this.....I am confused..Give me suggestions,Pls

问题回答

姓名总是// 姓名 之后,档案名称总是/filename 姓名如下,档案名称上的名称与档案名称链接,这相当简单:

#!/usr/bin/perl

use strict;
use warnings;

my $key;
my %name_to_filename;
while (<DATA>) {
    #only pay attention to lines that have //name or //filename
    #and save off the part before //name or //filename and which type it was
    next unless my ($name, $type) = m{(.*?)s+//(name|filename)}i;
    if ($type =~ /^name$/i) {
        $key = $name; #remember the last name seen
        next;
    }
    $name_to_filename{$key} = $name;
}

use Data::Dumper;
print Dumper \%name_to_filename;

__DATA__
Tom                 //name
Washington
account.txt             //filename
Gary                    //NAME
New York
accountbalance.png      //filename
Mary                    //name
New Jersey
Michelle               //NAME
Larry                  //NAME
Charles                //NAME
Washington
Real.cpp               //FILENAME

Have 3 variables Line_1,Line_2,Current_line. For first 2 lines read the variables Line_1,Line_2 are initialized. Now when reading 3rd line check whether its a File If yes then store the same in hash hash{filename} = name,city. If not the copy Line_2 to Line_1 and Current_line to Line_2. This shuld happen in a loop till whole file is read

Since you want to map names to a file name. The data shows that you get a list of names and then a file name. So you re going to need to store up keys until you know what you can store them with.

Additionally, since you didn t say anything about state names, I expect you want to ignore those. So we need a way to tell them apart. Fortunately, the states are a well-defined set, and can be put into a lookup table.

Then, we need a way to distinguish names from filenames, from what you show, I m going with the following pattern: at least one word character, then a single dot, then at least one word character for the extention.

So that will tell me whether we re on a file line, and can resolve the value of the pending names.

@ARGV =  /path/to/file ;

my %state_hash
    = ( Alabama => 1, Alaska => 1, Arizona => 1, ...
      ,  New Hampshire  => 1, ..., Wyoming => 1
      );

my ( @pending_names, %file_for );
while ( <> ) { 
    # Extract non-spaces at the beginning of the line
    # potentially separated with one-and-only-one space
    my ( $name_or_file ) = m/^(?:S+[ ]?)+)/;
    next unless $name_or_file or exists $state_hash{ $name_or_file };

    # if the extract value fits the file pattern
    if ( $name_or_file =~ m/^w+.w+$/ ) { 
        # store the name-file combination for each pending
        $file_for{ $_ } = $name_or_file foreach @pending_names;
        # they are not pending anymore, so clear them.
        @pending_names  = ();
    }
    else { 
        # store up pending names
        push @pending_names, $name_or_file;
    }
}

What you didn t ask to handle is whether or not, it being a "large file", a name is likely to repeat. If a name repeats more than once, you ll clobber the value you save last time.

可通过<代码>push<>/code>-ing on to the hash slot而不是简单地加以纠正。 与此类似:

push @{ $file_for{ $_ } }, $name_or_file foreach @pending_name;

该版本使用一个名称为%is_city <>/code>至像城市一样的电能线,并假设一个名称包含。 这两种假设都是坏的。 例如,我的名字包含一段时期,像麦迪逊这样的名字可以是城市或某个人的名字。

#!/usr/bin/perl

use strict;
use warnings;

my %is_city = map { $_ => 1 } (
    "Washington", "New York", "New Jersey",
);

my $key;
my %name_to_filename;
while (my $name = <DATA>) {
    chomp $name;
    next if $is_city{$name};
    if ($name =~ /[.]/) {
        $name_to_filename{$key} = $name;
        next;
    }
    $key = $name;
}

use Data::Dumper;
print Dumper \%name_to_filename;


__DATA__
Tom
Washington
account.txt
Gary
New York
accountbalance.png
Mary
New Jersey
Michelle
Larry
Charles
Washington
Real.cpp

假设所有档案名称都有.,而且档案名称为 /strong> 做的。

还假设城市和国家名单如此之多,无法找到完整的名单。

#! /usr/bin/env perl
use strict;
use warnings;

my @state_city_or_person;
my %files;

while(<>){
  chomp;
  if( index($_, . ) >= 0 ){
    push @{ $files{$_} }, @state_city_or_person;
    @state_city_or_person = ();
  }else{
    push @state_city_or_person, $_;
  }
}

use YAML;

print Dump \%files;
---
Real.cpp:
  - Mary
  - New Jersey
  - Michelle
  - Larry
  - Charles
  - Washington
account.txt:
  - Tom
  - Washington
accountbalance.png:
  - Gary
  - New York

你们仍然必须像城市和邦一样,通过和删除任何多余的数据,但这应当有助于你将其变为一种实际的可用格式。

如果从数据开始有某种结构,将是有益的。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...