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

Re: rename duplicate data

by monsoon (Pilgrim)
on Jun 22, 2012 at 02:06 UTC ( #977747=note: print w/ replies, xml ) Need Help??


in reply to rename duplicate data

You mean something like this?

while(<>){ chomp; if(/(ID=.+)$/){ if(++$ids{$1} > 1){ say $_, $ids{$1}; next; } } say; }

Try and get yourself a copy of the Perl Cookbook. It usually comes handy for such tasks.


Comment on Re: rename duplicate data
Download Code
Re^2: rename duplicate data
by blacknight (Initiate) on Jun 22, 2012 at 12:31 UTC
    #!/usr/bin/perl; use strict; use warnings; my $filename = $ARGV[0]; my $debug = $ARGV1; die "\n\tUSAGE: perl $0 output debug\n\n" unless $ARGV[0]; die "\n\tERROR: Cannot find the file $ARGV[0]\n\n" unless -e $ARGV[0]; open(IN,$filename); my $ids; while($filename){ chomp; if(/(ID=.+)$/){ if(++$ids{$1} > 1){ say $_, $ids{$1}; next; } } say; } print say;

    I am sorry for my english and my script error

    see you

      It would help if your code was properly formatted. Anyway, to use 'say' you need to

      use v5.10;

      for "my $debug" declaration you probably meant

      my $debug = $ARGV[1];

      'ids' need to be declared as a hash, not a scalar

      my %ids;

      while loop needs to read from the file handle that you opened instead of checking the truth value of $filename variable which causes infinite loop if $filename is anything other than 0 or empty string

      while(<IN>)

      'print say' at the end doesn't really do any good

      Continuing with the issues and advice:

      Please surround your code and data listings with <c> (at the beginning) and </c> (at the end). It makes your nodes much easier to read, and thus, more likely to draw help.

      Your indentation style may have been different, but surely you saw that your code didn't look 'right' (for some value of 'right' meaning 'easy to read'):

      #!/usr/bin/perl; use strict; use warnings; my $filename = $ARGV[0]; my $debug = $ARGV1; die "\n\tUSAGE: perl $0 output debug\n\n" unless $ARGV[0]; die "\n\tERROR: Cannot find the file $ARGV[0]\n\n" unless -e $ARGV[0]; + open(IN,$filename); my $ids; while($filename){ chomp; if(/(ID=.+)$/){ if(++$ids{$1} > 1){ say $_, $ids{$1}; next; } } say; } print say;

      The say in Line 18 probably doesn't do what you intended; print say; in Line 20 is meaningless.

      say is "Just like "print", but implicitly appends a newline" to quote the 5.014 doc. To use it, either include use 5.010 (or higher; 5.016 is current) as monsoon suggested or use feature qw(switch say); (where feature is a (relatively new) pragma to enable new features that are not available without specifically enabling them).

      Other issues start with the unnecessary (but harmless) semi-colon at the end of the hashbang (or, in order of severity, your incorrect attempt to invoke debug mode, as noted by monsoon.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://977747]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2014-08-21 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (142 votes), past polls