Solved

shift the % array with removal of the first element

Posted on 2000-03-14
3
265 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now