CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to open an .ntp file but am unsure about the name of the file?

 



iphone
User

Aug 22, 2010, 12:52 PM

Post #1 of 14 (1087 views)
How to open an .ntp file but am unsure about the name of the file? Can't Post

 

Code
INPUT:core\ssm\dleroma10.cpp;//source/core/ssm/dleroma/src/IxCDcfHandlerOma10.cpp - VU_SORE_MES_RMO.00.00.44 : 7 

OUTPUT:VU_SORE_MES_RMO

How do I change the below code to get the above output?Basically i want to print all the VU names in a given line?
use warnings;
while( <DATA> ){
(my $file_name) = /([^\\\/]+ \. [ch]) [\s#]/xms;
-----------------------------
(my $VU_name) = /^\/.VU \D/xms; --->How do i modify this??
-----------------------------
next if !$file_name;
print $file_name, "\n";
print $VU_name
}



(This post was edited by iphone on Aug 22, 2010, 11:56 PM)


BillKSmith
Veteran

Aug 22, 2010, 1:49 PM

Post #2 of 14 (1079 views)
Re: [iphone] Regex to print all the names that start with "VU" [In reply to] Can't Post


Code
(my $VU_name) = /^\/.VU \D/xms; --->How do i modify this??   

(my $VU_name) = /\s(VU\w+)/xms;





Either "\D" or "\w" would work for this example. "\w" would allow digits. "\D" would allow punctuation. Perhaps you need [a-z_A-Z].
Good Luck,
Bill


iphone
User

Aug 22, 2010, 2:18 PM

Post #3 of 14 (1075 views)
Re: [BillKSmith] How to grep unique values? [In reply to] Can't Post


Code
 
How to make sure duplicate values are not getting printed and only unique values are grepped?
while( <$file1> ){

(my $VU_name) = /\s(VU\w+)/xms;

next if !$VU_name;
print "$VU_name\n"; ------> Duplicate values are getting printed
print grep /$VU_name/, (@file_2, @file_3); ---->I want to grep only unique values

}
close $file1;



(This post was edited by iphone on Aug 22, 2010, 3:23 PM)


BillKSmith
Veteran

Aug 22, 2010, 4:01 PM

Post #4 of 14 (1065 views)
Re: [iphone] How to grep unique values? [In reply to] Can't Post

Use a hash to keep track of the names that have been seen.


Code
my %seen;   # add this before loop  

next if !$VU_name;

next if ((!$VU_name) or ($seen{$VU_name}++)); #replace prev. line


Good Luck,
Bill


iphone
User

Aug 22, 2010, 5:20 PM

Post #5 of 14 (1061 views)
Re: [BillKSmith] How to grep unique values? [In reply to] Can't Post

Thanks.Couple of questions.

1.Can you please explain me this line?How is %seen keeping track of the names?

2.open my $file1, '<', 'features.ntp' or die "could not open featues.txt: $!\n"; ---->Basically ,there is a folder called "p4" in the root directory on which am running this script.Inside this folder there is a .ftp file .I want to openthat file.How do I change this line to open that file?

Code
open my $file1, '<', '.\/p4\/*.ntp' or die "could not open NTP file: $!\n";  


Thanks



(This post was edited by iphone on Aug 22, 2010, 7:06 PM)


FishMonger
Veteran / Moderator

Aug 22, 2010, 8:27 PM

Post #6 of 14 (1050 views)
Re: [iphone] How to grep unique values? [In reply to] Can't Post

It's difficult to answer your question because you are unclear as to to the file that you want to open. Do you want to open a .txt file or a .ntp file or a .ftp file?

Do you know the exact name of the file? If so, then the answer is simple.


Code
my $file = '/p4/file.ftp'; 
open my $file1, '<', $file or die "could not open '$file' $!";



iphone
User

Aug 22, 2010, 8:38 PM

Post #7 of 14 (1048 views)
Re: [FishMonger] How to grep unique values? [In reply to] Can't Post

Thanks fishmonger.I want to open an .ntp file but am unsure about the name of the file.How to change the below code so that it picks up a file with extension .ntp on p4 folder?

Code
my $file = '/p4/file.ntp';  
open my $file1, '<', $file or die "could not open '$file' $!";



(This post was edited by iphone on Aug 22, 2010, 8:39 PM)


BillKSmith
Veteran

Aug 23, 2010, 9:23 AM

Post #8 of 14 (1031 views)
Re: [iphone] How to grep unique values? [In reply to] Can't Post


Quote
my %seen; # add this before loop

next if !$VU_name;

next if ((!$VU_name) or ($seen{$VU_name}++)); #replace prev. line


In Reply To
1.Can you please explain me this line?How is %seen keeping track of the names?

This is a common perl 'idiom' which I first saw in the book "Perl CookBook" In the case where a name has been seen, a key with that name exists in the hash. The corresponding value is number of times that it has been seen. A non-zero value is always 'true'. The ++ increments the count. Things are more complicated in the other case. The key does not exist. The hash returns a value of 'undef' which is 'false'. In order for the ++ to do anything, perl has to create (sometimes called autovivify) the key with a corresponding value of 'undef'. Incrementing 'undef' changes it to '1'.
Good Luck,
Bill


iphone
User

Aug 23, 2010, 12:28 PM

Post #9 of 14 (1022 views)
Re: [BillKSmith] How to grep unique values? [In reply to] Can't Post

Thanks Bill.I want to open an .ntp file but am unsure about the name of the file.How to change the below code so that it picks up a file with extension .ntp on p4 folder?Even I rename the file as file.ntp,the below code snippet doesnt seem to work for me..Please help

Code
my $file = '/p4/file.ntp';  
open my $file1, '<', $file or die "could not open '$file' $!";



BillKSmith
Veteran

Aug 23, 2010, 1:41 PM

Post #10 of 14 (1017 views)
Re: [iphone] How to grep unique values? [In reply to] Can't Post

You should trust FishMonger's code. There are only three possibilities.

  • The code is unable to open the file. It reports "Could not open /p4/file.ntp......"


  • This code is not being executed


  • The file is being opened. Your problem is elsewhere.




  • In the first case, the exact message is important. If you cannot understand it, ask here. It probably has something to do with the name or privileges of the file or its directory. It may help if you tell us your operating system and show proof of the names and privileges.

    If your are not getting that message, you can distinguish between the other two cases by temporarily putting a warn statement immediately after the open to verify that it is executed.
    Good Luck,
    Bill


    iphone
    User

    Aug 23, 2010, 2:08 PM

    Post #11 of 14 (1014 views)
    Re: [BillKSmith] How to grep unique values? [In reply to] Can't Post

    The below code works

    Code
    my $file = 'E:\code\orphan_script\p4\file.ntp 
    open my $file1, '<', $file or die "could not open '$file' $!";

    but,if I change the code to below,throws the following error.I am running this script from E:\code\orphan_script folder

    Code
    my $file = '/p4/file.ntp';  
    open my $file1, '<', $file or die "could not open '$file' $!";
    ERROR:-
    could not open '/p4/file.ntp' No such file or directory at orphan.pl line 11.



    In the first case, the exact message is important. If you cannot understand it, ask here. It probably has something to do with the name or privileges of the file or its directory. It may help if you tell us your operating system and show proof of the names and privileges.
    [[I am using WinXP and am running it local hard drive shouldn't have privileges issues]]


    (This post was edited by iphone on Aug 23, 2010, 3:22 PM)


    BillKSmith
    Veteran

    Aug 23, 2010, 4:34 PM

    Post #12 of 14 (994 views)
    Re: [iphone] How to grep unique values? [In reply to] Can't Post


    Code
    my $file = '/p4/file.ntp';  

    my $file = 'p4/file.ntp';



    Windows still uses most of the original DOS syntax.

    The first line says that pr\ is in the root directory of the curren drive.

    The second says that pr\ is in the current directory.

    Note: DOS required back slashes as directory seperators. Windows XP accepts forward slashes as well. The forward slashes are usually used in perl programming because they do not have to be escaped in double quoted strings.
    Good Luck,
    Bill


    iphone
    User

    Aug 26, 2010, 1:14 AM

    Post #13 of 14 (945 views)
    Re: [BillKSmith] How to grep unique values? [In reply to] Can't Post

    Thanks Bill.One question though.I don't know the name of the file
    I want to open .ntp file in p4 folder

    Will this work?Please suggest


    Code
    my $file = '/p4/*.ntp';



    BillKSmith
    Veteran

    Aug 26, 2010, 8:19 AM

    Post #14 of 14 (938 views)
    Re: [iphone] How to grep unique values? [In reply to] Can't Post

    No. Refer perldoc -f glob
    Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives