Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Appending an array

by boo_radley (Parson)
on May 30, 2002 at 18:24 UTC ( [id://170466]=note: print w/replies, xml ) Need Help??


in reply to Appending an array

push @data, 'webpage3' unless grep (/webpage3/ , @data); should be all you need. I'm curious, how did you know to use pop, but be unaware of push?

Replies are listed 'Best First'.
Re: Re: Appending an array
by Anonymous Monk on May 30, 2002 at 18:42 UTC
    I need it to accept anyword and add it to the array. If someone enters webpage45 it would add to array such as: ('webpage1','webpage2','webpage45')

    Then the next time the script is run someone enters page67 would then make the array look like this: ('webpage1','webpage2','webpage45','page67')

    Please help me make this happen in my script.

      boo_radley's method will work for anything (provided you use a variable instead of a literal 'webpage3'), except that you probably shouldn't use a regexp match as he did and just use eq instead, in case they, for example, type in 'webpage' (boo_radley's would match, and thus not add, even though it's technically different). Depends on how you want to handle those cases, though.

      #!/usr/bin/perl use warnings; use strict; our @data = qw(webpage1 webpage2); print "Webpage needed: "; # <STDIN> flushes output buffers chomp(my $response = <STDIN>); # <STDIN> instead of <> in case they # add command line arguments push @data, $response unless grep { $_ eq $response } @data; print "$_\n" for @data;

      (Update: Expounded upon why eq should probably be used instead of m//)

      bbfu
      Black flowers blossum
      Fearless on my breath
      Teardrops on the fire
      Fearless on my breath

        push @data, $response unless grep { $_ eq $response } @data;
        Please advise, I understand now that the push function adds to end of list but why is the grep on the response needed?
        Basically what is the grep { $_ eq $response } @data part doing??

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found