Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Removing and replacing values from an array.

by douggie305 (Initiate)
on Aug 18, 2014 at 15:34 UTC ( [id://1097864]=perlquestion: print w/replies, xml ) Need Help??

douggie305 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, I just started using perl, and this is my fist attempt and any sort or programming/scripting.

I would like to make a script that takes a file read from standard input then creates 3 arrays out it, the file contains IP addresses and URLs delimited with [] around the period, and after each there is white space followed by a description.

I want to first remove the description. In VI I would use %s/ .*//. to do this I would assume the same regex would work in the script.

Next I want to remove the braces and the period and replace it with just a period again in VI that would be %s/\[.\]/\./g

Then have what remains (just the IPs and URLs with out any braces or descriptions) put into an array that I will do more editing with, I have the last bit of code figured out I've been testing with the original file already having the [] and descriptions removed, Its just that the file's content will change every time I need to run this script so if I could make the script replace as well as the other functions it would save me a lot of time.

I don't have much code because I don't know where to start after I get the text into the script. I would like the new text placed in an array name @list after the braces and descriptions have been removed then I can take parts of that array and add them to 2 others one name @ips and the other @urls kinda self explanatory what my intent is but like I said I don't have the faintest idea where to start. Please help! Thanks in advance for any and all help!

#!/usr/bin/perl use warnings; use strict; foreach my $line (<STDIN>){ chomp $line; #remove extra words and braces. }

Replies are listed 'Best First'.
Re: Removing and replacing values from an array.
by kennethk (Abbot) on Aug 18, 2014 at 16:00 UTC
    Hi douggie305. Welcome to the monastery.

    What are you using as your template for learning Perl? There are a number of great resources out there, e.g. perlintro, Tutorials, course notes offered by Perl Training Australia. As you seem to know regular expressions from VI, you should find the information in perlretut a straightforward guide to Perl's particulars.

    In posting, it's also particularly helpful to provide an explicit input and and explicit output, wrapped in code tags. It removes ambiguity associated with wordy descriptions. See How do I post a question effectively?.

    I'm guessing you file looks vaguely like

    IP URL Desc IP URL Desc IP URL Desc IP URL Desc
    where the IPs look like 1[.]1[.]1[.]1 and the URLs look like url[.]com. The first natural step here would be split, where, since your description may contain whitespace, you will want to use the split /PATTERN/,EXPR,LIMIT syntax. That line might look like
    my @array = split /\s+/, $line, 3;
    or even better
    my ($ip, $url, $desc) = split /\s+/, $line, 3;
    You then want to clean up the IP and URL, so maybe
    $ip =~ s/\[\.\]/./g ; $url =~ s/\[\.\]/./g ;
    Note the subtle change in the regular expression from what you've proposed. You next step will depend on whether you just want a streaming parser, at which time you could just output, or if you actually want to store them for later use. Assuming you want to store them, you'd insert a declaration for 3 arrays outside your loop (my (@ips, @urls, @list);) and then push the new values onto the arrays:
    push @ips, $ip; push @urls, $url; push @list, $stuff_you_processed;
    This should give you a good starting point. Let me know how things progress.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks for the quick reply kennethk!

      This is what I've been using as a guide so far, http://www.perl.org/books/beginning-perl/

      The file's contents looks like this.

      1[.]1[.]1[.]1 description 1[.]1[.]1[.]1 description 1[.]1[.]1[.]1 description url[.]com description url[.]com description url[.]com description url[.]com description

      When done I want it to look like this.

      1.1.1.1 1.1.1.1 1.1.1.1 url.com url.com url.com url.com

      Or like this.

      @ips 1.1.1.1 1.1.1.1 1.1.1.1 @urls url.com url.com url.com url.com

      because eventually they'll need to be 2 files.

      Again thanks for all the help!

        If I had this task, I would probably solve your issue with a one liner:
        perl -pibak -e 's/^\s.*//; s/\[\.\]/./g;' filename
        See -p, -i[extension], and -e commandline in perlrun for details on the command line flags. In script form, this might be implemented as (where I have now stored data rather than modifying the input file)
        #!/usr/bin/perl use warnings; use strict; my @ips; while (my $line = <STDIN>){ last if $line =~ !/\S/; $line =~ s/^\s.*//; $line =~ s/\[\.\]/./g; push @ips, $line; } my @urls; while (my $line = <STDIN>){ last if $line =~ !/\S/; $line =~ s/^\s.*//; $line =~ s/\[\.\]/./g; push @urls, $line; }
        or more simply
        #!/usr/bin/perl use warnings; use strict; my @ips; while (<>){ last if !/\S/; s/^\s.*//; s/\[\.\]/./g; push @ips, $_; } my @urls; while (<>){ last if !/\S/; s/^\s.*//; s/\[\.\]/./g; push @urls, $_; }
        See Loop Control in perlsyn for details on last.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Removing and replacing values from an array.
by GotToBTru (Prior) on Aug 18, 2014 at 15:51 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097864]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 07:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found