Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Removing and replacing values from an array.

by kennethk (Abbot)
on Aug 18, 2014 at 16:00 UTC ( [id://1097870]=note: print w/replies, xml ) Need Help??


in reply to Removing and replacing values from an array.

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.

Replies are listed 'Best First'.
Re^2: Removing and replacing values from an array.
by douggie305 (Initiate) on Aug 18, 2014 at 16:40 UTC

    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.

        Unless I misunderstood the OP's requirement, it seems to me that this code will push both URLs and IPs into both arrays. Something is needed, such as a regex, to distinguish them and put URLs and IPs only into the right array. Finally, I don't see any reason to read the input file twice. Reading the file, doing all the cleaning of description and brackets, and dispatching the data to the right array could all be done in one go. Something like this very quick (untested) example:
        my (@ips, @urls); while (<>){ last if !/\S/; s/\s+.*//; s/\[\.\]/./g; if (/[^\d\.]) { push @urls, $_; } else { push @ips, $_; } }
        The regex to match URLs could be refined (and I would refine it in my real programs), but that's just a quick example.

        Thanks, that helps alot!!!

Log In?
Username:
Password:

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

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

    No recent polls found