Solved

shift the % array with removal of the first element

Posted on 2000-03-14
3
282 Views
Last Modified: 2010-03-05
I am doing the following

my(%array) = undef;

@array{@one) = @two;

After I assign like this I always have
the first element in %array (%array[0])
present but undefined.

I would like to shift it out.

Please help.

0
Comment
Question by:sstouk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
fragtastic earned 50 total points
ID: 2616751
Hmmmmm

1. %array isn't an array, it's a hash;

2. If you want to undef something I'd use something like

undef @array;

It looks less like you are trying to assign something to @array.

3. Try changing your code to look like this. I'm not sure what it is you're trying to achieve, but this might be the way forward.

Are you trying to just create a variable at the start, or remove the value from one already in existence??

undef %hash;           # Use this to "erase" hash
my %hash;              # Or use this to create a local hash
$two = "Hello!!";
$hash{one) = $two;     # Now the key in %hash called one will have a value of whatever the scalar $two is. This was set earlier.

print $hash{one};      # This will print "Hello!!" to the screen. You are printing the value from %hash that has a key name of one;

So in smoother code.

my %colours;

$1 = "Blue";
$2 = "Red";

$colours{car} = $1;
$colours{truck} = $2;

print "My car is $colours{car}\n";
print "My truck is $colours{truck}\n";

Make sense? Or did you really want to use an array???


0
 
LVL 6

Author Comment

by:sstouk
ID: 2616791
Perfect Answer.
Thank You very much

the statement:
my %hash;

is all I needed.

Thaks again!
0
 
LVL 1

Expert Comment

by:fragtastic
ID: 2616797
Looked at the title of your question.

If you want to work with an array. Do this.

@array = (one, two, three, four, five, six);
print "Array is @array\n";
$first = shift(@array);
print "After shifting, array is @array\n";
print "and the element shifted out is $first\n";

If you're trying to shift elements out of a hash, don't. The data in a hash isn't stored in order like an array. When you take a slice of an array, it always returns the same value. A hash wouldn't as you use the key to access the value, not the position in the array. For example, using the above code

$array[0]   would have a value of one (or two if you look at it afer the shift) and would always have that value

It seems like you need to be clear on the differences between arrays and hashes and know when to use each one.

Perhaps you could explain in more depth what you are trying to do and post some example code so I can help more.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question